diff --git a/src/main/java/zipdabang/server/converter/MemberConverter.java b/src/main/java/zipdabang/server/converter/MemberConverter.java index b8f1c56..10584cf 100644 --- a/src/main/java/zipdabang/server/converter/MemberConverter.java +++ b/src/main/java/zipdabang/server/converter/MemberConverter.java @@ -9,9 +9,11 @@ import zipdabang.server.aws.s3.AmazonS3Manager; import zipdabang.server.apiPayload.exception.handler.MemberException; import zipdabang.server.domain.Category; +import zipdabang.server.domain.enums.AlarmType; import zipdabang.server.domain.enums.GenderType; import zipdabang.server.domain.enums.SocialType; import zipdabang.server.domain.etc.Uuid; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.member.*; import zipdabang.server.domain.member.Deregister; import zipdabang.server.domain.member.Terms; @@ -419,6 +421,47 @@ public static MemberResponseDto.MyZipdabangDto toMyZipdabangDto(Member member, b .followingCount(staticMemberService.getFollowingCount(member)) .build(); } + + public static MemberResponseDto.PushAlarmDto toPushAlarmDto(PushAlarm pushAlarm){ + + AlarmType name = pushAlarm.getAlarmCategory().getName(); + Long targetPK = null; + switch (name){ + case USER: + targetPK = pushAlarm.getTargetMember().getMemberId(); + break; + case RECIPE: + targetPK = pushAlarm.getTargetRecipe().getId(); + break; + case MYPAGE: + break; + case NOTIFICATION: + targetPK = pushAlarm.getTargetNotification().getId(); + break; + } + + return MemberResponseDto.PushAlarmDto.builder() + .title(pushAlarm.getTitle()) + .body(pushAlarm.getBody()) + .isConfirmed(pushAlarm.getIsConfirmed()) + .alarmType(name) + .targetPK(targetPK) + .build(); + } + + public static MemberResponseDto.PushAlarmListDto toPushAlarmListDto(Page pushAlarmPage){ + List pushAlarmDtoList = pushAlarmPage.stream() + .map(MemberConverter::toPushAlarmDto).collect(Collectors.toList()); + + return MemberResponseDto.PushAlarmListDto.builder() + .pushAlarmDtoList(pushAlarmDtoList) + .isLast(pushAlarmPage.isLast()) + .isFirst(pushAlarmPage.isFirst()) + .totalPage(pushAlarmPage.getTotalPages()) + .totalElements(pushAlarmPage.getTotalElements()) + .currentPageElements(pushAlarmDtoList.size()) + .build(); + } } diff --git a/src/main/java/zipdabang/server/domain/enums/AlarmType.java b/src/main/java/zipdabang/server/domain/enums/AlarmType.java new file mode 100644 index 0000000..cbfe522 --- /dev/null +++ b/src/main/java/zipdabang/server/domain/enums/AlarmType.java @@ -0,0 +1,6 @@ +package zipdabang.server.domain.enums; + +public enum AlarmType { + + USER, RECIPE,MYPAGE,NOTIFICATION +} diff --git a/src/main/java/zipdabang/server/domain/inform/AlarmCategory.java b/src/main/java/zipdabang/server/domain/inform/AlarmCategory.java new file mode 100644 index 0000000..3f4ca74 --- /dev/null +++ b/src/main/java/zipdabang/server/domain/inform/AlarmCategory.java @@ -0,0 +1,26 @@ +package zipdabang.server.domain.inform; + +import lombok.*; +import org.hibernate.annotations.DynamicInsert; +import org.hibernate.annotations.DynamicUpdate; +import zipdabang.server.domain.enums.AlarmType; + +import javax.persistence.*; + +@Entity +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@DynamicInsert +@DynamicUpdate +public class AlarmCategory { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(unique = true) + @Enumerated(EnumType.STRING) + private AlarmType name; +} diff --git a/src/main/java/zipdabang/server/domain/inform/PushAlarm.java b/src/main/java/zipdabang/server/domain/inform/PushAlarm.java new file mode 100644 index 0000000..b401372 --- /dev/null +++ b/src/main/java/zipdabang/server/domain/inform/PushAlarm.java @@ -0,0 +1,56 @@ +package zipdabang.server.domain.inform; + +import lombok.*; +import org.hibernate.annotations.DynamicInsert; +import org.hibernate.annotations.DynamicUpdate; +import zipdabang.server.domain.common.BaseEntity; +import zipdabang.server.domain.member.Member; +import zipdabang.server.domain.recipe.Recipe; + +import javax.persistence.*; + +@Entity +@Getter +@Builder +@AllArgsConstructor(access = AccessLevel.PROTECTED) +@NoArgsConstructor(access = AccessLevel.PROTECTED) +@DynamicInsert +@DynamicUpdate +public class PushAlarm extends BaseEntity { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + + private String body; + + private Boolean isConfirmed; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "owner_id") + private Member ownerMember; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "member_id") + private Member targetMember; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "recipe_id") + private Recipe targetRecipe; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "notification_id") + private Notification targetNotification; + + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "category_id") + private AlarmCategory alarmCategory; + + public void setMember(Member member){ + if (this.ownerMember != null) + this.ownerMember.getPushAlarmList().remove(this); + this.ownerMember = member; + member.getPushAlarmList().add(this); + } +} diff --git a/src/main/java/zipdabang/server/domain/member/Member.java b/src/main/java/zipdabang/server/domain/member/Member.java index d3af241..638d3db 100644 --- a/src/main/java/zipdabang/server/domain/member/Member.java +++ b/src/main/java/zipdabang/server/domain/member/Member.java @@ -6,6 +6,7 @@ import org.hibernate.annotations.DynamicInsert; import org.hibernate.annotations.DynamicUpdate; import zipdabang.server.domain.enums.SocialType; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.inform.Question; import zipdabang.server.domain.recipe.Recipe; import zipdabang.server.domain.recipe.Scrap; @@ -115,6 +116,9 @@ public class Member extends BaseEntity { @OneToMany(mappedBy = "follower", cascade = CascadeType.ALL) private List myFollowingList; + @OneToMany(mappedBy = "ownerMember", cascade = CascadeType.ALL) + private List pushAlarmList; + public void setProfileUrl(String profileUrl) { this.profileUrl = profileUrl; } diff --git a/src/main/java/zipdabang/server/firebase/fcm/dto/FcmAOSMessage.java b/src/main/java/zipdabang/server/firebase/fcm/dto/FcmAOSMessage.java index aaf5492..ff448ff 100644 --- a/src/main/java/zipdabang/server/firebase/fcm/dto/FcmAOSMessage.java +++ b/src/main/java/zipdabang/server/firebase/fcm/dto/FcmAOSMessage.java @@ -30,6 +30,6 @@ public static class Data{ private String body; private String targetView; private String targetPK; - private String targetNotification; + private String targetNotificationPK; } } diff --git a/src/main/java/zipdabang/server/firebase/fcm/service/FirebaseServiceImpl.java b/src/main/java/zipdabang/server/firebase/fcm/service/FirebaseServiceImpl.java index 87ab169..0cb92b2 100644 --- a/src/main/java/zipdabang/server/firebase/fcm/service/FirebaseServiceImpl.java +++ b/src/main/java/zipdabang/server/firebase/fcm/service/FirebaseServiceImpl.java @@ -53,7 +53,7 @@ private String makeAOSMessage(String targeToken, String title, String body, Stri .title(title) .body(body) .targetView(targetView) - .targetNotification(targetNotification) + .targetNotificationPK(targetNotification) .targetPK(targetPK).build() ). build() diff --git a/src/main/java/zipdabang/server/repository/AlarmRepository/AlarmCategoryRepository.java b/src/main/java/zipdabang/server/repository/AlarmRepository/AlarmCategoryRepository.java new file mode 100644 index 0000000..7f241d5 --- /dev/null +++ b/src/main/java/zipdabang/server/repository/AlarmRepository/AlarmCategoryRepository.java @@ -0,0 +1,12 @@ +package zipdabang.server.repository.AlarmRepository; + +import org.springframework.data.jpa.repository.JpaRepository; +import zipdabang.server.domain.enums.AlarmType; +import zipdabang.server.domain.inform.AlarmCategory; + +import java.util.Optional; + +public interface AlarmCategoryRepository extends JpaRepository { + + Optional findByName(AlarmType name); +} diff --git a/src/main/java/zipdabang/server/repository/AlarmRepository/PushAlarmRepository.java b/src/main/java/zipdabang/server/repository/AlarmRepository/PushAlarmRepository.java new file mode 100644 index 0000000..9c3e058 --- /dev/null +++ b/src/main/java/zipdabang/server/repository/AlarmRepository/PushAlarmRepository.java @@ -0,0 +1,12 @@ +package zipdabang.server.repository.AlarmRepository; + +import org.springframework.data.domain.Page; +import org.springframework.data.domain.PageRequest; +import org.springframework.data.jpa.repository.JpaRepository; +import zipdabang.server.domain.inform.PushAlarm; +import zipdabang.server.domain.member.Member; + +public interface PushAlarmRepository extends JpaRepository { + + Page findByOwnerMember(Member member, PageRequest pageRequest); +} diff --git a/src/main/java/zipdabang/server/service/MemberService.java b/src/main/java/zipdabang/server/service/MemberService.java index 6c9d794..960a123 100644 --- a/src/main/java/zipdabang/server/service/MemberService.java +++ b/src/main/java/zipdabang/server/service/MemberService.java @@ -3,6 +3,7 @@ import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import zipdabang.server.domain.Category; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.member.Follow; import zipdabang.server.domain.member.Inquery; import zipdabang.server.domain.enums.DeregisterType; @@ -72,4 +73,6 @@ public interface MemberService { public void updateProfileDefault(Member member); Boolean checkFollowing(Member loginMember, Member targetMember); public MemberResponseDto.MyZipdabangDto getMyZipdabang(Member member, Long targetId); - } + + Page getPushAlarms(Member member, Integer page); +} diff --git a/src/main/java/zipdabang/server/service/serviceImpl/MemberServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/MemberServiceImpl.java index 9e38e72..e27dd83 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/MemberServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/MemberServiceImpl.java @@ -19,12 +19,14 @@ import zipdabang.server.domain.enums.DeregisterType; import zipdabang.server.domain.enums.SocialType; import zipdabang.server.domain.etc.Uuid; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.member.Member; import zipdabang.server.domain.member.MemberPreferCategory; import zipdabang.server.domain.member.Terms; import zipdabang.server.domain.member.TermsAgree; import zipdabang.server.domain.member.*; import zipdabang.server.redis.domain.RefreshToken; +import zipdabang.server.repository.AlarmRepository.PushAlarmRepository; import zipdabang.server.repository.TermsAgreeRepository; import zipdabang.server.repository.TermsRepository; import zipdabang.server.redis.service.RedisService; @@ -79,6 +81,9 @@ public class MemberServiceImpl implements MemberService { private final DeregisterReasonRepository deregisterReasonRepository; private final BlockedMemberRepository blockedMemberRepository; private final FollowRepository followRepository; + + private final PushAlarmRepository pushAlarmRepository; + private static String defaultProfileImage; @Value("${paging.size}") @@ -488,10 +493,16 @@ else if(blockedMemberRepository.existsByOwnerAndBlocked(member,target)){ return MemberConverter.toMyZipdabangDto(target, checkSelf, isFollowing, memberPreferCategoryDto); } -} - - + @Override + public Page getPushAlarms(Member member, Integer page) { + page -= 1; + Page pushAlarms = pushAlarmRepository.findByOwnerMember(member, PageRequest.of(page, pageSize, Sort.by(Sort.Direction.DESC, "createdAt"))); + if(pushAlarms.getTotalPages() <= page) + throw new MemberException(CommonStatus.OVER_PAGE_INDEX_ERROR); + return pushAlarms; + } +} \ No newline at end of file diff --git a/src/main/java/zipdabang/server/service/serviceImpl/RootServiceImpl.java b/src/main/java/zipdabang/server/service/serviceImpl/RootServiceImpl.java index 1891253..20a99c5 100644 --- a/src/main/java/zipdabang/server/service/serviceImpl/RootServiceImpl.java +++ b/src/main/java/zipdabang/server/service/serviceImpl/RootServiceImpl.java @@ -9,13 +9,20 @@ import zipdabang.server.apiPayload.exception.handler.RootException; import zipdabang.server.domain.Category; import zipdabang.server.domain.Report; +import zipdabang.server.domain.enums.AlarmType; +import zipdabang.server.domain.inform.AlarmCategory; import zipdabang.server.domain.inform.Notification; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.member.Member; +import zipdabang.server.domain.recipe.Recipe; import zipdabang.server.firebase.fcm.service.FirebaseService; +import zipdabang.server.repository.AlarmRepository.AlarmCategoryRepository; import zipdabang.server.repository.CategoryRepository; import zipdabang.server.repository.NotificationRepository; +import zipdabang.server.repository.AlarmRepository.PushAlarmRepository; import zipdabang.server.repository.ReportRepository; import zipdabang.server.repository.memberRepositories.MemberRepository; +import zipdabang.server.repository.recipeRepositories.RecipeRepository; import zipdabang.server.service.RootService; import java.io.IOException; @@ -39,6 +46,12 @@ public class RootServiceImpl implements RootService { private final FirebaseService firebaseService; + private final PushAlarmRepository pushAlarmRepository; + + private final AlarmCategoryRepository alarmCategoryRepository; + + private final RecipeRepository recipeRepository; + @Override public List getAllCategories() { return categoryRepository.findAll(); @@ -93,13 +106,28 @@ public List getAllReports() { } @Override + @Transactional public void testFCMService(String fcmToken) throws IOException { String title = "집다방 FCM 테스트"; String body = "되나? 되나? 되나? 되나?"; - String targetView = "레시피"; - String targetPK = "1"; - String targetNotification = "2"; - firebaseService.sendMessageTo(fcmToken,title,body,targetView,targetPK,targetNotification); + String targetView = AlarmType.RECIPE.toString(); + String targetPK = "120"; + + Recipe recipe = recipeRepository.findById(120L).get(); + + PushAlarm pushAlarm = pushAlarmRepository.save(PushAlarm.builder() + .title(title) + .body(body) + .isConfirmed(false) + .targetRecipe(recipe) + .alarmCategory(alarmCategoryRepository.findByName(AlarmType.RECIPE).get()) + .build()); + + + pushAlarm.setMember(memberRepository.findById(108L).get()); + + + firebaseService.sendMessageTo(fcmToken,title,body,targetView,targetPK,pushAlarm.getId().toString()); } } diff --git a/src/main/java/zipdabang/server/web/controller/MemberRestController.java b/src/main/java/zipdabang/server/web/controller/MemberRestController.java index 900850d..9c8dab0 100644 --- a/src/main/java/zipdabang/server/web/controller/MemberRestController.java +++ b/src/main/java/zipdabang/server/web/controller/MemberRestController.java @@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.checkerframework.checker.units.qual.A; import org.springframework.data.domain.Page; import org.springframework.http.MediaType; import org.springframework.validation.annotation.Validated; @@ -26,6 +27,7 @@ import zipdabang.server.apiPayload.exception.handler.MemberException; import zipdabang.server.converter.MemberConverter; import zipdabang.server.domain.Category; +import zipdabang.server.domain.inform.PushAlarm; import zipdabang.server.domain.member.Follow; import zipdabang.server.domain.member.Inquery; import zipdabang.server.domain.member.Member; @@ -510,5 +512,14 @@ public ResponseDto getMyZipdabang(@CheckTempMe } + @GetMapping("/members/push-alarms") + @Parameters({ + @Parameter(name = "member", hidden = true) + }) + public ResponseDto showPushAlarms(@CheckTempMember @AuthMember Member member, @CheckPage @RequestParam(name = "page") Integer page){ + Page pushAlarms = memberService.getPushAlarms(member, page); + return ResponseDto.of(MemberConverter.toPushAlarmListDto(pushAlarms)); + } + } diff --git a/src/main/java/zipdabang/server/web/dto/responseDto/MemberResponseDto.java b/src/main/java/zipdabang/server/web/dto/responseDto/MemberResponseDto.java index 8727450..75a5380 100644 --- a/src/main/java/zipdabang/server/web/dto/responseDto/MemberResponseDto.java +++ b/src/main/java/zipdabang/server/web/dto/responseDto/MemberResponseDto.java @@ -1,6 +1,7 @@ package zipdabang.server.web.dto.responseDto; import lombok.*; +import zipdabang.server.domain.enums.AlarmType; import zipdabang.server.domain.enums.GenderType; @@ -285,4 +286,29 @@ public static class MyZipdabangDto{ Long followingCount; } + + @Builder + @Getter + @AllArgsConstructor(access = AccessLevel.PROTECTED) + @NoArgsConstructor(access = AccessLevel.PROTECTED) + public static class PushAlarmDto{ + String title; + String body; + AlarmType alarmType; + Long targetPK; + Boolean isConfirmed; + } + + @Builder + @Getter + @AllArgsConstructor(access = AccessLevel.PROTECTED) + @NoArgsConstructor(access = AccessLevel.PROTECTED) + public static class PushAlarmListDto{ + List pushAlarmDtoList; + Long totalElements; + Integer currentPageElements; + Integer totalPage; + Boolean isFirst; + Boolean isLast; + } }