From 1e344af0548cdabe3f87e8e4a52b2123499cf9d0 Mon Sep 17 00:00:00 2001 From: jyj1289 Date: Sun, 24 Nov 2024 08:11:31 +0900 Subject: [PATCH] =?UTF-8?q?feat(#23):=20=EC=95=8C=EB=A6=BC=20=EC=A0=84?= =?UTF-8?q?=EC=B2=B4=20=EB=B0=9C=EC=86=A1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20-=20=EC=95=8C=EB=A6=BC=20=EC=A0=84=EC=B2=B4=20?= =?UTF-8?q?=EB=B0=9C=EC=86=A1=20API=EB=A5=BC=20=EA=B5=AC=ED=98=84=ED=96=88?= =?UTF-8?q?=EC=96=B4=EC=9A=94.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SendNotificationToAllUserUseCase.java | 31 +++++++++++++++++++ .../notification/NotificationController.java | 12 +++++++ .../dto/request/SendNotificationRequest.java | 3 ++ 3 files changed, 46 insertions(+) create mode 100644 src/main/java/com/bamdoliro/sinabro/application/notification/SendNotificationToAllUserUseCase.java diff --git a/src/main/java/com/bamdoliro/sinabro/application/notification/SendNotificationToAllUserUseCase.java b/src/main/java/com/bamdoliro/sinabro/application/notification/SendNotificationToAllUserUseCase.java new file mode 100644 index 0000000..07c7c6c --- /dev/null +++ b/src/main/java/com/bamdoliro/sinabro/application/notification/SendNotificationToAllUserUseCase.java @@ -0,0 +1,31 @@ +package com.bamdoliro.sinabro.application.notification; + +import com.bamdoliro.sinabro.domain.fcm.token.service.FCMTokenFacade; +import com.bamdoliro.sinabro.domain.notification.domain.Notification; +import com.bamdoliro.sinabro.domain.user.domain.User; +import com.bamdoliro.sinabro.infrastructure.fcm.FCMService; +import com.bamdoliro.sinabro.infrastructure.persistence.fcm.token.FCMTokenRepository; +import com.bamdoliro.sinabro.infrastructure.persistence.notification.NotificationRepository; +import com.bamdoliro.sinabro.infrastructure.persistence.user.UserRepository; +import com.bamdoliro.sinabro.presentation.notification.dto.request.SendNotificationRequest; +import com.bamdoliro.sinabro.shared.annotation.UseCase; +import lombok.RequiredArgsConstructor; +import org.springframework.transaction.annotation.Transactional; + +@RequiredArgsConstructor +@UseCase +public class SendNotificationToAllUserUseCase { + + private final FCMService fcmService; + private final NotificationRepository notificationRepository; + private final FCMTokenRepository fcmTokenRepository; + + @Transactional + public void execute(SendNotificationRequest request) { + fcmTokenRepository.findAll() + .forEach(fcmToken -> { + fcmService.sendMessageTo(fcmToken.getToken(), request.getTitle(), request.getBody()); + notificationRepository.save(new Notification(request.getTitle(), request.getBody(), fcmToken.getUser())); + }); + } +} diff --git a/src/main/java/com/bamdoliro/sinabro/presentation/notification/NotificationController.java b/src/main/java/com/bamdoliro/sinabro/presentation/notification/NotificationController.java index eb106d8..5abb5a9 100644 --- a/src/main/java/com/bamdoliro/sinabro/presentation/notification/NotificationController.java +++ b/src/main/java/com/bamdoliro/sinabro/presentation/notification/NotificationController.java @@ -1,11 +1,13 @@ package com.bamdoliro.sinabro.presentation.notification; import com.bamdoliro.sinabro.application.notification.QueryNotificationListUseCase; +import com.bamdoliro.sinabro.application.notification.SendNotificationToAllUserUseCase; import com.bamdoliro.sinabro.application.notification.SendNotificationUseCase; import com.bamdoliro.sinabro.domain.user.domain.User; import com.bamdoliro.sinabro.presentation.notification.dto.request.SendNotificationRequest; import com.bamdoliro.sinabro.presentation.notification.dto.response.ListNotificationResponse; import com.bamdoliro.sinabro.shared.auth.AuthenticationPrincipal; +import com.bamdoliro.sinabro.shared.auth.Authority; import com.bamdoliro.sinabro.shared.response.CommonResponse; import com.bamdoliro.sinabro.shared.response.ListCommonResponse; import jakarta.validation.Valid; @@ -19,6 +21,7 @@ public class NotificationController { private final SendNotificationUseCase sendNotificationUseCase; + private final SendNotificationToAllUserUseCase sendNotificationToAllUserUseCase; private final QueryNotificationListUseCase queryNotificationListUseCase; @ResponseStatus(HttpStatus.NO_CONTENT) @@ -30,6 +33,15 @@ public void sendNotification( sendNotificationUseCase.execute(user, request); } + @ResponseStatus(HttpStatus.NO_CONTENT) + @PostMapping("/all") + public void sendNotificationToAllUser( + @AuthenticationPrincipal(authority = Authority.ADMIN) User user, + @RequestBody @Valid SendNotificationRequest request + ) { + sendNotificationToAllUserUseCase.execute(request); + } + @GetMapping public ListCommonResponse queryNotification( @AuthenticationPrincipal User user diff --git a/src/main/java/com/bamdoliro/sinabro/presentation/notification/dto/request/SendNotificationRequest.java b/src/main/java/com/bamdoliro/sinabro/presentation/notification/dto/request/SendNotificationRequest.java index 72e9947..a2ca5ad 100644 --- a/src/main/java/com/bamdoliro/sinabro/presentation/notification/dto/request/SendNotificationRequest.java +++ b/src/main/java/com/bamdoliro/sinabro/presentation/notification/dto/request/SendNotificationRequest.java @@ -1,5 +1,6 @@ package com.bamdoliro.sinabro.presentation.notification.dto.request; +import jakarta.validation.constraints.NotBlank; import lombok.*; @Getter @@ -7,8 +8,10 @@ @AllArgsConstructor public class SendNotificationRequest { + @NotBlank(message = "필수값입니다.") private String title; + @NotBlank(message = "필수값입니다.") private String body; }