-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[FEAT] 알림 리스트 저장, 조회, 삭제 기능 구현
- Loading branch information
Showing
10 changed files
with
225 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/java/com/server/capple/domain/notifiaction/controller/NotificationController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.server.capple.domain.notifiaction.controller; | ||
|
||
import com.server.capple.config.security.AuthMember; | ||
import com.server.capple.domain.member.entity.Member; | ||
import com.server.capple.domain.notifiaction.dto.NotificationResponse.NotificationInfo; | ||
import com.server.capple.domain.notifiaction.service.NotificationService; | ||
import com.server.capple.global.common.BaseResponse; | ||
import com.server.capple.global.common.SliceResponse; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.data.domain.PageRequest; | ||
import org.springframework.data.domain.Sort; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@Tag(name = "알림 API", description = "알림 API입니다.") | ||
@RestController | ||
@RequiredArgsConstructor | ||
@RequestMapping("/notifications") | ||
public class NotificationController { | ||
private final NotificationService notificationService; | ||
|
||
@Operation(summary = "알림 리스트 조회 API", description = "API를 호출한 사용자가 받은 알림 리스트를 조회합니다. 알림은 최신순으로 정렬되어 반환됩니다.") | ||
@GetMapping | ||
public BaseResponse<SliceResponse<NotificationInfo>> getNotifications(@AuthMember Member member, @RequestParam(defaultValue = "0", required = false) Integer pageNumber, @RequestParam(defaultValue = "1000", required = false) Integer pageSize) { | ||
return BaseResponse.onSuccess(new SliceResponse<>(notificationService.getNotifications(member, PageRequest.of(pageNumber, pageSize, Sort.by(Sort.Direction.DESC, "createdAt"))))); | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/com/server/capple/domain/notifiaction/dto/NotificationResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.server.capple.domain.notifiaction.dto; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.ToString; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class NotificationResponse { | ||
@Getter | ||
@Builder | ||
@ToString | ||
@AllArgsConstructor | ||
public static class NotificationInfo { | ||
private String title; | ||
private String subtitle; | ||
private String content; | ||
private String boardId; | ||
private String boardCommentId; | ||
private LocalDateTime createdAt; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
src/main/java/com/server/capple/domain/notifiaction/entity/Notification.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.server.capple.domain.notifiaction.entity; | ||
|
||
import com.server.capple.global.common.BaseEntity; | ||
import jakarta.persistence.*; | ||
import lombok.*; | ||
|
||
@Getter | ||
@Entity | ||
@Builder | ||
@NoArgsConstructor(access = AccessLevel.PROTECTED) | ||
@AllArgsConstructor | ||
public class Notification extends BaseEntity { | ||
@Id | ||
@GeneratedValue(strategy = GenerationType.IDENTITY) | ||
private Long id; | ||
@Column(nullable = false) | ||
private Long memberId; | ||
@Column(nullable = false) | ||
private String title; | ||
private String subtitle; | ||
@Column(nullable = false) | ||
private String content; | ||
@Column(nullable = false) | ||
private String boardId; | ||
private String boardCommentId; | ||
} |
28 changes: 28 additions & 0 deletions
28
src/main/java/com/server/capple/domain/notifiaction/mapper/NotificationMapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package com.server.capple.domain.notifiaction.mapper; | ||
|
||
import com.server.capple.config.apns.dto.ApnsClientRequest; | ||
import com.server.capple.domain.notifiaction.entity.Notification; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
public class NotificationMapper { | ||
public Notification toNotification(Long memberId, ApnsClientRequest.BoardNotificationBody boardNotificationBody) { | ||
return Notification.builder() | ||
.memberId(memberId) | ||
.title(boardNotificationBody.getAps().getAlert().getTitle()) | ||
.content(boardNotificationBody.getAps().getAlert().getBody()) | ||
.boardId(boardNotificationBody.getBoardId()) | ||
.build(); | ||
} | ||
|
||
public Notification toNotification(Long memberId, ApnsClientRequest.BoardCommentNotificationBody boardCommentNotificationBody) { | ||
return Notification.builder() | ||
.memberId(memberId) | ||
.title(boardCommentNotificationBody.getAps().getAlert().getTitle()) | ||
.subtitle(boardCommentNotificationBody.getAps().getAlert().getSubtitle()) | ||
.content(boardCommentNotificationBody.getAps().getAlert().getBody()) | ||
.boardId(boardCommentNotificationBody.getBoardId()) | ||
.boardCommentId(boardCommentNotificationBody.getBoardCommentId()) | ||
.build(); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/server/capple/domain/notifiaction/repository/NotificationRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.server.capple.domain.notifiaction.repository; | ||
|
||
import com.server.capple.domain.notifiaction.entity.Notification; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
<T> Slice<T> findByMemberId(Long memberId, Pageable pageable, Class<T> type); | ||
void deleteNotificationsByCreatedAtBefore(LocalDateTime targetTime); | ||
} |
24 changes: 24 additions & 0 deletions
24
src/main/java/com/server/capple/domain/notifiaction/scheduler/NotificationScheduler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package com.server.capple.domain.notifiaction.scheduler; | ||
|
||
import com.server.capple.domain.notifiaction.service.NotificationService; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.scheduling.annotation.Scheduled; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Slf4j | ||
@Component | ||
@RequiredArgsConstructor | ||
public class NotificationScheduler { | ||
private final NotificationService notificationService; | ||
private final long NOTIFICATION_CACHE_WEEK = 1L; | ||
|
||
@Scheduled(cron = "0 0 * * * *") //매 0분에 | ||
public void deleteNotifications() { | ||
LocalDateTime targetTime = LocalDateTime.now().minusWeeks(NOTIFICATION_CACHE_WEEK); | ||
notificationService.deleteNotificationsByCreatedAtBefore(targetTime); | ||
log.info("오래된 알림을 제거했습니다. 제거 기한 : " + targetTime); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
src/main/java/com/server/capple/global/common/SliceResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.server.capple.global.common; | ||
|
||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import org.springframework.data.domain.Slice; | ||
|
||
import java.util.List; | ||
|
||
@Getter | ||
@NoArgsConstructor | ||
public class SliceResponse<T> { | ||
int number; | ||
int size; | ||
List<T> content; | ||
int numberOfElements; | ||
boolean hasPrevious; | ||
boolean hasNext; | ||
public SliceResponse(Slice<T> sliceObject) { | ||
number = sliceObject.getNumber(); | ||
size = sliceObject.getSize(); | ||
content = sliceObject.getContent(); | ||
numberOfElements = sliceObject.getNumberOfElements(); | ||
hasPrevious = sliceObject.hasPrevious(); | ||
hasNext = sliceObject.hasNext(); | ||
} | ||
} |