Skip to content

Commit

Permalink
✨ Feat : 알림 모아보기 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
CYY1007 committed Oct 11, 2023
1 parent e662cc0 commit 9098b25
Show file tree
Hide file tree
Showing 15 changed files with 252 additions and 14 deletions.
43 changes: 43 additions & 0 deletions src/main/java/zipdabang/server/converter/MemberConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PushAlarm> pushAlarmPage){
List<MemberResponseDto.PushAlarmDto> 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();
}
}


6 changes: 6 additions & 0 deletions src/main/java/zipdabang/server/domain/enums/AlarmType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package zipdabang.server.domain.enums;

public enum AlarmType {

USER, RECIPE,MYPAGE,NOTIFICATION
}
26 changes: 26 additions & 0 deletions src/main/java/zipdabang/server/domain/inform/AlarmCategory.java
Original file line number Diff line number Diff line change
@@ -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;
}
56 changes: 56 additions & 0 deletions src/main/java/zipdabang/server/domain/inform/PushAlarm.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
4 changes: 4 additions & 0 deletions src/main/java/zipdabang/server/domain/member/Member.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -115,6 +116,9 @@ public class Member extends BaseEntity {
@OneToMany(mappedBy = "follower", cascade = CascadeType.ALL)
private List<Follow> myFollowingList;

@OneToMany(mappedBy = "ownerMember", cascade = CascadeType.ALL)
private List<PushAlarm> pushAlarmList;

public void setProfileUrl(String profileUrl) {
this.profileUrl = profileUrl;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public static class Data{
private String body;
private String targetView;
private String targetPK;
private String targetNotification;
private String targetNotificationPK;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AlarmCategory, Long> {

Optional<AlarmCategory> findByName(AlarmType name);
}
Original file line number Diff line number Diff line change
@@ -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<PushAlarm, Long> {

Page<PushAlarm> findByOwnerMember(Member member, PageRequest pageRequest);
}
5 changes: 4 additions & 1 deletion src/main/java/zipdabang/server/service/MemberService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<PushAlarm> getPushAlarms(Member member, Integer page);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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}")
Expand Down Expand Up @@ -488,10 +493,16 @@ else if(blockedMemberRepository.existsByOwnerAndBlocked(member,target)){
return MemberConverter.toMyZipdabangDto(target, checkSelf, isFollowing, memberPreferCategoryDto);

}
}



@Override
public Page<PushAlarm> getPushAlarms(Member member, Integer page) {
page -= 1;

Page<PushAlarm> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<Category> getAllCategories() {
return categoryRepository.findAll();
Expand Down Expand Up @@ -93,13 +106,28 @@ public List<Report> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -510,5 +512,14 @@ public ResponseDto<MemberResponseDto.MyZipdabangDto> getMyZipdabang(@CheckTempMe

}

@GetMapping("/members/push-alarms")
@Parameters({
@Parameter(name = "member", hidden = true)
})
public ResponseDto<MemberResponseDto.PushAlarmListDto> showPushAlarms(@CheckTempMember @AuthMember Member member, @CheckPage @RequestParam(name = "page") Integer page){
Page<PushAlarm> pushAlarms = memberService.getPushAlarms(member, page);
return ResponseDto.of(MemberConverter.toPushAlarmListDto(pushAlarms));
}

}

Loading

0 comments on commit 9098b25

Please sign in to comment.