Skip to content

Commit

Permalink
feat: #126 비즈니스 로직과 원격 알림 발송 로직 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
jaewonLeeKOR committed Sep 10, 2024
1 parent f610335 commit 08e3b95
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ public class ApnsClientRequest {
@ToString
public static class SimplePushBody {
private Aps aps;

public SimplePushBody(String title, String subTitle, String body, Integer badge, String threadId, String targetContentId) {
this.aps = new Aps(new Aps.Alert(title, subTitle, body), badge, threadId, targetContentId);
@Builder
public SimplePushBody(String title, String subTitle, String body, Integer badge, String sound, String threadId, String targetContentId) {
this.aps = new Aps(new Aps.Alert(title, subTitle, body), badge, sound, threadId, targetContentId);
}

@ToString
Expand All @@ -23,6 +23,8 @@ public SimplePushBody(String title, String subTitle, String body, Integer badge,
public static class Aps {
private Alert alert;
private Integer badge;
@Schema(defaultValue = "default")
private String sound; // Library/Sounds 폴더 내의 파일 이름
@JsonProperty("thread-id")
private String threadId;
@JsonProperty("target-content-id")
Expand Down Expand Up @@ -58,7 +60,7 @@ public static class Aps {
private Integer badge; // 앱 아이콘에 표시할 뱃지 숫자
@Schema(defaultValue = "default")
private String sound; // Library/Sounds 폴더 내의 파일 이름
@Schema(defaultValue = "thread-id")
@JsonProperty("thread-id")
private String threadId; // 알림 그룹화를 위한 thread id (UNNotificationContent 객체의 threadIdentifier와 일치해야 함)
private String category; // 알림 그룹화를 위한 category, (UNNotificationCategory 식별자와 일치해야 함)
@Schema(defaultValue = "0")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public <T> Boolean sendApns(T request, List<String> deviceToken) {

deviceToken.parallelStream()
.forEach(token -> {
if (token.isBlank()) return;
tmpWebClient
.method(HttpMethod.POST)
.uri(token)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.server.capple.domain.board.mapper.BoardMapper;
import com.server.capple.domain.board.repository.BoardHeartRedisRepository;
import com.server.capple.domain.board.repository.BoardRepository;
import com.server.capple.domain.boardSubscribeMember.service.BoardSubscribeMemberService;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.domain.notifiaction.service.NotificationService;
import com.server.capple.global.exception.RestApiException;
import com.server.capple.global.exception.errorCode.BoardErrorCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -24,6 +26,8 @@ public class BoardServiceImpl implements BoardService {
private final BoardRepository boardRepository;
private final BoardHeartRedisRepository boardHeartRedisRepository;
private final BoardMapper boardMapper;
private final NotificationService notificationService;
private final BoardSubscribeMemberService boardSubscribeMemberService;

@Override
public BoardResponse.BoardCreate createBoard(Member member, BoardType boardType, String content) {
Expand All @@ -33,6 +37,7 @@ public BoardResponse.BoardCreate createBoard(Member member, BoardType boardType,
} else {
throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST);
}
boardSubscribeMemberService.createBoardSubscribeMember(member, board);
return boardMapper.toBoardCreate(board);
}

Expand All @@ -49,8 +54,8 @@ public BoardResponse.BoardsGetByBoardType getBoardsByBoardType(BoardType boardTy
throw new RestApiException(BoardErrorCode.BOARD_BAD_REQUEST);
}
return boardMapper.toBoardsGetByBoardType(boards.stream()
.map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId())))
.toList()
.map(board -> boardMapper.toBoardsGetByBoardTypeBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId())))
.toList()
);
}

Expand All @@ -62,30 +67,30 @@ public BoardResponse.BoardDelete deleteBoard(Member member, Long boardId) {
}

board.delete();
boardSubscribeMemberService.deleteBoardSubscribeMemberByBoardId(boardId);
return boardMapper.toBoardDelete(board);
}

@Override
public BoardResponse.BoardsSearchByKeyword searchBoardsByKeyword(String keyword) {
List<Board> boards = boardRepository.findBoardsByKeyword(keyword);
return boardMapper.toBoardsSearchByKeyword(boards.stream()
.map(board -> boardMapper.toBoardsSearchByKeywordBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId())))
.toList());
.map(board -> boardMapper.toBoardsSearchByKeywordBoardInfo(board, boardHeartRedisRepository.getBoardHeartsCount(board.getId())))
.toList());
}

@Override
public BoardResponse.BoardToggleHeart toggleBoardHeart(Member member, Long boardId) {
Board board = findBoard(boardId);
System.out.println(boardHeartRedisRepository.getBoardHeartCreateAt(board.getId(), member.getId()));

Boolean isLiked = boardHeartRedisRepository.toggleBoardHeart(member.getId(), board.getId());
boolean isLiked = boardHeartRedisRepository.toggleBoardHeart(member.getId(), board.getId());
if (isLiked) notificationService.sendBoardHeartNotification(member.getId(), board);
return new BoardResponse.BoardToggleHeart(boardId, isLiked);
}

