Skip to content

Commit

Permalink
Merge pull request #121 from hellokitty-coding-club/feature/#107-aler…
Browse files Browse the repository at this point in the history
…t-fcm

알림센터 조회 API
  • Loading branch information
ray-yhc authored Oct 27, 2023
2 parents 27898dc + a7306e3 commit e1ab7b0
Show file tree
Hide file tree
Showing 14 changed files with 358 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import swm.hkcc.LGTM.app.global.constant.ResponseCode;
import swm.hkcc.LGTM.app.global.dto.ApiDataResponse;
import swm.hkcc.LGTM.app.global.exception.GeneralException;
import swm.hkcc.LGTM.app.global.notification.service.NotificationService;
import swm.hkcc.LGTM.app.modules.notification.service.NotificationService;

import java.util.Optional;

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package swm.hkcc.LGTM.app.modules.notification.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import swm.hkcc.LGTM.app.global.dto.ApiDataResponse;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.member.domain.custom.CustomUserDetails;
import swm.hkcc.LGTM.app.modules.notification.dto.NotificationDTO;
import swm.hkcc.LGTM.app.modules.notification.service.NotificationCenterService;

import java.util.List;

@RestController
@RequestMapping("/v1/notification")
@RequiredArgsConstructor
public class NotificationCenterController {
private final NotificationCenterService notificationCenterService;

@GetMapping
public ApiDataResponse<List<NotificationDTO>> getNotification(
@AuthenticationPrincipal CustomUserDetails customUserDetails
) {
Member member = customUserDetails.getMember();
return ApiDataResponse.of(notificationCenterService.getNotification(member));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package swm.hkcc.LGTM.app.modules.notification.domain;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import swm.hkcc.LGTM.app.global.entity.BaseEntity;
import swm.hkcc.LGTM.app.modules.member.domain.Member;

import java.io.Serializable;
import java.util.Map;

@Getter
@Setter
@Builder
@ToString
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Notification extends BaseEntity implements Serializable {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "notification_id")
private Long notificationId;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "member_id")
private Member member;

private String title;
private String body;

@ColumnDefault("false")
private boolean isRead;

public static Notification from(
Member member,
Map<String, String> data
) {
return Notification.builder()
.title(data.getOrDefault("title", ""))
.body(data.getOrDefault("body", ""))
.member(member)
.isRead(false)
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package swm.hkcc.LGTM.app.modules.notification.dto;

import jakarta.persistence.*;
import lombok.*;
import org.hibernate.annotations.ColumnDefault;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.notification.domain.Notification;

import java.util.Map;

@Data
@AllArgsConstructor
@Builder
public class NotificationDTO {
private Long notificationId;
private String title;
private String body;
private Boolean isRead;

public static NotificationDTO from(Notification notification) {
return NotificationDTO.builder()
.notificationId(notification.getNotificationId())
.title(notification.getTitle())
.body(notification.getBody())
.isRead(notification.isRead())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package swm.hkcc.LGTM.app.modules.notification.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.notification.domain.Notification;

import java.util.List;

public interface NotificationRepository extends JpaRepository<Notification, Long> {
List<Notification> findAllByMember(Member member);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package swm.hkcc.LGTM.app.modules.notification.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.notification.domain.Notification;
import swm.hkcc.LGTM.app.modules.notification.dto.NotificationDTO;
import swm.hkcc.LGTM.app.modules.notification.repository.NotificationRepository;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
@RequiredArgsConstructor
@Slf4j
public class NotificationCenterService {
private final NotificationRepository notificationRepository;

@Transactional
public List<NotificationDTO> getNotification(Member member) {
List<Notification> notifications = notificationRepository.findAllByMember(member);
List<Notification> unreadNotifications = new ArrayList<>();
List<NotificationDTO> notificationDTOList = new ArrayList<>();
notifications.stream()
.forEach(notification -> {
notificationDTOList.add(NotificationDTO.from(notification));
if (!notification.isRead()) {
notification.setRead(true);
unreadNotifications.add(notification);
}
});

if (!unreadNotifications.isEmpty())
notificationRepository.saveAll(unreadNotifications);

return notificationDTOList;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swm.hkcc.LGTM.app.global.notification.service;
package swm.hkcc.LGTM.app.modules.notification.service;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swm.hkcc.LGTM.app.global.notification.service;
package swm.hkcc.LGTM.app.modules.notification.service;

import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException;
Expand All @@ -8,10 +8,13 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Profile;
import org.springframework.stereotype.Service;
import swm.hkcc.LGTM.app.modules.notification.domain.Notification;
import swm.hkcc.LGTM.app.modules.notification.repository.NotificationRepository;
import swm.hkcc.LGTM.app.modules.member.domain.Member;
import swm.hkcc.LGTM.app.modules.member.exception.NotExistMember;
import swm.hkcc.LGTM.app.modules.member.repository.MemberRepository;

import java.util.List;
import java.util.Map;

@RequiredArgsConstructor
Expand All @@ -21,6 +24,7 @@
public class NotificationServiceImpl implements NotificationService {
private final FirebaseMessaging firebaseMessaging;
private final MemberRepository memberRepository;
private final NotificationRepository notificationRepository;

@Override
public void sendNotification(
Expand All @@ -29,11 +33,14 @@ public void sendNotification(
) {
Member member = memberRepository.findById(targetMemberId).orElseThrow(NotExistMember::new);
sendNotification(member, data);
saveNotification(member, data);
}

@Override
public void broadcast(Map<String, String> data) {
memberRepository.findAll().forEach(member -> sendNotification(member, data));
List<Member> members = memberRepository.findAll();
members.forEach(member -> sendNotification(member, data));
members.forEach(member -> saveNotification(member, data));
}

private void sendNotification(
Expand All @@ -54,4 +61,12 @@ private void sendNotification(
log.error("device token - {}", member.getDeviceToken());
}
}

private void saveNotification(
Member member,
Map<String, String> data
) {
Notification notification = Notification.from(member, data);
notificationRepository.save(notification);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package swm.hkcc.LGTM.app.global.notification.service;
package swm.hkcc.LGTM.app.modules.notification.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import swm.hkcc.LGTM.app.global.notification.service.NotificationServiceImpl;
import swm.hkcc.LGTM.app.modules.notification.service.NotificationServiceImpl;

import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document;
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@


import com.epages.restdocs.apispec.ResourceSnippetParameters;
import com.epages.restdocs.apispec.SimpleType;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -15,11 +16,11 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import swm.hkcc.LGTM.app.global.notification.service.NotificationServiceImpl;
import swm.hkcc.LGTM.app.modules.notification.service.NotificationServiceImpl;
import swm.hkcc.LGTM.utils.CustomMDGenerator;

import static com.epages.restdocs.apispec.MockMvcRestDocumentationWrapper.document;
import static com.epages.restdocs.apispec.ResourceDocumentation.resource;
import static com.epages.restdocs.apispec.ResourceDocumentation.*;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.documentationConfiguration;
import static org.springframework.restdocs.mockmvc.RestDocumentationRequestBuilders.post;
import static org.springframework.restdocs.operation.preprocess.Preprocessors.*;
Expand Down Expand Up @@ -92,6 +93,15 @@ void contextLoads() throws Exception {
tableRow("400", "10100", "존재하지 않는 회원입니다.")
)
.build())
.queryParameters(
parameterWithName("isBroadcast").type(SimpleType.BOOLEAN).optional().description("전체 방송 여부"),
parameterWithName("targetMemberId").type(SimpleType.NUMBER).optional().description("전송 멤버 id")
)
.requestFields(
fieldWithPath("data").type(JsonFieldType.OBJECT).description("푸시 알림 데이터"),
fieldWithPath("data.title").type(JsonFieldType.STRING).description("푸시 알림 제목"),
fieldWithPath("data.body").type(JsonFieldType.STRING).description("푸시 알림 내용")
)
.responseFields( // 문서의 응답 필드
fieldWithPath("success").type(JsonFieldType.BOOLEAN).description("성공여부"),
fieldWithPath("responseCode").type(JsonFieldType.NUMBER).description("응답코드"),
Expand Down
Loading

0 comments on commit e1ab7b0

Please sign in to comment.