Skip to content

Commit

Permalink
feat(#23): 알림 전체 발송 기능 구현
Browse files Browse the repository at this point in the history
- 알림 전체 발송 API를 구현했어요.
  • Loading branch information
jyj1289 committed Nov 23, 2024
1 parent c1f45e1 commit 1e344af
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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()));
});
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,6 +21,7 @@
public class NotificationController {

private final SendNotificationUseCase sendNotificationUseCase;
private final SendNotificationToAllUserUseCase sendNotificationToAllUserUseCase;
private final QueryNotificationListUseCase queryNotificationListUseCase;

@ResponseStatus(HttpStatus.NO_CONTENT)
Expand All @@ -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<ListNotificationResponse> queryNotification(
@AuthenticationPrincipal User user
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.bamdoliro.sinabro.presentation.notification.dto.request;

import jakarta.validation.constraints.NotBlank;
import lombok.*;

@Getter
@ToString
@AllArgsConstructor
public class SendNotificationRequest {

@NotBlank(message = "필수값입니다.")
private String title;

@NotBlank(message = "필수값입니다.")
private String body;
}

0 comments on commit 1e344af

Please sign in to comment.