Skip to content

Commit

Permalink
Merge pull request #156 from Re-4aliens/fix/#150_front_report_errors
Browse files Browse the repository at this point in the history
Fix/#150 프론트가 신고한 에러수정
  • Loading branch information
mjj111 authored Sep 3, 2024
2 parents 5fa4c22 + 0be6a29 commit b7e13d8
Show file tree
Hide file tree
Showing 15 changed files with 164 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public BoardController(final BoardService boardService, final BoardReadService b
@PostMapping("/normal")
public SuccessResponse<?> createBoard(@Login final LoginMember loginMember,
@RequestPart final BoardCreateRequest request,
@RequestPart final List<MultipartFile> boardImages) {
@RequestPart(required = false) final List<MultipartFile> boardImages) {
boardService.postNormalBoard(request, boardImages, loginMember);
return SuccessResponse.of(BoardSuccess.POST_BOARD_SUCCESS);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.aliens.backend.board.service;

import com.aliens.backend.board.controller.dto.request.ChildCommentCreateRequest;
import com.aliens.backend.board.controller.dto.request.ParentCommentCreateRequest;
import com.aliens.backend.board.domain.Board;
import com.aliens.backend.notification.controller.dto.NotificationRequest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CommentEventPublisher {
private final ApplicationEventPublisher eventPublisher;

public CommentEventPublisher(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}

public void sendParentCommentNotification(Board board,
ParentCommentCreateRequest request) {

eventPublisher.publishEvent(new NotificationRequest(
board.getCategory(),
board.getId(),
"\"" + request.content() + "\" 라는 댓글이 달렸습니다.",
List.of(board.getWriterId())));
}

public void sendChildCommentNotification(Board board,
ChildCommentCreateRequest request,
Long parentCommentMemberId) {
System.out.println("작성자 Id" + board.getWriterId());
System.out.println("부모댓글 Id" + parentCommentMemberId);
eventPublisher.publishEvent(new NotificationRequest(
board.getCategory(),
board.getId(),
"\"" + request.content() + "\" 라는 댓글이 달렸습니다.",
List.of(board.getWriterId(), parentCommentMemberId)));
}
}
50 changes: 22 additions & 28 deletions src/main/java/com/aliens/backend/board/service/CommentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
import com.aliens.backend.global.exception.RestApiException;
import com.aliens.backend.global.response.error.BoardError;
import com.aliens.backend.global.response.error.MemberError;
import com.aliens.backend.notification.controller.dto.NotificationRequest;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand All @@ -30,10 +28,14 @@ public class CommentService {
private final MemberRepository memberRepository;
private final BoardRepository boardRepository;
private final CommentCustomRepository commentCustomRepository;
private final ApplicationEventPublisher eventPublisher;
private final CommentEventPublisher eventPublisher;


public CommentService(final CommentRepository commentRepository, final MemberRepository memberRepository, final BoardRepository boardRepository, final CommentCustomRepository commentCustomRepository, final ApplicationEventPublisher eventPublisher) {
public CommentService(final CommentRepository commentRepository,
final MemberRepository memberRepository,
final BoardRepository boardRepository,
final CommentCustomRepository commentCustomRepository,
final CommentEventPublisher eventPublisher) {
this.commentRepository = commentRepository;
this.memberRepository = memberRepository;
this.boardRepository = boardRepository;
Expand All @@ -46,27 +48,19 @@ public void postParentComment(final ParentCommentCreateRequest request,
final LoginMember loginMember) {
Member member = getMember(loginMember);
Board board = findBoard(request.boardId());

Comment comment = Comment.parentOf(request.content(), board, member);
board.addComment(comment);

commentRepository.save(comment);

if(!comment.isWriter(board.getWriterId())) {
sendParentCommentNotification(board, request);
eventPublisher.sendParentCommentNotification(board, request);
}
}

private void sendParentCommentNotification(Board board,
ParentCommentCreateRequest request) {
eventPublisher.publishEvent(new NotificationRequest(
board.getCategory(),
board.getId(),
"\"" + request.content() + "\" 라는 댓글이 달렸습니다.",
List.of(board.getWriterId())));
}

private Board findBoard(final Long boardId) {
return boardRepository.findById(boardId).orElseThrow(() -> new RestApiException(MemberError.NULL_MEMBER));
return boardRepository.findById(boardId).orElseThrow(() -> new RestApiException(BoardError.INVALID_BOARD_ID));
}

private Member getMember(final LoginMember loginMember) {
Expand All @@ -78,26 +72,20 @@ public void postChildComment(final ChildCommentCreateRequest request,
final LoginMember loginMember) {
Member member = getMember(loginMember);
Board board = findBoard(request.boardId());
Comment parentComment = commentRepository.findById(request.parentCommentId()).orElseThrow(() -> new RestApiException(MemberError.NULL_MEMBER));
Comment parentComment = findComment(request);

Comment childComment = Comment.childOf(request, board, member);
board.addComment(childComment);

commentRepository.save(childComment);

if(!parentComment.isWriter(member.getId())) {
sendChildCommentNotification(board, request, parentComment.getWriterId());
eventPublisher.sendChildCommentNotification(board, request, parentComment.getWriterId());
}
}

private void sendChildCommentNotification(Board board,
ChildCommentCreateRequest request,
Long parentCommentMemberId) {
eventPublisher.publishEvent(new NotificationRequest(
board.getCategory(),
board.getId(),
"\"" + request.content() + "\" 라는 댓글이 달렸습니다.",
List.of(board.getWriterId(), parentCommentMemberId)));
private Comment findComment(ChildCommentCreateRequest request) {
return commentRepository.findById(request.parentCommentId()).orElseThrow(() -> new RestApiException(MemberError.NULL_MEMBER));
}

@Transactional(readOnly = true)
Expand All @@ -108,28 +96,34 @@ public List<BoardResponse> getCommentedBoardPage(final LoginMember loginMember,
@Transactional(readOnly = true)
public List<CommentResponse> getCommentsByBoardId(final Long boardId) {
List<Comment> comments = commentCustomRepository.getCommentsByBoardId(boardId);

ArrayList<CommentResponse> result = new ArrayList<>();
sortComments(comments, result);

return result;
}

private void sortComments(List<Comment> comments, ArrayList<CommentResponse> result) {
for(Comment comment : comments) {
if (!comment.isParent()){
continue;
}

CommentResponse parentComment = comment.getCommentResponse();

List<CommentResponse> childrenComment = comments.stream()
.filter(c -> (c.isChildFrom(comment.getId())))
.map(Comment::getCommentResponse)
.toList();

if(isNotEmtyChildren(childrenComment)) {
if(isNotEmptyChildren(childrenComment)) {
parentComment.setChildren(childrenComment);
}
result.add(parentComment);
}
return result;
}

private boolean isNotEmtyChildren(final List<CommentResponse> childrenComment) {
private boolean isNotEmptyChildren(final List<CommentResponse> childrenComment) {
return childrenComment != null && !childrenComment.isEmpty();
}

Expand Down
14 changes: 6 additions & 8 deletions src/main/java/com/aliens/backend/email/service/EmailService.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,13 @@ public class EmailService {

private final EmailAuthenticationRepository emailAuthenticationRepository;
private final EmailSender emailSender;
private final SymmetricKeyEncoder symmetricKeyEncoder;
private final MemberRepository memberRepository;

public EmailService(final EmailAuthenticationRepository emailRepository,
final EmailSender emailSender,
final SymmetricKeyEncoder symmetricKeyEncoder, final MemberRepository memberRepository) {
final MemberRepository memberRepository) {
this.emailAuthenticationRepository = emailRepository;
this.emailSender = emailSender;
this.symmetricKeyEncoder = symmetricKeyEncoder;
this.memberRepository = memberRepository;
}

Expand All @@ -50,7 +48,7 @@ public String sendAuthenticationEmail(final String email) {
EmailAuthentication emailEntity = new EmailAuthentication(email);
emailAuthenticationRepository.save(emailEntity);

String emailToken = symmetricKeyEncoder.encrypt(String.valueOf(emailEntity.getId()));
String emailToken = SymmetricKeyEncoder.encrypt(email);
emailSender.sendAuthenticationEmail(email, emailToken);

return EmailResponse.EMAIL_SEND_SUCCESS.getMessage();
Expand All @@ -70,14 +68,14 @@ private void deleteExistsEmail(final String email) {

@Transactional
public String authenticateEmail(final String token) {
Long emailEntityId = Long.valueOf(symmetricKeyEncoder.decrypt(token));
EmailAuthentication emailEntity = getEmailAuthentication(emailEntityId);
String email = SymmetricKeyEncoder.decrypt(token);
EmailAuthentication emailEntity = getEmailAuthenticationByEmail(email);
emailEntity.authenticate();
return EmailResponse.EMAIL_AUTHENTICATION_SUCCESS.getMessage();
}

private EmailAuthentication getEmailAuthentication(final Long emailEntityId) {
return emailAuthenticationRepository.findById(emailEntityId).orElseThrow(() -> new RestApiException(EmailError.NULL_EMAIL));
private EmailAuthentication getEmailAuthenticationByEmail(final String email) {
return emailAuthenticationRepository.findByEmail(email).orElseThrow(() -> new RestApiException(EmailError.NULL_EMAIL));
}

@EventListener
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,10 @@ public Long getRound() {
return round;
}

public LocalDateTime getRequestStartTime() {
return requestStartTime;
}

public LocalDateTime getRequestEndTime() {
return requestEndTime;
}

public LocalDateTime getValidStartTime() {
return validStartTime;
}

public LocalDateTime getValidEndTime() {
return validEndTime;
}

public DayOfWeek getDayOfWeek() {
return requestStartTime.getDayOfWeek();
}
Expand Down Expand Up @@ -90,4 +78,8 @@ public String toString() {
", validEndTime=" + validEndTime +
'}';
}

public boolean isFirstTime() {
return round == 1;
}
}
Loading

0 comments on commit b7e13d8

Please sign in to comment.