Skip to content

Commit

Permalink
Merge pull request #35 from GPGT-Algorithm-Study/develop
Browse files Browse the repository at this point in the history
배포를 위한 머지
  • Loading branch information
fing9 authored May 26, 2024
2 parents f952af2 + 530e003 commit c4ee4a9
Show file tree
Hide file tree
Showing 12 changed files with 904 additions and 684 deletions.
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
package com.randps.randomdefence.domain.comment.service;

import com.amazonaws.services.kms.model.NotFoundException;
import com.randps.randomdefence.domain.board.domain.Board;
import com.randps.randomdefence.domain.board.service.port.BoardRepository;
import com.randps.randomdefence.domain.comment.domain.Comment;
import com.randps.randomdefence.domain.comment.dto.CommentDto;
import com.randps.randomdefence.domain.comment.dto.CommentPublishRequest;
import com.randps.randomdefence.domain.comment.dto.CommentUpdateRequest;
import com.randps.randomdefence.domain.comment.service.port.CommentRepository;
import com.randps.randomdefence.domain.notify.enums.NotifyType;
import com.randps.randomdefence.domain.notify.service.NotifyService;
import com.randps.randomdefence.domain.user.domain.User;
import com.randps.randomdefence.domain.user.service.port.UserRepository;
import java.util.List;
import javax.transaction.Transactional;
import lombok.Builder;
Expand All @@ -17,78 +23,114 @@
@Service
public class CommentService {

private final CommentRepository commentRepository;

/**
* 코멘트 저장
*/
@Transactional
public Comment save(CommentPublishRequest commentPublishRequest) {
Comment comment = Comment.builder().boardId(commentPublishRequest.getBoardId())
.bojHandle(commentPublishRequest.getBojHandle())
.parentCommentId(commentPublishRequest.getParentCommentId()).content(commentPublishRequest.getContent())
.build();

commentRepository.save(comment);

private final BoardRepository boardRepository;

private final UserRepository userRepository;

private final CommentRepository commentRepository;

private final NotifyService notifyService;

/**
* 코멘트 저장
*/
@Transactional
public Comment save(CommentPublishRequest commentPublishRequest) {
Comment comment = Comment.builder().boardId(commentPublishRequest.getBoardId())
.bojHandle(commentPublishRequest.getBojHandle())
.parentCommentId(commentPublishRequest.getParentCommentId())
.content(commentPublishRequest.getContent())
.build();

commentRepository.save(comment);

// 알림을 발행한다.
Board board = boardRepository.findById(commentPublishRequest.getBoardId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 게시글입니다."));
User commenter = userRepository.findByBojHandle(commentPublishRequest.getBojHandle())
.orElseThrow(() -> new NotFoundException("존재하지 않는 유저입니다."));

if (commentPublishRequest.getParentCommentId() != null) {
// 대댓글 알림을 발행한다.
Comment parentComment = commentRepository.findById(commentPublishRequest.getParentCommentId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 댓글입니다."));
User parentCommenter = userRepository.findByBojHandle(parentComment.getBojHandle())
.orElseThrow(
() -> new NotFoundException("존재하지 않는 유저입니다."));
if (!parentCommenter.getBojHandle().equals(commenter.getBojHandle())) {
notifyService.systemPublish(parentCommenter.getBojHandle(),
"[" + board.getTitle() + "] 에 작성한 댓글에 [" + commenter.getNotionId()
+ "] 님이 댓글을 작성했습니다." + " - \"" + commentPublishRequest.getContent() + "\"",
NotifyType.SYSTEM, null);
return comment;
}
}

/**
* 댓글 수정
*/
@Transactional
public Comment update(CommentUpdateRequest commentUpdateRequest) {
Comment comment = commentRepository.findById(commentUpdateRequest.getCommentId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 댓글입니다."));

// 수정
comment.update(commentUpdateRequest.getBoardId(), commentUpdateRequest.getBojHandle(),
commentUpdateRequest.getParentCommentId(), commentUpdateRequest.getContent());
commentRepository.save(comment);

return comment;
}

/**
* 댓글Id로 댓글 삭제
*/
@Transactional
public void delete(Long commentId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 댓글입니다."));

// 우선 삭제
commentRepository.delete(comment);

// 대댓글을 삭제하기 위해 모두 조회한다.
List<Comment> subComments = commentRepository.findAllByParentCommentId(comment.getId());

// 모든 서브 comment를 삭제했다면 종료
if (subComments.size() == 0) {
return;
}

// 재귀적 삭제
for (Comment subComment : subComments) {
delete(subComment.getId());
}
}

/**
* 게시글Id로 모든 댓글 조회
*/
public List<CommentDto> findAllByBoardId(Long boardId) {
return commentRepository.findAllByBoardId(boardId);
if (!commenter.getBojHandle().equals(board.getBojHandle())) {
// 글쓴이에게 보내는 알림을 발행한다.
notifyService.systemPublish(board.getBojHandle(),
"작성한 글 [" + board.getTitle() + "] 에 [" + commenter.getNotionId()
+ "] 님이 댓글을 작성했습니다." + " - \"" + commentPublishRequest.getContent() + "\"",
NotifyType.SYSTEM, null);
}

@Transactional
public void deleteAllByBoardId(Long boardId) {
commentRepository.deleteAllByBoardId(boardId);
return comment;
}

/**
* 댓글 수정
*/
@Transactional
public Comment update(CommentUpdateRequest commentUpdateRequest) {
Comment comment = commentRepository.findById(commentUpdateRequest.getCommentId())
.orElseThrow(() -> new NotFoundException("존재하지 않는 댓글입니다."));

// 수정
comment.update(commentUpdateRequest.getBoardId(), commentUpdateRequest.getBojHandle(),
commentUpdateRequest.getParentCommentId(), commentUpdateRequest.getContent());
commentRepository.save(comment);

return comment;
}

/**
* 댓글Id로 댓글 삭제
*/
@Transactional
public void delete(Long commentId) {
Comment comment = commentRepository.findById(commentId)
.orElseThrow(() -> new NotFoundException("존재하지 않는 댓글입니다."));

// 우선 삭제
commentRepository.delete(comment);

// 대댓글을 삭제하기 위해 모두 조회한다.
List<Comment> subComments = commentRepository.findAllByParentCommentId(comment.getId());

// 모든 서브 comment를 삭제했다면 종료
if (subComments.size() == 0) {
return;
}

@Transactional
public void deleteAllByBojHandle(String bojHandle) {
commentRepository.deleteAllByBojHandle(bojHandle);
// 재귀적 삭제
for (Comment subComment : subComments) {
delete(subComment.getId());
}
}

/**
* 게시글Id로 모든 댓글 조회
*/
public List<CommentDto> findAllByBoardId(Long boardId) {
return commentRepository.findAllByBoardId(boardId);
}

@Transactional
public void deleteAllByBoardId(Long boardId) {
commentRepository.deleteAllByBoardId(boardId);
}

@Transactional
public void deleteAllByBojHandle(String bojHandle) {
commentRepository.deleteAllByBojHandle(bojHandle);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.randps.randomdefence.domain.notify.controller;

import com.randps.randomdefence.domain.notify.dto.NotifyResponse;
import com.randps.randomdefence.domain.notify.dto.UnreadNotifyCountResponse;
import com.randps.randomdefence.domain.notify.service.NotifySearchService;
import com.randps.randomdefence.global.jwt.component.JWTRefreshUtil;
import java.nio.file.AccessDeniedException;
Expand All @@ -9,6 +10,7 @@
import lombok.Builder;
import lombok.RequiredArgsConstructor;
import org.springframework.data.repository.query.Param;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -28,9 +30,29 @@ public class NotifySearchController {
* 사용자가 자신에게 온 알림을 조회한다.
*/
@GetMapping("/receiver")
public List<NotifyResponse> searchByReceiver(@RequestHeader("Refresh_Token") String refresh, @Param("receiver") String receiver)
public List<NotifyResponse> searchByReceiver(@RequestHeader("Refresh_Token") String refresh,
@Param("receiver") String receiver)
throws CertificateExpiredException, AccessDeniedException {
return notifySearchService.findAllByReceiver(receiver, jwtRefreshUtil.getBojHandle(refresh));
}

/*
* 사용자가 아직 읽지 않은 알림의 개수를 조회한다.
*/
@GetMapping("/unread/count")
public UnreadNotifyCountResponse searchUnreadCount(Authentication authentication)
throws AccessDeniedException {
String userName = authentication.getName();
if (userName == null) {
throw new AccessDeniedException("권한이 없습니다.");
}

Long count = (long) notifySearchService.findNotReadNotifiesAllByReceiver(userName, userName)
.size();

return UnreadNotifyCountResponse.builder()
.count(count)
.build();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,19 @@ public class Notify extends BaseTimeEntity {
@Column(length = 2000)
private String message;

private Long relatedBoardId;

private NotifyType type;

private Boolean isRead;


@Builder
public Notify(Long id, String receiver, String message, NotifyType type) {
public Notify(Long id, String receiver, String message, Long relatedBoardId, NotifyType type) {
this.id = id;
this.receiver = receiver;
this.message = message;
this.relatedBoardId = relatedBoardId;
this.type = type;
this.isRead = false;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@ public class NotifyResponse {

private NotifyType type;

private Long relatedBoardId;

private Boolean isRead;

public static NotifyResponse of(Long id, String receiver, String message, NotifyType type, Boolean isRead) {
public static NotifyResponse of(Long id, String receiver, String message, NotifyType type,
Long relatedBoardId, Boolean isRead) {
return NotifyResponse.builder()
.id(id)
.receiver(receiver)
.message(message)
.type(type)
.relatedBoardId(relatedBoardId)
.isRead(isRead)
.build();
}
Expand All @@ -35,6 +39,7 @@ public static NotifyResponse of(Notify notify) {
.receiver(notify.getReceiver())
.message(notify.getMessage())
.type(notify.getType())
.relatedBoardId(notify.getRelatedBoardId())
.isRead(notify.getIsRead())
.build();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.randps.randomdefence.domain.notify.dto;

import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class UnreadNotifyCountResponse {

private Long count;

@Builder
public UnreadNotifyCountResponse(Long count) {
this.count = count;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.randps.randomdefence.domain.notify.dto.NotifyPublishToUsersRequest;
import com.randps.randomdefence.domain.notify.dto.NotifyReadRequest;
import com.randps.randomdefence.domain.notify.dto.NotifyUpdateRequest;
import com.randps.randomdefence.domain.notify.enums.NotifyType;
import com.randps.randomdefence.domain.notify.service.port.NotifyRepository;
import com.randps.randomdefence.domain.user.domain.User;
import com.randps.randomdefence.domain.user.service.port.UserRepository;
Expand Down Expand Up @@ -47,6 +48,25 @@ public Notify publish(NotifyPublishRequest request, String sender) throws Access
return notifyRepository.save(notify);
}

/*
* 알림을 발행한다.
*/
@Transactional
public Notify systemPublish(String receiver, String message, NotifyType type,
Long relatedBoardId) {
if (userRepository.findByBojHandle(receiver).isEmpty()) {
return null;
}

Notify notify = Notify.builder()
.receiver(receiver)
.message(message)
.type(type)
.relatedBoardId(relatedBoardId)
.build();
return notifyRepository.save(notify);
}

/*
* 특정 알림의 내용을 수정한다.
*/
Expand Down
Loading

0 comments on commit c4ee4a9

Please sign in to comment.