-
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.
Merge pull request #150 from Gamegoo-repo/feat/145
[Feat/145] 알림 관련 API 구현
- Loading branch information
Showing
13 changed files
with
584 additions
and
261 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
98 changes: 98 additions & 0 deletions
98
src/main/java/com/gamegoo/controller/notification/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,98 @@ | ||
package com.gamegoo.controller.notification; | ||
|
||
import com.gamegoo.apiPayload.ApiResponse; | ||
import com.gamegoo.converter.NotificationConverter; | ||
import com.gamegoo.domain.notification.Notification; | ||
import com.gamegoo.dto.notification.NotificationResponse; | ||
import com.gamegoo.dto.notification.NotificationResponse.notificationReadDTO; | ||
import com.gamegoo.service.notification.NotificationService; | ||
import com.gamegoo.util.JWTUtil; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.Parameter; | ||
import io.swagger.v3.oas.annotations.Parameters; | ||
import io.swagger.v3.oas.annotations.tags.Tag; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Slice; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.PatchMapping; | ||
import org.springframework.web.bind.annotation.PathVariable; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RequestParam; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
@RequestMapping("/v1/notification") | ||
@Tag(name = "Notification", description = "Notification 관련 API") | ||
public class NotificationController { | ||
|
||
private final NotificationService notificationService; | ||
|
||
@Operation(summary = "알림 목록 조회 API", description = "알림 팝업 화면에서 알림 목록을 조회하는 API 입니다.") | ||
@Parameters({ | ||
@Parameter(name = "cursor", description = "페이징을 위한 커서, Long 타입 notificationId를 보내주세요. 보내지 않으면 가장 최근 알림 10개를 조회합니다."), | ||
@Parameter(name = "type", description = "알림 타입 조회 필터, general 또는 friend를 입력해주세요.") | ||
}) | ||
@GetMapping | ||
public ApiResponse<NotificationResponse.cursorNotificationListDTO> getNotificationList( | ||
@RequestParam(name = "type") String type, | ||
@RequestParam(name = "cursor", required = false) Long cursor | ||
) { | ||
|
||
Long memberId = JWTUtil.getCurrentUserId(); | ||
|
||
Slice<Notification> notifications = notificationService.getNotificationListByCursor( | ||
memberId, type, cursor); | ||
|
||
return ApiResponse.onSuccess( | ||
NotificationConverter.toCursorNotificationListDTO(notifications)); | ||
} | ||
|
||
@Operation(summary = "알림 전체 목록 조회 API", description = "알림 전체보기 화면에서 알림 목록을 조회하는 API 입니다.") | ||
@Parameter(name = "page", description = "페이지 번호, 1 이상의 숫자를 입력해 주세요.") | ||
@GetMapping("/total") | ||
public ApiResponse<NotificationResponse.pageNotificationListDTO> getTotalNotificationList( | ||
@RequestParam(name = "page") Integer page | ||
) { | ||
Long memberId = JWTUtil.getCurrentUserId(); | ||
|
||
Page<Notification> notifications = notificationService.getNotificationListByPage( | ||
memberId, page - 1); | ||
|
||
return ApiResponse.onSuccess( | ||
NotificationConverter.toPageNotificationListDTO(notifications) | ||
); | ||
} | ||
|
||
@Operation(summary = "알림 읽음 처리 API", description = "특정 알림을 읽음 처리하는 API 입니다.") | ||
@Parameter(name = "notificationId", description = "읽음 처리할 알림의 id 입니다.") | ||
@PatchMapping("/{notificationId}") | ||
public ApiResponse<NotificationResponse.notificationReadDTO> getTotalNotificationList( | ||
@PathVariable(name = "notificationId") Long notificationId | ||
) { | ||
Long memberId = JWTUtil.getCurrentUserId(); | ||
|
||
Notification notification = notificationService.readNotification(memberId, notificationId); | ||
|
||
NotificationResponse.notificationReadDTO result = notificationReadDTO.builder() | ||
.notificationId(notification.getId()) | ||
.message("알림 읽음 처리 성공") | ||
.build(); | ||
|
||
return ApiResponse.onSuccess(result); | ||
} | ||
|
||
@Operation(summary = "안읽은 알림 개수 조회 API", description = "해당 회원의 안읽은 알림의 개수를 조회하는 API 입니다.") | ||
@GetMapping("/unread/count") | ||
public ApiResponse<Long> getUnreadNotificationCount( | ||
) { | ||
Long memberId = JWTUtil.getCurrentUserId(); | ||
|
||
return ApiResponse.onSuccess(notificationService.countUnreadNotification(memberId)); | ||
} | ||
|
||
|
||
} |
67 changes: 67 additions & 0 deletions
67
src/main/java/com/gamegoo/converter/NotificationConverter.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,67 @@ | ||
package com.gamegoo.converter; | ||
|
||
import com.gamegoo.domain.notification.Notification; | ||
import com.gamegoo.dto.notification.NotificationResponse; | ||
import com.gamegoo.dto.notification.NotificationResponse.notificationDTO; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Slice; | ||
|
||
public class NotificationConverter { | ||
|
||
public static NotificationResponse.cursorNotificationListDTO toCursorNotificationListDTO( | ||
Slice<Notification> notifications) { | ||
List<notificationDTO> notificationDTOList = notifications.stream() | ||
.map(NotificationConverter::toNotificationDTO).collect(Collectors.toList()); | ||
|
||
return NotificationResponse.cursorNotificationListDTO.builder() | ||
.notificationDTOList(notificationDTOList) | ||
.list_size(notificationDTOList.size()) | ||
.has_next(notifications.hasNext()) | ||
.next_cursor(notifications.hasNext() ? notifications.getContent().get(9).getId() | ||
: null) // next cursor를 현재 notificationList의 가장 마지막 요소의 id로 주기 | ||
.build(); | ||
} | ||
|
||
public static NotificationResponse.pageNotificationListDTO toPageNotificationListDTO( | ||
Page<Notification> notifications) { | ||
List<notificationDTO> notificationDTOList = notifications.stream() | ||
.map(NotificationConverter::toNotificationDTO).collect(Collectors.toList()); | ||
|
||
return NotificationResponse.pageNotificationListDTO.builder() | ||
.notificationDTOList(notificationDTOList) | ||
.listSize(notificationDTOList.size()) | ||
.totalPage(notifications.getTotalPages()) | ||
.totalElements(notifications.getTotalElements()) | ||
.isFirst(notifications.isFirst()) | ||
.isLast(notifications.isLast()) | ||
.build(); | ||
} | ||
|
||
public static NotificationResponse.notificationDTO toNotificationDTO( | ||
Notification notification) { | ||
|
||
String pageUrl = null; | ||
|
||
if (notification.getNotificationType().getSourceUrl() != null) { | ||
StringBuilder urlBuilder = new StringBuilder( | ||
notification.getNotificationType().getSourceUrl()); | ||
|
||
if (notification.getSourceId() != null) { | ||
urlBuilder.append(notification.getSourceId()); | ||
} | ||
|
||
pageUrl = urlBuilder.toString(); | ||
} | ||
|
||
return NotificationResponse.notificationDTO.builder() | ||
.notificationId(notification.getId()) | ||
.notificationType(notification.getNotificationType().getId().intValue()) | ||
.content(notification.getContent()) | ||
.pageUrl(pageUrl) | ||
.read(notification.isRead()) | ||
.createdAt(notification.getCreatedAt().withNano(0)) | ||
.build(); | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
src/main/java/com/gamegoo/dto/notification/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,62 @@ | ||
package com.gamegoo.dto.notification; | ||
|
||
import java.time.LocalDateTime; | ||
import java.util.List; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
|
||
public class NotificationResponse { | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class cursorNotificationListDTO { | ||
|
||
List<notificationDTO> notificationDTOList; | ||
Integer list_size; | ||
Boolean has_next; | ||
Long next_cursor; | ||
} | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class pageNotificationListDTO { | ||
|
||
List<notificationDTO> notificationDTOList; | ||
Integer listSize; | ||
Integer totalPage; | ||
Long totalElements; | ||
Boolean isFirst; | ||
Boolean isLast; | ||
} | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class notificationDTO { | ||
|
||
Long notificationId; | ||
int notificationType; | ||
String content; | ||
String pageUrl; | ||
boolean read; | ||
LocalDateTime createdAt; | ||
} | ||
|
||
@Builder | ||
@Getter | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
public static class notificationReadDTO { | ||
|
||
Long notificationId; | ||
String message; | ||
} | ||
|
||
} |
8 changes: 7 additions & 1 deletion
8
src/main/java/com/gamegoo/repository/notification/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 |
---|---|---|
@@ -1,8 +1,14 @@ | ||
package com.gamegoo.repository.notification; | ||
|
||
import com.gamegoo.domain.member.Member; | ||
import com.gamegoo.domain.notification.Notification; | ||
import org.springframework.data.domain.Page; | ||
import org.springframework.data.domain.Pageable; | ||
import org.springframework.data.jpa.repository.JpaRepository; | ||
|
||
public interface NotificationRepository extends JpaRepository<Notification, Long> { | ||
public interface NotificationRepository extends JpaRepository<Notification, Long>, | ||
NotificationRepositoryCustom { | ||
|
||
Page<Notification> findNotificationsByMember(Member member, Pageable pageable); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/gamegoo/repository/notification/NotificationRepositoryCustom.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,9 @@ | ||
package com.gamegoo.repository.notification; | ||
|
||
import com.gamegoo.domain.notification.Notification; | ||
import org.springframework.data.domain.Slice; | ||
|
||
public interface NotificationRepositoryCustom { | ||
|
||
Slice<Notification> findNotificationsByCursorAndType(Long memberId, String type, Long cursor); | ||
} |
Oops, something went wrong.