Skip to content

Commit

Permalink
#555 Refactoring some API endpoints in backend.
Browse files Browse the repository at this point in the history
  • Loading branch information
alitpc25 committed Dec 12, 2023
1 parent 61296cc commit 9bd66c5
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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<Notification> viewAllNotifications(@RequestParam Long userId) {
public List<NotificationDto> 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));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down

0 comments on commit 9bd66c5

Please sign in to comment.