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

GET http://localhost:8080/users/all

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

PUT http://localhost:8080/users/{id}

DELETE http://localhost:8080/users/{id}

Пример JSON для создания пользователя:

{

"name": "Иван Иванов", "email": "ivan@example.com", "phone": "+79990001122",

"deviceToken": "device-token-123", "telegramChatId": "123456789"

}

Часть 5. Реализация CRUD для Notification

5.1. Создайте NotificationService

package org.example.service;

import lombok.RequiredArgsConstructor; import org.example.model.dto.NotificationDto; import org.example.model.entity.Notification; import org.example.model.entity.User;

import org.example.model.enums.NotificationChannel; import org.example.model.enums.NotificationStatus; import org.example.repository.NotificationRepository; import org.example.repository.UserRepository;

import org.springframework.stereotype.Service;

import java.time.LocalDateTime; import java.util.List;

@Service

@RequiredArgsConstructor

public class NotificationService {

private final NotificationRepository notificationRepository; private final UserRepository userRepository;

public Notification createNotification(NotificationDto request) { User user =

userRepository.findById(request.getRecipientId()).orElseThrow();

Notification notification = new Notification(); notification.setTitle(request.getTitle()); notification.setMessage(request.getMessage()); notification.setChannel(request.getChannel());

12

notification.setStatus(NotificationStatus.CREATED);

notification.setCreatedAt(LocalDateTime.now());

notification.setRecipient(user);

return notificationRepository.save(notification);

}

public List<Notification> getAllNotifications() { return notificationRepository.findAll();

}

public Notification getNotificationById(Long id) {

return notificationRepository.findById(id).orElseThrow();

}

public Notification updateNotification(Long id, NotificationDto request)

{

Notification notification = notificationRepository.findById(id).orElseThrow();

notification.setTitle(request.getTitle());

notification.setMessage(request.getMessage());

notification.setChannel(request.getChannel());

notification.setStatus(request.getStatus()); return notificationRepository.save(notification);

}

public void deleteNotification(Long id) { Notification notification =

notificationRepository.findById(id).orElseThrow(); notificationRepository.delete(notification);

}

public List<Notification> getNotificationsByStatus(NotificationStatus status) {

return notificationRepository.findByStatus(status);

}

public List<Notification> getNotificationsByChannel(NotificationChannel channel) {

return notificationRepository.findByChannel(channel);

}

public List<Notification> getNotificationsByRecipientId(Long recipientId) {

return notificationRepository.findByRecipientId(recipientId);

}

}

13

5.2. Создайте NotificationController

package org.example.controller;

import jakarta.validation.Valid; import lombok.RequiredArgsConstructor;

import org.example.model.dto.NotificationDto; import org.example.model.entity.Notification; import org.example.model.enums.NotificationChannel; import org.example.model.enums.NotificationStatus; import org.example.service.NotificationService; import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController @RequestMapping("/notifications") @RequiredArgsConstructor

public class NotificationController {

private final NotificationService notificationService;

@PostMapping("/add")

public NotificationDto createNotification(@RequestBody @Valid NotificationDto request) {

Notification response = notificationService.createNotification(request);

return 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();

}

@GetMapping("/all")

public List<NotificationDto> getAllNotifications() {

return notificationService.getAllNotifications().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();

14

}

@GetMapping("/{id}")

public NotificationDto getNotificationById(@PathVariable Long id) { Notification response = notificationService.getNotificationById(id); return 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();

}

@PutMapping("/{id}")

public NotificationDto updateNotification(@PathVariable Long id, @RequestBody @Valid

NotificationDto request) {

Notification response = notificationService.updateNotification(id, request);

return 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();

}

@DeleteMapping("/{id}")

public String deleteNotification(@PathVariable Long id) { notificationService.deleteNotification(id);

return "Уведомление удалено";

}

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

public List<NotificationDto> getByStatus(@PathVariable NotificationStatus status) {

return notificationService.getNotificationsByStatus(status).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())

15

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