@Override
public Board findBoard(Long boardId) {
return boardRepository.findById(boardId)
.orElseThrow(() -> new RestApiException(BoardErrorCode.BOARD_NOT_FOUND));
.orElseThrow(() -> new RestApiException(BoardErrorCode.BOARD_NOT_FOUND));
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import com.server.capple.domain.boardComment.mapper.BoardCommentMapper;
import com.server.capple.domain.boardComment.repository.BoardCommentHeartRedisRepository;
import com.server.capple.domain.boardComment.repository.BoardCommentRepository;
import com.server.capple.domain.boardSubscribeMember.service.BoardSubscribeMemberService;
import com.server.capple.domain.member.entity.Member;
import com.server.capple.domain.member.service.MemberService;
import com.server.capple.domain.notifiaction.service.NotificationService;
import com.server.capple.global.exception.RestApiException;
import com.server.capple.global.exception.errorCode.CommentErrorCode;
import lombok.RequiredArgsConstructor;
Expand All @@ -30,6 +32,8 @@ public class BoardCommentServiceImpl implements BoardCommentService {
private final BoardCommentRepository boardCommentRepository;
private final BoardCommentHeartRedisRepository boardCommentHeartRedisRepository;
private final BoardCommentMapper boardCommentMapper;
private final NotificationService notificationService;
private final BoardSubscribeMemberService boardSubscribeMemberService;

@Override
@Transactional
Expand All @@ -39,6 +43,8 @@ public BoardCommentId createBoardComment(Member member, Long boardId, BoardComme

BoardComment boardComment = boardCommentRepository.save(
boardCommentMapper.toBoardCommentEntity(loginMember, board, request.getComment()));
notificationService.sendBoardCommentNotification(loginMember.getId(), board, boardComment); // 게시글 댓글 알림
boardSubscribeMemberService.createBoardSubscribeMember(loginMember, board); // 알림 리스트 추가

return new BoardCommentId(boardComment.getId());
}
Expand Down Expand Up @@ -69,6 +75,11 @@ public BoardCommentId deleteBoardComment(Member member, Long commentId) {
public BoardCommentHeart heartBoardComment(Member member, Long commentId) {
Boolean isLiked = boardCommentHeartRedisRepository.
toggleBoardCommentHeart(commentId, member.getId());
if(isLiked) {
BoardComment boardComment = boardCommentRepository.findById(commentId).get();
if(!boardComment.getMember().getId().equals(member.getId()))
notificationService.sendBoardCommentHeartNotification(member.getId(), boardComment.getBoard(), boardComment);
}

return new BoardCommentHeart(commentId, isLiked);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.server.capple.domain.boardSubscribeMember.entity;

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.member.entity.Member;
import jakarta.persistence.*;
import lombok.*;

@Getter
@Entity
@AllArgsConstructor
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BoardSubscribeMember {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "member_id", nullable = false)
private Member member;

@ManyToOne
@JoinColumn(name = "board_id", nullable = false)
private Board board;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.server.capple.domain.boardSubscribeMember.reposiotry;

import com.server.capple.domain.boardSubscribeMember.entity.BoardSubscribeMember;
import com.server.capple.domain.member.entity.Member;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface BoardSubscribeMemberRepository extends JpaRepository<BoardSubscribeMember, Long> {
Boolean existsByMemberIdAndBoardId(Long memberId, Long boardId);
List<BoardSubscribeMember> findBoardSubscribeMembersByBoardId(Long boardId);
void deleteBoardSubscribeMemberByBoardId(Long boardId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.server.capple.domain.boardSubscribeMember.service;

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.member.entity.Member;

import java.util.List;

public interface BoardSubscribeMemberService {
void createBoardSubscribeMember(Member member, Board board);
List<Member> findBoardSubscribeMembers(Long boardId);
void deleteBoardSubscribeMemberByBoardId(Long boardId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.server.capple.domain.boardSubscribeMember.service;

import com.server.capple.domain.board.entity.Board;
import com.server.capple.domain.boardSubscribeMember.entity.BoardSubscribeMember;
import com.server.capple.domain.boardSubscribeMember.reposiotry.BoardSubscribeMemberRepository;
import com.server.capple.domain.member.entity.Member;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
public class BoardSubscribeMemberServiceImpl implements BoardSubscribeMemberService {
private final BoardSubscribeMemberRepository boardSubscribeMemberRepository;
@Override
public void createBoardSubscribeMember(Member member, Board board) {
if(boardSubscribeMemberRepository.existsByMemberIdAndBoardId(member.getId(), board.getId())) {
return;
}
boardSubscribeMemberRepository.save(BoardSubscribeMember.builder().member(member).board(board).build());
}

@Override
public List<Member> findBoardSubscribeMembers(Long boardId) {
return boardSubscribeMemberRepository.findBoardSubscribeMembersByBoardId(boardId).stream().map(BoardSubscribeMember::getMember).toList();
}

@Override
public void deleteBoardSubscribeMemberByBoardId(Long boardId) {
boardSubscribeMemberRepository.deleteBoardSubscribeMemberByBoardId(boardId);
}
}

0 comments on commit 08e3b95

Please sign in to comment.