From e6791853fc64e4324bca34838b38c573ca419958 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 5 Dec 2023 16:58:48 +0900 Subject: [PATCH 1/6] =?UTF-8?q?feat:=20naming=20=EB=B3=80=EA=B2=BD=20&=20c?= =?UTF-8?q?ontroller,=20usecase=20layer?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../command/api/AlarmController.java | 150 ++++++++++++ .../apimodule/command/api/AuthController.java | 6 +- .../apimodule/command/api/FcmController.java | 38 --- .../fcm/application/service/AlarmUseCase.java | 230 ++++++++++++++++++ .../application/service/NoticeFcmUseCase.java | 82 ------- 5 files changed, 383 insertions(+), 123 deletions(-) create mode 100644 api-module/src/main/java/com/koa/apimodule/command/api/AlarmController.java delete mode 100644 api-module/src/main/java/com/koa/apimodule/command/api/FcmController.java create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java delete mode 100644 core-module/src/main/java/com/koa/coremodule/notice/application/service/NoticeFcmUseCase.java diff --git a/api-module/src/main/java/com/koa/apimodule/command/api/AlarmController.java b/api-module/src/main/java/com/koa/apimodule/command/api/AlarmController.java new file mode 100644 index 00000000..bba2e01e --- /dev/null +++ b/api-module/src/main/java/com/koa/apimodule/command/api/AlarmController.java @@ -0,0 +1,150 @@ +package com.koa.apimodule.command.api; + +import com.koa.commonmodule.common.ApplicationResponse; +import com.koa.coremodule.fcm.application.dto.AlarmLists; +import com.koa.coremodule.fcm.application.service.AlarmUseCase; +import com.koa.coremodule.fcm.domain.entity.Alarm; +import com.koa.coremodule.fcm.domain.entity.AlarmType; +import com.koa.coremodule.fcm.domain.repository.AlarmRepository; +import com.koa.coremodule.member.domain.entity.Authority; +import com.koa.coremodule.member.domain.entity.Member; +import com.koa.coremodule.member.domain.repository.MemberRepository; +import com.koa.coremodule.notice.application.dto.fcm.RegisterTokenRequest; +import com.koa.coremodule.notice.application.dto.fcm.SendNotificationRequest; +import com.koa.coremodule.notice.domain.entity.Curriculum; +import com.koa.coremodule.notice.domain.entity.Notice; +import com.koa.coremodule.notice.domain.entity.NoticeTeam; +import com.koa.coremodule.notice.domain.repository.CurriculumRepository; +import com.koa.coremodule.notice.domain.repository.NoticeRepository; +import com.koa.coremodule.notice.domain.repository.NoticeTeamRepository; +import jakarta.annotation.PostConstruct; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@Slf4j +@RestController +@RequiredArgsConstructor +@RequestMapping("/v1/alarm") +public class AlarmController { + + private final AlarmUseCase alarmUseCase; + + private final MemberRepository memberRepository; + private final NoticeTeamRepository noticeTeamRepository; + private final CurriculumRepository curriculumRepository; + private final NoticeRepository noticeRepository; + private final AlarmRepository alarmRepository; + + @PostConstruct + public void test() { + final Member member = Member.builder() + .authority(Authority.MEMBER) + .email("austinan123@gmail.com") + .password("001215") + .name("안정후") + .build(); + memberRepository.save(member); + final Curriculum curriculum = Curriculum.builder() + .curriculumName("기업프로젝트") + .build(); + curriculumRepository.save(curriculum); + final NoticeTeam noticeTeam = NoticeTeam.builder() + .teamName("경영총괄팀") + .build(); + noticeTeamRepository.save(noticeTeam); + final Notice notice = Notice.builder() + .member(member) + .title("sampletitle") + .content("samplecontent") + .curriculum(curriculum) + .build(); + noticeRepository.save(notice); + final Notice notice2 = Notice.builder() + .member(member) + .title("sampletitle22") + .content("samplecontent22") + .curriculum(curriculum) + .build(); + noticeRepository.save(notice2); + final Alarm alarm = Alarm.builder().type(AlarmType.NOTICE).title("공지알림").content("notice content").build(); + alarmRepository.save(alarm); + final Alarm alar2 = Alarm.builder().type(AlarmType.COMMENT).title("댓글알림").content("comment content").build(); + alarmRepository.save(alar2); + } + + + /** + * 토큰 등록 + */ + @PostMapping("/register") + public ApplicationResponse registerToken(@RequestBody RegisterTokenRequest request) { + + alarmUseCase.registerFcmToken(request.fcmToken()); + return ApplicationResponse.ok(null, "성공적으로 토큰이 등록되었습니다."); + } + + /** + * 메세지 전달 ( 공지 알림 ) + */ + @PostMapping("/send/notice") + public ApplicationResponse sendNoticeNotification(@RequestBody SendNotificationRequest request) { + + alarmUseCase.sendNoticeNotification(request); + return ApplicationResponse.ok(null, "성공적으로 공지 알림을 보냈습니다."); + } + + /** + * 메세지 전달 ( 댓글 알림 ) + */ + @PostMapping("/send/comment") + public ApplicationResponse sendCommentNotification(@RequestBody SendNotificationRequest request) { + + alarmUseCase.sendCommentNotification(request); + return ApplicationResponse.ok(null, "성공적으로 댓글 알림을 보냈습니다."); + } + + /** + * 메세지 전달 ( 대댓글 알림 ) + */ + @PostMapping("/send/recomment") + public ApplicationResponse sendReCommentNotification(@RequestBody SendNotificationRequest request) { + + alarmUseCase.sendReCommentNotification(request); + return ApplicationResponse.ok(null, "성공적으로 대댓글 알림을 보냈습니다."); + } + + + /** + * 알림 목록 조회 ( 조회 여부까지 포함 ) + */ + @GetMapping("/lists") + public ApplicationResponse> getAlarmLists() { + + List lists = alarmUseCase.getAlarmLists(); + return ApplicationResponse.ok(lists, "성공적으로 알림 목록을 조회했습니다."); + } + + /** + * 링크 누를 시 api 호출 필요 ( 조회 여부 판단 ) + */ + @PostMapping("/view") + public ApplicationResponse saveAlarmView(Long alarmId) { + + alarmUseCase.saveAlarmView(alarmId); + return ApplicationResponse.ok(null, "성공적으로 조회 기록을 적재하였습니다."); + } + + /** + * 알림 삭제 + */ + @DeleteMapping("/delete") + public ApplicationResponse deleteAlarm(Long alarmId) { + + alarmUseCase.deleteAlarm(alarmId); + return ApplicationResponse.ok(null, "성공적으로 알림을 삭제했습니다."); + } + +} diff --git a/api-module/src/main/java/com/koa/apimodule/command/api/AuthController.java b/api-module/src/main/java/com/koa/apimodule/command/api/AuthController.java index ae19d880..f56510da 100644 --- a/api-module/src/main/java/com/koa/apimodule/command/api/AuthController.java +++ b/api-module/src/main/java/com/koa/apimodule/command/api/AuthController.java @@ -6,7 +6,7 @@ import com.koa.coremodule.auth.application.service.AuthUseCase; import com.koa.coremodule.auth.application.service.LogoutUseCase; import com.koa.coremodule.member.domain.entity.Authority; -import com.koa.coremodule.notice.application.service.NoticeFcmUseCase; +import com.koa.coremodule.fcm.application.service.AlarmUseCase; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.web.bind.annotation.*; @@ -20,7 +20,7 @@ public class AuthController { private final AuthUseCase authUseCase; private final LogoutUseCase logoutUseCase; - private final NoticeFcmUseCase noticeFcmUseCase; + private final AlarmUseCase alarmUseCase; @GetMapping("/login/{authority}") public ApplicationResponse authLogin(@PathVariable Authority authority, @RequestParam String email, @RequestParam String password){ @@ -39,7 +39,7 @@ public ApplicationResponse logout(@RequestHeader(AuthConsts.REFRESH_TOKEN_ logoutUseCase.logoutAccessUser(refreshToken); // 로그아웃 시 토큰 제거 - noticeFcmUseCase.deleteFcmToken(); + alarmUseCase.deleteFcmToken(); return ApplicationResponse.ok(null); } diff --git a/api-module/src/main/java/com/koa/apimodule/command/api/FcmController.java b/api-module/src/main/java/com/koa/apimodule/command/api/FcmController.java deleted file mode 100644 index 39d539a3..00000000 --- a/api-module/src/main/java/com/koa/apimodule/command/api/FcmController.java +++ /dev/null @@ -1,38 +0,0 @@ -package com.koa.apimodule.command.api; - -import com.koa.commonmodule.common.ApplicationResponse; -import com.koa.coremodule.notice.application.dto.fcm.RegisterTokenRequest; -import com.koa.coremodule.notice.application.dto.fcm.SendNotificationRequest; -import com.koa.coremodule.notice.application.service.NoticeFcmUseCase; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -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; - -@Slf4j -@RestController -@RequiredArgsConstructor -@RequestMapping("/v1/fcm") -public class FcmController { - - private final NoticeFcmUseCase noticeFcmUseCase; - - @PostMapping("/register") - public ApplicationResponse registerToken(@RequestBody RegisterTokenRequest request) { - - noticeFcmUseCase.registerFcmToken(request.fcmToken()); - - return ApplicationResponse.ok(null, "성공적으로 토큰이 등록되었습니다."); - } - - @PostMapping("/send") - public ApplicationResponse sendNotification(@RequestBody SendNotificationRequest request) { - - noticeFcmUseCase.sendNotification(request.title(), request.content()); - - return ApplicationResponse.ok(null, "성공적으로 알림을 보냈습니다."); - } - -} diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java b/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java new file mode 100644 index 00000000..8faf5b93 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java @@ -0,0 +1,230 @@ +package com.koa.coremodule.fcm.application.service; + +import com.google.firebase.messaging.FirebaseMessaging; +import com.google.firebase.messaging.FirebaseMessagingException; +import com.google.firebase.messaging.Message; +import com.google.firebase.messaging.Notification; +import com.koa.coremodule.fcm.application.dto.AlarmLists; +import com.koa.coremodule.fcm.domain.entity.Alarm; +import com.koa.coremodule.fcm.domain.entity.AlarmType; +import com.koa.coremodule.fcm.domain.entity.AlarmView; +import com.koa.coremodule.fcm.domain.service.AlarmFindService; +import com.koa.coremodule.fcm.domain.service.AlarmSaveService; +import com.koa.coremodule.member.domain.entity.Member; +import com.koa.coremodule.member.domain.utils.MemberUtils; +import com.koa.coremodule.notice.application.dto.fcm.SendNotificationRequest; +import com.koa.coremodule.notice.domain.entity.Notice; +import com.koa.coremodule.notice.domain.entity.ViewType; +import com.koa.coremodule.notice.domain.service.NoticeQueryService; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.ArrayList; +import java.util.List; + +@Slf4j +@Service +@RequiredArgsConstructor +@Transactional +public class AlarmUseCase { + + private final FirebaseMessaging firebaseMessaging; + private final MemberUtils memberUtils; + private final AlarmSaveService alarmSaveService; + private final AlarmFindService alarmFindService; + private final NoticeQueryService noticeQueryService; + + private final static String NOTICE_TITLE = "새로운 공지를 확인하세요"; + private final static String COMMENT_TITLE = "새로운 댓글이 달렸어요"; + + public void registerFcmToken(String token) { + + Member memberRequest = memberUtils.getAccessMember(); + + Member member = findMember(memberRequest.getId()); + member.updateFcmToken(token); + log.info("회원의 fcm 토큰이 등록되었습니다."); + } + + public void sendNoticeNotification(SendNotificationRequest request) { + + List members = findAllMember(); + + Notification notification = Notification.builder() + .setTitle(NOTICE_TITLE) + .setBody(request.content()) + .build(); + + for (Member m : members) { + if (m.getFcmToken() != null && !m.getFcmToken().isEmpty()) { + Message message = Message.builder() + .setToken(m.getFcmToken()) + .setNotification(notification) + .build(); + + try { + firebaseMessaging.send(message); + + Notice notice = noticeQueryService.findByNoticeId(request.noticeId()); + // 알람 테이블 저장 + Alarm alarm = Alarm.builder() + .type(AlarmType.NOTICE) + .title(NOTICE_TITLE) + .content(request.content()) + .notice(notice) + .build(); + alarmSaveService.save(alarm); + + } catch (FirebaseMessagingException ex) { + log.info("알림 전송에 실패했습니다."); + } + } + } + } + + public void sendCommentNotification(SendNotificationRequest request) { + + Member member = findNoticeMember(request.noticeId()); + + Notification notification = Notification.builder() + .setTitle(COMMENT_TITLE) + .setBody(request.content()) + .build(); + + if (member.getFcmToken() != null && !member.getFcmToken().isEmpty()) { + Message message = Message.builder() + .setToken(member.getFcmToken()) + .setNotification(notification) + .build(); + + try { + firebaseMessaging.send(message); + + Notice notice = noticeQueryService.findByNoticeId(request.noticeId()); + // 알람 테이블 저장 + Alarm alarm = Alarm.builder() + .type(AlarmType.COMMENT) + .title(COMMENT_TITLE) + .content(request.content()) + .notice(notice) + .build(); + alarmSaveService.save(alarm); + + } catch (FirebaseMessagingException ex) { + log.info("알림 전송에 실패했습니다."); + } + } + } + + public void sendReCommentNotification(SendNotificationRequest request) { + + Member memberRequest = memberUtils.getAccessMember(); + List members = new ArrayList<>(); + + Member noticeMember = findNoticeMember(request.noticeId()); + Member commentMember = findCommentMember(memberRequest.getId()); + members.add(noticeMember); + members.add(commentMember); + + Notification notification = Notification.builder() + .setTitle(COMMENT_TITLE) + .setBody(request.content()) + .build(); + + for (Member m : members) { + if (m.getFcmToken() != null && !m.getFcmToken().isEmpty()) { + Message message = Message.builder() + .setToken(m.getFcmToken()) + .setNotification(notification) + .build(); + + try { + firebaseMessaging.send(message); + + Notice notice = noticeQueryService.findByNoticeId(request.noticeId()); + // 알람 테이블 저장 + Alarm alarm = Alarm.builder() + .type(AlarmType.RECOMMENT) + .title(COMMENT_TITLE) + .content(request.content()) + .notice(notice) + .build(); + alarmSaveService.save(alarm); + + } catch (FirebaseMessagingException ex) { + log.info("알림 전송에 실패했습니다."); + } + } + } + } + + public List getAlarmLists() { + + Member memberRequest = memberUtils.getAccessMember(); + List alarmList = alarmFindService.findAll(); + List alarmViews = alarmFindService.findViews(memberRequest.getId()); + List result = new ArrayList<>(); + + for (Alarm a : alarmList) { + boolean isViewed = false; + + // 조회 여부 확인 + for (AlarmView al : alarmViews) { + if (a.getId().equals(al.getId())) { + isViewed = true; + break; + } + } + + AlarmLists alarmLists = AlarmLists.builder() + .alarmId(a.getId()) + .title(a.getTitle()) + .content(a.getContent()) + .date(a.getCreatedAt()) + .viewYn(isViewed) // viewYn을 조회 여부에 따라 설정 + .build(); + + result.add(alarmLists); + } + + return result; + } + + public void saveAlarmView(Long alarmId) { + + Member memberRequest = memberUtils.getAccessMember(); + Member member = alarmFindService.findMember(memberRequest.getId()); + Alarm alarm = alarmFindService.findAlarmById(alarmId); + + AlarmView alarmView = AlarmView.builder().alarm(alarm).view(ViewType.VIEWED).member(member).build(); + alarmSaveService.saveAlarmView(alarmView); + } + + public void deleteFcmToken() { + + Member memberRequest = memberUtils.getAccessMember(); + alarmSaveService.deleteFcmToken(memberRequest.getId()); + } + + public void deleteAlarm(Long alarmId) { + alarmSaveService.deleteAlarm(alarmId); + } + + private Member findMember(Long memberId) { + return alarmFindService.findMember(memberId); + } + + private List findAllMember() { + return alarmFindService.findAllMember(); + } + + private Member findNoticeMember(Long noticeId) { + return alarmFindService.findNoticeMember(noticeId); + } + + private Member findCommentMember(Long memberId) { + return alarmFindService.findMember(memberId); + } +} diff --git a/core-module/src/main/java/com/koa/coremodule/notice/application/service/NoticeFcmUseCase.java b/core-module/src/main/java/com/koa/coremodule/notice/application/service/NoticeFcmUseCase.java deleted file mode 100644 index 9b3ebff8..00000000 --- a/core-module/src/main/java/com/koa/coremodule/notice/application/service/NoticeFcmUseCase.java +++ /dev/null @@ -1,82 +0,0 @@ -package com.koa.coremodule.notice.application.service; - -import com.google.firebase.messaging.FirebaseMessaging; -import com.google.firebase.messaging.FirebaseMessagingException; -import com.google.firebase.messaging.Message; -import com.google.firebase.messaging.Notification; -import com.koa.commonmodule.exception.BusinessException; -import com.koa.commonmodule.exception.Error; -import com.koa.coremodule.member.domain.entity.Member; -import com.koa.coremodule.member.domain.repository.MemberRepository; -import com.koa.coremodule.member.domain.utils.MemberUtils; -import com.koa.coremodule.notice.domain.repository.NoticeRepository; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -import java.util.List; - -@Slf4j -@Service -@RequiredArgsConstructor -public class NoticeFcmUseCase { - - private final FirebaseMessaging firebaseMessaging; - private final MemberUtils memberUtils; - private final MemberRepository memberRepository; - private final NoticeRepository noticeRepository; - - @Transactional - public void registerFcmToken(String token) { - - Member memberRequest = memberUtils.getAccessMember(); - - Member member = findMember(memberRequest.getId()); - member.updateFcmToken(token); - log.info("회원의 fcm 토큰이 등록되었습니다."); - } - - @Transactional - public void sendNotification(String title, String content) { - - List members = findAllMember(); - - Notification notification = Notification.builder() - .setTitle(title) - .setBody(content) - .build(); - - for (Member m : members) { - if (m.getFcmToken() != null && !m.getFcmToken().isEmpty()) { - Message message = Message.builder() - .setToken(m.getFcmToken()) - .setNotification(notification) - .build(); - - try { - firebaseMessaging.send(message); - } catch (FirebaseMessagingException ex) { - log.info("알림 전송에 실패했습니다."); - } - } - } - } - - @Transactional - public void deleteFcmToken() { - - Member memberRequest = memberUtils.getAccessMember(); - - noticeRepository.deleteToken(memberRequest.getId()); - } - - private Member findMember(Long memberId) { - return memberRepository.findById(memberId) - .orElseThrow(() -> new BusinessException(Error.MEMBER_NOT_FOUND)); - } - - private List findAllMember() { - return memberRepository.findAll(); - } -} From 959b59d8e6506a43c23a625a223f255b4232e14c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 5 Dec 2023 16:59:09 +0900 Subject: [PATCH 2/6] feat: alarm entity --- .../coremodule/fcm/domain/entity/Alarm.java | 33 +++++++++++++++++++ .../fcm/domain/entity/AlarmType.java | 5 +++ .../fcm/domain/entity/AlarmView.java | 31 +++++++++++++++++ 3 files changed, 69 insertions(+) create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/Alarm.java create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmType.java create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmView.java diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/Alarm.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/Alarm.java new file mode 100644 index 00000000..563bf721 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/Alarm.java @@ -0,0 +1,33 @@ +package com.koa.coremodule.fcm.domain.entity; + +import com.koa.commonmodule.domain.BaseEntity; +import com.koa.coremodule.member.domain.entity.Member; +import com.koa.coremodule.notice.domain.entity.Notice; +import jakarta.persistence.*; +import lombok.*; + +@Builder +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Entity +public class Alarm extends BaseEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "alarm_id") + private Long id; + + private AlarmType type; + private String title; + private String content; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "notice_id") + private Notice notice; + +} diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmType.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmType.java new file mode 100644 index 00000000..a9253d7a --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmType.java @@ -0,0 +1,5 @@ +package com.koa.coremodule.fcm.domain.entity; + +public enum AlarmType { + NOTICE, COMMENT, RECOMMENT +} diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmView.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmView.java new file mode 100644 index 00000000..cd67d041 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/entity/AlarmView.java @@ -0,0 +1,31 @@ +package com.koa.coremodule.fcm.domain.entity; + +import com.koa.coremodule.member.domain.entity.Member; +import com.koa.coremodule.notice.domain.entity.ViewType; +import jakarta.persistence.*; +import lombok.*; + +@Builder +@Getter +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@AllArgsConstructor +@Entity +public class AlarmView { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + @Column(name = "alarm_view_id") + private Long id; + + @Enumerated(EnumType.STRING) + private ViewType view; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member member; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "alarm_id") + private Alarm alarm; + +} From 46bd6d7a5864411990ffd1d0565fef73ed2b9a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 5 Dec 2023 16:59:33 +0900 Subject: [PATCH 3/6] feat: dto & error --- .../com/koa/commonmodule/exception/Error.java | 3 +++ .../fcm/application/dto/AlarmLists.java | 20 +++++++++++++++++++ .../dto/fcm/SendNotificationRequest.java | 2 +- 3 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/application/dto/AlarmLists.java diff --git a/common-module/src/main/java/com/koa/commonmodule/exception/Error.java b/common-module/src/main/java/com/koa/commonmodule/exception/Error.java index 14c2496a..708631ba 100644 --- a/common-module/src/main/java/com/koa/commonmodule/exception/Error.java +++ b/common-module/src/main/java/com/koa/commonmodule/exception/Error.java @@ -20,6 +20,9 @@ public enum Error { VOTE_ITEM_NOT_FOUND("투표 항목이 존재하지 않습니다.", 400), VOTE_ITEM_RECORD_NOT_FOUND("투표 항목 참여자가 존재하지 않습니다.", 400), + // ALARM + ALARM_NOT_FOUND("알림이 존재하지 않습니다.", 400), + // MEMBER MEMBER_NOT_FOUND("사용자를 찾을 수 없습니다.", 2000), WRONG_PASSWORD("비밀번호가 틀렸습니다.", 2001), diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/application/dto/AlarmLists.java b/core-module/src/main/java/com/koa/coremodule/fcm/application/dto/AlarmLists.java new file mode 100644 index 00000000..75f82cce --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/application/dto/AlarmLists.java @@ -0,0 +1,20 @@ +package com.koa.coremodule.fcm.application.dto; + +import lombok.Builder; +import lombok.Data; +import lombok.extern.jackson.Jacksonized; + +import java.time.LocalDateTime; + +@Data +@Builder +@Jacksonized +public class AlarmLists { + + private Long alarmId; + private String title; + private String content; + private LocalDateTime date; + private Boolean viewYn; + +} diff --git a/core-module/src/main/java/com/koa/coremodule/notice/application/dto/fcm/SendNotificationRequest.java b/core-module/src/main/java/com/koa/coremodule/notice/application/dto/fcm/SendNotificationRequest.java index fc8365a8..6afbf42c 100644 --- a/core-module/src/main/java/com/koa/coremodule/notice/application/dto/fcm/SendNotificationRequest.java +++ b/core-module/src/main/java/com/koa/coremodule/notice/application/dto/fcm/SendNotificationRequest.java @@ -1,5 +1,5 @@ package com.koa.coremodule.notice.application.dto.fcm; -public record SendNotificationRequest(String title, String content) { +public record SendNotificationRequest(String content, Long noticeId) { } From 7a86aa8b82ea28f786ea38f9f02636d6c520448f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 5 Dec 2023 16:59:59 +0900 Subject: [PATCH 4/6] feat: notice & alarm repository --- .../fcm/domain/repository/AlarmRepository.java | 10 ++++++++++ .../fcm/domain/repository/AlarmViewRepository.java | 14 ++++++++++++++ .../notice/domain/repository/NoticeRepository.java | 3 +++ 3 files changed, 27 insertions(+) create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmRepository.java create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmViewRepository.java diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmRepository.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmRepository.java new file mode 100644 index 00000000..498ba3ba --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmRepository.java @@ -0,0 +1,10 @@ +package com.koa.coremodule.fcm.domain.repository; + +import com.koa.coremodule.fcm.domain.entity.Alarm; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface AlarmRepository extends JpaRepository { + + void deleteAlarmById(Long alarmId); + +} diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmViewRepository.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmViewRepository.java new file mode 100644 index 00000000..93c26521 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/repository/AlarmViewRepository.java @@ -0,0 +1,14 @@ +package com.koa.coremodule.fcm.domain.repository; + +import com.koa.coremodule.fcm.domain.entity.AlarmView; +import org.springframework.data.jpa.repository.JpaRepository; + +import java.util.List; + +public interface AlarmViewRepository extends JpaRepository { + + List findAllByMemberId(Long memberId); + + void deleteAlarmViewByAlarmId(Long alarmId); + +} diff --git a/core-module/src/main/java/com/koa/coremodule/notice/domain/repository/NoticeRepository.java b/core-module/src/main/java/com/koa/coremodule/notice/domain/repository/NoticeRepository.java index 2e6affdb..81f37a07 100644 --- a/core-module/src/main/java/com/koa/coremodule/notice/domain/repository/NoticeRepository.java +++ b/core-module/src/main/java/com/koa/coremodule/notice/domain/repository/NoticeRepository.java @@ -46,4 +46,7 @@ public interface NoticeRepository extends JpaRepository, NoticeDyn @Query("update Member m set m.fcmToken = null where m.id = :id") void deleteToken(Long id); + @Query("select n.member.id from Notice n where n.id = :noticeId") + Long findMemberIdByNoticeId(Long noticeId); + } From d29e3e4beb0fbb47d7a8076b2c1b50e40fa2271b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Tue, 5 Dec 2023 17:00:10 +0900 Subject: [PATCH 5/6] feat: alarm query service --- .../fcm/domain/service/AlarmFindService.java | 55 +++++++++++++++++++ .../fcm/domain/service/AlarmSaveService.java | 39 +++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java create mode 100644 core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmSaveService.java diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java new file mode 100644 index 00000000..27cd02a7 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java @@ -0,0 +1,55 @@ +package com.koa.coremodule.fcm.domain.service; + +import com.koa.commonmodule.exception.BusinessException; +import com.koa.commonmodule.exception.Error; +import com.koa.coremodule.fcm.domain.entity.Alarm; +import com.koa.coremodule.fcm.domain.entity.AlarmView; +import com.koa.coremodule.fcm.domain.repository.AlarmRepository; +import com.koa.coremodule.fcm.domain.repository.AlarmViewRepository; +import com.koa.coremodule.member.domain.entity.Member; +import com.koa.coremodule.member.domain.repository.MemberRepository; +import com.koa.coremodule.notice.domain.repository.NoticeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +import java.util.List; + +@Service +@RequiredArgsConstructor +@Transactional +public class AlarmFindService { + + private final MemberRepository memberRepository; + private final NoticeRepository noticeRepository; + private final AlarmRepository alarmRepository; + private final AlarmViewRepository alarmViewRepository; + + public List findAll() { + return alarmRepository.findAll(); + } + + public Alarm findAlarmById(Long alarmId) { + return alarmRepository.findById(alarmId) + .orElseThrow(() -> new BusinessException(Error.ALARM_NOT_FOUND)); + } + + public List findViews(Long memberId) { + return alarmViewRepository.findAllByMemberId(memberId); + } + + public Member findMember(Long memberId) { + return memberRepository.findById(memberId) + .orElseThrow(() -> new BusinessException(Error.MEMBER_NOT_FOUND)); + } + + public Member findNoticeMember(Long noticeId) { + Long memberId = noticeRepository.findMemberIdByNoticeId(noticeId); + return findMember(memberId); + } + + public List findAllMember() { + return memberRepository.findAll(); + } + +} diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmSaveService.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmSaveService.java new file mode 100644 index 00000000..3055cf11 --- /dev/null +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmSaveService.java @@ -0,0 +1,39 @@ +package com.koa.coremodule.fcm.domain.service; + +import com.koa.coremodule.fcm.domain.entity.Alarm; +import com.koa.coremodule.fcm.domain.entity.AlarmView; +import com.koa.coremodule.fcm.domain.repository.AlarmRepository; +import com.koa.coremodule.fcm.domain.repository.AlarmViewRepository; +import com.koa.coremodule.notice.domain.repository.NoticeRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +@Transactional +public class AlarmSaveService { + + private final AlarmRepository alarmRepository; + private final AlarmViewRepository alarmViewRepository; + private final NoticeRepository noticeRepository; + + public Alarm save(Alarm alarm) { + return alarmRepository.save(alarm); + } + + public void saveAlarmView(AlarmView alarmView) { + alarmViewRepository.save(alarmView); + } + + public void deleteFcmToken(Long id) { + noticeRepository.deleteToken(id); + } + + public void deleteAlarm(Long alarmId) { + + alarmViewRepository.deleteAlarmViewByAlarmId(alarmId); + alarmRepository.deleteAlarmById(alarmId); + } + +} From e66d19d490fc5738b7b7dc81b74c97ac47e269a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=95=88=EC=A0=95=ED=9B=84?= Date: Wed, 6 Dec 2023 15:20:46 +0900 Subject: [PATCH 6/6] fix: service naming --- .../fcm/application/service/AlarmUseCase.java | 20 +++++++++---------- ...indService.java => AlarmQueryService.java} | 2 +- 2 files changed, 11 insertions(+), 11 deletions(-) rename core-module/src/main/java/com/koa/coremodule/fcm/domain/service/{AlarmFindService.java => AlarmQueryService.java} (98%) diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java b/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java index 8faf5b93..8008a992 100644 --- a/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java +++ b/core-module/src/main/java/com/koa/coremodule/fcm/application/service/AlarmUseCase.java @@ -8,7 +8,7 @@ import com.koa.coremodule.fcm.domain.entity.Alarm; import com.koa.coremodule.fcm.domain.entity.AlarmType; import com.koa.coremodule.fcm.domain.entity.AlarmView; -import com.koa.coremodule.fcm.domain.service.AlarmFindService; +import com.koa.coremodule.fcm.domain.service.AlarmQueryService; import com.koa.coremodule.fcm.domain.service.AlarmSaveService; import com.koa.coremodule.member.domain.entity.Member; import com.koa.coremodule.member.domain.utils.MemberUtils; @@ -33,7 +33,7 @@ public class AlarmUseCase { private final FirebaseMessaging firebaseMessaging; private final MemberUtils memberUtils; private final AlarmSaveService alarmSaveService; - private final AlarmFindService alarmFindService; + private final AlarmQueryService alarmQueryService; private final NoticeQueryService noticeQueryService; private final static String NOTICE_TITLE = "새로운 공지를 확인하세요"; @@ -163,8 +163,8 @@ public void sendReCommentNotification(SendNotificationRequest request) { public List getAlarmLists() { Member memberRequest = memberUtils.getAccessMember(); - List alarmList = alarmFindService.findAll(); - List alarmViews = alarmFindService.findViews(memberRequest.getId()); + List alarmList = alarmQueryService.findAll(); + List alarmViews = alarmQueryService.findViews(memberRequest.getId()); List result = new ArrayList<>(); for (Alarm a : alarmList) { @@ -195,8 +195,8 @@ public List getAlarmLists() { public void saveAlarmView(Long alarmId) { Member memberRequest = memberUtils.getAccessMember(); - Member member = alarmFindService.findMember(memberRequest.getId()); - Alarm alarm = alarmFindService.findAlarmById(alarmId); + Member member = alarmQueryService.findMember(memberRequest.getId()); + Alarm alarm = alarmQueryService.findAlarmById(alarmId); AlarmView alarmView = AlarmView.builder().alarm(alarm).view(ViewType.VIEWED).member(member).build(); alarmSaveService.saveAlarmView(alarmView); @@ -213,18 +213,18 @@ public void deleteAlarm(Long alarmId) { } private Member findMember(Long memberId) { - return alarmFindService.findMember(memberId); + return alarmQueryService.findMember(memberId); } private List findAllMember() { - return alarmFindService.findAllMember(); + return alarmQueryService.findAllMember(); } private Member findNoticeMember(Long noticeId) { - return alarmFindService.findNoticeMember(noticeId); + return alarmQueryService.findNoticeMember(noticeId); } private Member findCommentMember(Long memberId) { - return alarmFindService.findMember(memberId); + return alarmQueryService.findMember(memberId); } } diff --git a/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmQueryService.java similarity index 98% rename from core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java rename to core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmQueryService.java index 27cd02a7..66883140 100644 --- a/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmFindService.java +++ b/core-module/src/main/java/com/koa/coremodule/fcm/domain/service/AlarmQueryService.java @@ -18,7 +18,7 @@ @Service @RequiredArgsConstructor @Transactional -public class AlarmFindService { +public class AlarmQueryService { private final MemberRepository memberRepository; private final NoticeRepository noticeRepository;