From 4e173ed1ab3ace7397c735f16308839557d0bafd Mon Sep 17 00:00:00 2001 From: orijoon98 Date: Tue, 2 Jul 2024 22:42:27 +0900 Subject: [PATCH] =?UTF-8?q?=EC=A0=95=EB=93=A4=EC=97=88=EB=8D=98=20?= =?UTF-8?q?=ED=95=98=EC=9A=B0=EC=8A=A4=20=EB=A7=88=EC=A7=80=EB=A7=89=20?= =?UTF-8?q?=EC=BB=A4=EB=B0=8B=EC=9E=85=EB=8B=88=EB=8B=A4..=20=EA=B3=A0?= =?UTF-8?q?=EC=83=9D=ED=96=88=EC=96=B4=EC=9A=94=20=ED=98=9C=EC=A0=95?= =?UTF-8?q?=EC=9D=B4=20=ED=98=81=EC=A4=80=EC=9D=B4=20!!!!!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notification/NotificationController.java | 35 +++++++++++++++++++ .../notification/NotificationService.java | 11 ++++++ .../NotificationSendAllRequestDto.java | 19 ++++++++++ .../api/service/user/UserServiceUtils.java | 4 +++ 4 files changed, 69 insertions(+) create mode 100644 hous-api/src/main/java/hous/api/notification/NotificationController.java create mode 100644 hous-api/src/main/java/hous/api/service/notification/dto/request/NotificationSendAllRequestDto.java diff --git a/hous-api/src/main/java/hous/api/notification/NotificationController.java b/hous-api/src/main/java/hous/api/notification/NotificationController.java new file mode 100644 index 00000000..a7cf2716 --- /dev/null +++ b/hous-api/src/main/java/hous/api/notification/NotificationController.java @@ -0,0 +1,35 @@ +package hous.api.notification; + +import javax.validation.Valid; + +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import hous.api.service.notification.NotificationService; +import hous.api.service.notification.dto.request.NotificationSendAllRequestDto; +import hous.common.dto.SuccessResponse; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.RequiredArgsConstructor; + +@Api(tags = "Notification") +@RestController +@RequestMapping("/v1") +@RequiredArgsConstructor +public class NotificationController { + + private final NotificationService notificationService; + + @ApiOperation( + value = "마지막 API 입니당. ㅠㅠ 혜정이 혁준이 고생했어요.", + notes = "앱 종료 푸시 알림을 전송합니다." + ) + @PostMapping("/notification/all") + public ResponseEntity> sendAll(@Valid @RequestBody NotificationSendAllRequestDto request) { + notificationService.sendAll(request); + return SuccessResponse.OK; + } +} diff --git a/hous-api/src/main/java/hous/api/service/notification/NotificationService.java b/hous-api/src/main/java/hous/api/service/notification/NotificationService.java index fd8d9aef..cc098f61 100644 --- a/hous-api/src/main/java/hous/api/service/notification/NotificationService.java +++ b/hous-api/src/main/java/hous/api/service/notification/NotificationService.java @@ -6,6 +6,8 @@ import org.springframework.transaction.annotation.Transactional; import hous.api.config.sqs.producer.SqsProducer; +import hous.api.service.notification.dto.request.NotificationSendAllRequestDto; +import hous.api.service.user.UserServiceUtils; import hous.common.dto.sqs.FirebaseDto; import hous.core.domain.badge.BadgeInfo; import hous.core.domain.notification.Notification; @@ -18,6 +20,7 @@ import hous.core.domain.user.PushStatus; import hous.core.domain.user.TodoPushStatus; import hous.core.domain.user.User; +import hous.core.domain.user.mysql.UserRepository; import lombok.RequiredArgsConstructor; @RequiredArgsConstructor @@ -26,6 +29,7 @@ public class NotificationService { private final NotificationRepository notificationRepository; + private final UserRepository userRepository; private final SqsProducer sqsProducer; public void sendNewTodoNotification(User to, Todo todo, boolean isTake) { @@ -133,6 +137,13 @@ public void sendNewBadgeNotification(User to, BadgeInfo badgeInfo) { } } + public void sendAll(NotificationSendAllRequestDto request) { + List users = UserServiceUtils.findAllUsers(userRepository); + users.forEach(user -> { + sqsProducer.produce(FirebaseDto.of(user.getFcmToken(), request.getTitle(), request.getBody())); + }); + } + private String generateContent(String name, String message) { return String.format("'%s' %s", name, message); } diff --git a/hous-api/src/main/java/hous/api/service/notification/dto/request/NotificationSendAllRequestDto.java b/hous-api/src/main/java/hous/api/service/notification/dto/request/NotificationSendAllRequestDto.java new file mode 100644 index 00000000..971daac2 --- /dev/null +++ b/hous-api/src/main/java/hous/api/service/notification/dto/request/NotificationSendAllRequestDto.java @@ -0,0 +1,19 @@ +package hous.api.service.notification.dto.request; + +import lombok.AccessLevel; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.ToString; + +@ToString +@Getter +@NoArgsConstructor(access = AccessLevel.PRIVATE) +@AllArgsConstructor(access = AccessLevel.PRIVATE) +@Builder(access = AccessLevel.PRIVATE) +public class NotificationSendAllRequestDto { + + private String title; + private String body; +} diff --git a/hous-api/src/main/java/hous/api/service/user/UserServiceUtils.java b/hous-api/src/main/java/hous/api/service/user/UserServiceUtils.java index 9c664777..2c014ad6 100644 --- a/hous-api/src/main/java/hous/api/service/user/UserServiceUtils.java +++ b/hous-api/src/main/java/hous/api/service/user/UserServiceUtils.java @@ -42,6 +42,10 @@ public static User findUserBySocialIdAndSocialType(UserRepository userRepository return user; } + public static List findAllUsers(UserRepository userRepository) { + return userRepository.findAll(); + } + public static User findUserById(UserRepository userRepository, Long userId) { User user = userRepository.findUserById(userId); if (user == null) {