From 9bd66c5c3e102b2d760ff33b28868714cc176c4b Mon Sep 17 00:00:00 2001 From: alitpc25 Date: Tue, 12 Dec 2023 16:00:19 +0300 Subject: [PATCH] #555 Refactoring some API endpoints in backend. --- .../controller/NotificationController.java | 16 +++++++------ .../resq/converter/NotificationConverter.java | 24 +++++++++++++++++++ .../com/groupa1/resq/dto/NotificationDto.java | 18 ++++++++++++++ .../resq/response/ResourceResponse.java | 3 +-- 4 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/converter/NotificationConverter.java create mode 100644 resq/backend/resq/src/main/java/com/groupa1/resq/dto/NotificationDto.java diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/NotificationController.java b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/NotificationController.java index 6e54bd36..cabe1f9e 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/controller/NotificationController.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/controller/NotificationController.java @@ -1,14 +1,13 @@ package com.groupa1.resq.controller; -import com.groupa1.resq.entity.Need; -import com.groupa1.resq.entity.Notification; +import com.groupa1.resq.converter.NotificationConverter; +import com.groupa1.resq.dto.NotificationDto; import com.groupa1.resq.service.NotificationService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; -import java.math.BigDecimal; import java.util.List; @CrossOrigin(origins = "*", maxAge = 3600) @@ -20,17 +19,20 @@ public class NotificationController { @Autowired private NotificationService notificationService; + @Autowired + private NotificationConverter notificationConverter; + @GetMapping("/viewAllNotifications") @PreAuthorize("hasRole('FACILITATOR') or hasRole('VICTIM') or hasRole('RESPONDER')") - public List viewAllNotifications(@RequestParam Long userId) { + public List viewAllNotifications(@RequestParam Long userId) { log.info("Viewing all notifications"); - return notificationService.viewAllNotifications(userId); + return notificationService.viewAllNotifications(userId).stream().map(notificationConverter::convertToDto).toList(); } @GetMapping("/viewNotificationById") @PreAuthorize("hasRole('FACILITATOR') or hasRole('VICTIM') or hasRole('RESPONDER')") - public Notification viewNotificationById(@RequestParam Long notificationId, @RequestParam Long userId) { + public NotificationDto viewNotificationById(@RequestParam Long notificationId, @RequestParam Long userId) { log.info("Viewing notification with id: {}, user id: {}", notificationId, userId); - return notificationService.viewNotificationById(userId, notificationId); + return notificationConverter.convertToDto(notificationService.viewNotificationById(userId, notificationId)); } } diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/converter/NotificationConverter.java b/resq/backend/resq/src/main/java/com/groupa1/resq/converter/NotificationConverter.java new file mode 100644 index 00000000..5ac42c9b --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/converter/NotificationConverter.java @@ -0,0 +1,24 @@ +package com.groupa1.resq.converter; + +import com.groupa1.resq.dto.NotificationDto; +import com.groupa1.resq.entity.Notification; +import org.springframework.stereotype.Service; + +@Service +public class NotificationConverter { + + public NotificationDto convertToDto(Notification notification) { + NotificationDto notificationDto = new NotificationDto(); + notificationDto.setId(notification.getId()); + notificationDto.setCreatedAt(notification.getCreatedAt()); + notificationDto.setModifiedAt(notification.getModifiedAt()); + notificationDto.setNotificationType(notification.getNotificationType().toString()); + notificationDto.setUserId(notification.getUser().getId()); + notificationDto.setTitle(notification.getTitle()); + notificationDto.setBody(notification.getBody()); + notificationDto.setRead(notification.isRead()); + notificationDto.setRelatedEntityId(notification.getRelatedEntityId()); + notificationDto.setNotificationType(notification.getNotificationType().toString()); + return notificationDto; + } +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/dto/NotificationDto.java b/resq/backend/resq/src/main/java/com/groupa1/resq/dto/NotificationDto.java new file mode 100644 index 00000000..1637ef4a --- /dev/null +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/dto/NotificationDto.java @@ -0,0 +1,18 @@ +package com.groupa1.resq.dto; + +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class NotificationDto { + private Long id; + private LocalDateTime createdAt; + private LocalDateTime modifiedAt; + private Long userId; + private String title; + private String body; + private boolean isRead; + private Long relatedEntityId; + private String notificationType; +} diff --git a/resq/backend/resq/src/main/java/com/groupa1/resq/response/ResourceResponse.java b/resq/backend/resq/src/main/java/com/groupa1/resq/response/ResourceResponse.java index 1f9e98e3..24b05a69 100644 --- a/resq/backend/resq/src/main/java/com/groupa1/resq/response/ResourceResponse.java +++ b/resq/backend/resq/src/main/java/com/groupa1/resq/response/ResourceResponse.java @@ -1,6 +1,5 @@ package com.groupa1.resq.response; -import com.groupa1.resq.entity.User; import com.groupa1.resq.entity.enums.EGender; import lombok.Data; import lombok.experimental.Accessors; @@ -11,7 +10,7 @@ @Accessors(chain = true) public class ResourceResponse { private long id; - private User sender; + private long senderId; private int quantity; private EGender gender; private String categoryId;