Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Методички / spring_lab4.pdf
Скачиваний:
0
Добавлен:
28.06.2026
Размер:
111.26 Кб
Скачать

.build())

.toList();

}

@GetMapping("/channel/{channel}")

public List<NotificationDto> getByChannel(@PathVariable NotificationChannel channel) {

return notificationService.getNotificationsByChannel(channel).stream()

.map(response -> NotificationDto.builder()

.title(response.getTitle())

.message(response.getMessage())

.channel(response.getChannel())

.status(response.getStatus())

.createdAt(response.getCreatedAt())

.sentAt(response.getSentAt())

.recipientId(response.getRecipient().getId())

.build())

.toList();

}

@GetMapping("/recipient/{recipientId}")

public List<NotificationDto> getByRecipientId(@PathVariable Long recipientId) {

return notificationService.getNotificationsByRecipientId(recipientId).stream()

.map(response -> NotificationDto.builder()

.title(response.getTitle())

.message(response.getMessage())

.channel(response.getChannel())

.status(response.getStatus())

.createdAt(response.getCreatedAt())

.sentAt(response.getSentAt())

.recipientId(response.getRecipient().getId())

.build())

.toList();

}

}

5.3. Проверьте работу CRUD-операций для Notification

Обратите внимание: в данном примере преобразование сущности Notification в NotificationDto выполняется вручную в каждом методе контроллера. Такой подход нагляден на начальном этапе изучения, но приводит к повторению одинакового кода. В качестве самостоятельного улучшения можно вынести эту логику в отдельный метод mapToDto() внутри контроллера или в отдельный mapper-класс.

POST http://localhost:8080/notifications/add

GET http://localhost:8080/notifications/all

GET http://localhost:8080/notifications/{id}

16

Соседние файлы в папке Методички