diff --git a/app/backend/src/main/java/com/app/gamereview/controller/NotificationController.java b/app/backend/src/main/java/com/app/gamereview/controller/NotificationController.java index 24132209..f4fe93ad 100644 --- a/app/backend/src/main/java/com/app/gamereview/controller/NotificationController.java +++ b/app/backend/src/main/java/com/app/gamereview/controller/NotificationController.java @@ -1,24 +1,14 @@ package com.app.gamereview.controller; -import com.app.gamereview.dto.request.post.GetPostListFilterRequestDto; -import com.app.gamereview.dto.response.comment.GetPostCommentsResponseDto; -import com.app.gamereview.dto.response.post.GetPostListResponseDto; +import com.app.gamereview.dto.request.notification.GetNotificationsRequestDto; import com.app.gamereview.model.Notification; import com.app.gamereview.model.User; import com.app.gamereview.service.NotificationService; -import com.app.gamereview.service.PostService; -import com.app.gamereview.util.JwtUtil; import com.app.gamereview.util.validation.annotation.AuthorizationRequired; import jakarta.servlet.http.HttpServletRequest; -import jakarta.validation.Valid; -import org.springdoc.core.annotations.ParameterObject; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpHeaders; import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestHeader; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; import java.util.List; @@ -34,9 +24,9 @@ public NotificationController(NotificationService notificationService) { @AuthorizationRequired @GetMapping("/get-notifications") - public ResponseEntity> getNotificationList( @RequestHeader String Authorization, HttpServletRequest request) { + public ResponseEntity> getNotificationList(@RequestHeader String Authorization, @RequestBody(required = false) GetNotificationsRequestDto filter, HttpServletRequest request) { User user = (User) request.getAttribute("authenticatedUser"); - List notifications = notificationService.getNotificationList(user.getId()); + List notifications = notificationService.getNotificationList(user.getId(), filter); return ResponseEntity.ok(notifications); } } diff --git a/app/backend/src/main/java/com/app/gamereview/dto/request/notification/GetNotificationsRequestDto.java b/app/backend/src/main/java/com/app/gamereview/dto/request/notification/GetNotificationsRequestDto.java new file mode 100644 index 00000000..02aefbc1 --- /dev/null +++ b/app/backend/src/main/java/com/app/gamereview/dto/request/notification/GetNotificationsRequestDto.java @@ -0,0 +1,10 @@ +package com.app.gamereview.dto.request.notification; + +import lombok.Getter; +import lombok.Setter; + +@Getter +@Setter +public class GetNotificationsRequestDto { + private Boolean isRead; +} diff --git a/app/backend/src/main/java/com/app/gamereview/model/Notification.java b/app/backend/src/main/java/com/app/gamereview/model/Notification.java index a0be37c2..3a24ba31 100644 --- a/app/backend/src/main/java/com/app/gamereview/model/Notification.java +++ b/app/backend/src/main/java/com/app/gamereview/model/Notification.java @@ -18,6 +18,17 @@ public class Notification extends BaseModel { private NotificationParent parentType; private String message; private String user; + private Boolean isRead = false; + public Notification(Notification notification) { + this.parent = notification.getParent(); + this.parentType = notification.getParentType(); + this.message = notification.getMessage(); + this.user = notification.getUser(); + this.isRead = notification.getIsRead(); + } + public void setIsRead(Boolean isRead) { + this.isRead = isRead; + } } diff --git a/app/backend/src/main/java/com/app/gamereview/service/NotificationService.java b/app/backend/src/main/java/com/app/gamereview/service/NotificationService.java index 29b58f21..e6c83abd 100644 --- a/app/backend/src/main/java/com/app/gamereview/service/NotificationService.java +++ b/app/backend/src/main/java/com/app/gamereview/service/NotificationService.java @@ -1,6 +1,7 @@ package com.app.gamereview.service; import com.app.gamereview.dto.request.notification.CreateNotificationRequestDto; +import com.app.gamereview.dto.request.notification.GetNotificationsRequestDto; import com.app.gamereview.model.Notification; import com.app.gamereview.repository.*; import org.modelmapper.ModelMapper; @@ -13,6 +14,7 @@ import java.time.LocalDateTime; import java.util.List; +import java.util.ArrayList; @Service public class NotificationService { private final NotificationRepository notificationRepository; @@ -28,13 +30,30 @@ public NotificationService(NotificationRepository notificationRepository, MongoT this.modelMapper = modelMapper; } - public List getNotificationList(String userId) { + public List getNotificationList(String userId, GetNotificationsRequestDto filter) { Query query = new Query(); query.addCriteria(Criteria.where("user").is(userId)); + if(filter != null && filter.getIsRead() != null){ + query.addCriteria(Criteria.where("isRead").is(filter.getIsRead())); + } query.with(Sort.by(Sort.Direction.DESC, "createdAt")); List notificationList = mongoTemplate.find(query, Notification.class); - return notificationList; + // create a deep copy to return without isRead being changed + List copiedList = new ArrayList<>(); + for (Notification notification : notificationList) { + copiedList.add(new Notification(notification)); + } + + if(filter == null || filter.getIsRead() == null){ + for (Notification notification : notificationList) { + if(!notification.getIsRead()) { + notification.setIsRead(true); + notificationRepository.save(notification); + } + } + } + return copiedList; } public Notification createNotification(CreateNotificationRequestDto request){