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..b9821031 --- /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 = "Todo") +@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) {