Skip to content

Commit

Permalink
Merge pull request #107 from siwan9/dev
Browse files Browse the repository at this point in the history
모집 게시판, 모집 신청 리팩토링
  • Loading branch information
siwan9 authored Nov 18, 2024
2 parents 1cc30af + 1e83113 commit 211e7bc
Show file tree
Hide file tree
Showing 33 changed files with 227 additions and 228 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.example.demo.domain.board.domain.dto.request.BoardUpdateRequest;
import com.example.demo.domain.board.domain.dto.response.BoardInfoResponse;
import com.example.demo.domain.board.domain.dto.response.BoardTitleInfoResponse;
import com.example.demo.global.base.dto.page.GlobalPageResponse;
import com.example.demo.domain.board.domain.dto.vo.BoardType;
import com.example.demo.domain.board.domain.dto.vo.Status;
import com.example.demo.domain.board.domain.entity.Board;
Expand All @@ -13,14 +12,14 @@
import com.example.demo.domain.board.service.service.ViewIncreaseService;
import com.example.demo.domain.newsletter.event.EmailNotificationEvent;
import com.example.demo.domain.newsletter.strategy.SeminarSummaryEmailDeliveryStrategy;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import com.example.demo.domain.user.domain.User;
import com.example.demo.domain.user.domain.vo.Role;
import com.example.demo.domain.user.service.UserService;
import com.example.demo.global.base.dto.page.GlobalPageResponse;
import com.example.demo.global.base.exception.ErrorCode;
import com.example.demo.global.base.exception.ServiceException;

import lombok.RequiredArgsConstructor;

import org.springframework.context.ApplicationEventPublisher;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
Expand All @@ -39,7 +38,7 @@ public class BoardUseCase {
public BoardInfoResponse saveDraftBoard(Long userId, BoardCreateRequest boardCreateRequest) {
User user = userService.validateUser(userId);
// 공지사항은 관리자만 작성 가능
if(boardCreateRequest.getBoardType().equals(BoardType.NOTICE) && !user.getRole().equals(Role.ROLE_ADMIN)){
if (boardCreateRequest.getBoardType().equals(BoardType.NOTICE) && !user.getRole().equals(Role.ROLE_ADMIN)) {
throw new ServiceException(ErrorCode.NOT_AUTHORIZED_WRITE_NOTICE);
}
return boardCommandService.createDraftBoard(user, boardCreateRequest);
Expand All @@ -57,10 +56,10 @@ public BoardInfoResponse updateBoard(Long userId, BoardUpdateRequest boardUpdate
BoardInfoResponse boardInfoResponse = boardCommandService.updateBoard(boardUpdateRequest, board);

// 게시 상태로 변경이면 뉴스레터 전송
if(boardUpdateRequest.getIsPublished() && board.getBoardType().equals(BoardType.SEMINAR) && board.getStatus().equals(Status.DRAFT)) {
if (boardUpdateRequest.getIsPublished() && board.getBoardType().equals(BoardType.SEMINAR) && board.getStatus().equals(Status.DRAFT)) {
eventPublisher.publishEvent(EmailNotificationEvent.create(
com.example.demo.domain.recruitment_board.domain.dto.vo.BoardType.SEMINAR_SUMMARY,
SeminarSummaryEmailDeliveryStrategy.create(board)
EntireBoardType.SEMINAR_SUMMARY,
SeminarSummaryEmailDeliveryStrategy.create(board)
));
}

Expand All @@ -72,7 +71,7 @@ public void deleteBoard(Long userId, Long boardId) {
}

@Transactional(readOnly = true)
public GlobalPageResponse<BoardTitleInfoResponse> findBoardList(Pageable pageable) {
public GlobalPageResponse<BoardTitleInfoResponse> findBoardList(Pageable pageable) {
return boardQueryService.findBoardPageList(pageable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.example.demo.domain.comment.domain.dto.response.CommentResponse;
import com.example.demo.domain.comment.domain.vo.CommentTargetBoardType;
import com.example.demo.domain.comment.service.CommentService;
import com.example.demo.domain.recruitment_board.domain.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import com.example.demo.global.aop.AssignUserId;
import com.example.demo.global.base.dto.ResponseBody;
import jakarta.validation.Valid;
Expand All @@ -31,7 +31,7 @@ public class CommentController {
* [게시물 별 댓글 조회]<br>
* 게시물 id에 해당하는 댓글 조회
*
* @param boardType [BASIC, RECRUITMENT]
* @param commentTargetBoardType [BASIC, RECRUITMENT]
* @apiNote 1. 삭제된 댓글도 응답으로 보낸다 <br>
* -> 프론트에서 deletedAt 널 체크 후 "삭제되었습니다"로 내용 변경이 필요 <br>
* 2. 기획의 요청에 따른 페이징 처리 X <br>
Expand All @@ -40,16 +40,16 @@ public class CommentController {
@GetMapping("/{boardId}")
public ResponseEntity<ResponseBody<CommentResponse>> getBoardComments(
@PathVariable Long boardId,
@RequestParam CommentTargetBoardType boardType) {
@RequestParam CommentTargetBoardType commentTargetBoardType) {
// TODO : 내가 차단한 사용자 댓글은 보이지 않도록(로그인했을 시)
return ResponseEntity.ok(createSuccessResponse(commentService.findCommentsByBoardId(boardId, boardType)));
return ResponseEntity.ok(createSuccessResponse(commentService.findCommentsByBoardId(boardId, commentTargetBoardType)));
}

/**
* [사용자 작성 댓글 조회] <br>
* 사용자 정보 창에서 게시판 타입 선택 시 타입에 해당하는 게시물에 작성했던 사용자의 댓글 조회
*
* @param boardType [STUDY, PROJECT, MENTORING, SEMINAR_NOTICE, SEMINAR_SUMMARY]
* @param entireBoardType [STUDY, PROJECT, MENTORING, SEMINAR_NOTICE, SEMINAR_SUMMARY]
* @apiNote 1. QueryDsl 에 정렬 조건 넣기가 까다로워서 일단 날짜순으로 내림차순 정렬 하드코딩 해놨음 <br>
* 2. 쿼리파라미터의 BoardType은 대소문자 따지지않고 스펠링만 맞으면 역직렬화 성공하도록 구현
*/
Expand All @@ -59,25 +59,25 @@ public ResponseEntity<ResponseBody<CommentResponse>> getBoardComments(
public ResponseEntity<ResponseBody<CommentPageResponse>> getUserComments(
Long userId,
@PageableDefault(page = 0, size = 10, sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
@RequestParam BoardType boardType) {
return ResponseEntity.ok(createSuccessResponse(commentService.findCommentsByUserId(userId, pageable, boardType)));
@RequestParam EntireBoardType entireBoardType) {
return ResponseEntity.ok(createSuccessResponse(commentService.findCommentsByUserId(userId, pageable, entireBoardType)));
}

/**
* [댓글 저장] <br>
* 게시글에서 댓글 작성 후 저장
*
* @param boardType [BASIC, RECRUITMENT]
* @param commentTargetBoardType [BASIC, RECRUITMENT]
*/
@AssignUserId
@PreAuthorize("isAuthenticated() and hasAnyRole('ROLE_USER')")
@PostMapping("/{boardId}")
public ResponseEntity<ResponseBody<CommentInfoResponse>> createComment(
Long userId,
@PathVariable Long boardId,
@RequestParam CommentTargetBoardType boardType,
@RequestParam CommentTargetBoardType commentTargetBoardType,
@RequestBody @Valid CommentRequest commentRequest) {
return ResponseEntity.ok(createSuccessResponse(commentService.saveComment(userId, boardId, boardType, commentRequest)));
return ResponseEntity.ok(createSuccessResponse(commentService.saveComment(userId, boardId, commentTargetBoardType, commentRequest)));
}

/**
Expand All @@ -98,7 +98,7 @@ public ResponseEntity<ResponseBody<CommentInfoResponse>> updateComment(
* [댓글 삭제] <br>
* 작성한 댓글 soft 삭제
*
* @apiNote 1. 삭제된 댓글도 응답으로 보내야하므로 Comment 엔티티에 SQLDelete 처리를 해놓지 않았음
* @apiNote 1. 삭제된 댓글도 응답으로 보내야하므로 Comment 엔티티에 SQLRestriction 처리를 해놓지 않았음
*/
@AssignUserId
@PreAuthorize("isAuthenticated() and hasAnyRole('ROLE_USER')")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.demo.domain.comment.domain.dto.response;

import com.example.demo.domain.comment.domain.entity.Comment;
import com.example.demo.domain.recruitment_board.domain.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
Expand All @@ -20,9 +20,9 @@ public class CommentPageResponse {
private final String pageSort;
private final List<MyCommentResponse> myCommentResponseList;

public static CommentPageResponse from(Page<Comment> commentEntityPage, BoardType boardType) {
public static CommentPageResponse from(Page<Comment> commentEntityPage, EntireBoardType entireBoardType) {
List<MyCommentResponse> myCommentResponseList;
switch (boardType) {
switch (entireBoardType) {
case SEMINAR_NOTICE, SEMINAR_SUMMARY -> myCommentResponseList = commentEntityPage.stream()
.map(MyCommentResponse::fromSeminarComment)
.collect(Collectors.toList());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ public class Comment extends BaseEntity {
@JoinColumn(name = "group_id")
private Comment parentComment;

// 쿼리 실험 필요
@OneToMany(mappedBy = "parentComment", cascade = CascadeType.ALL, orphanRemoval = true, fetch = FetchType.LAZY)
@OrderBy("createdAt ASC")
private List<Comment> replyComments = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.demo.domain.comment.repository;

import com.example.demo.domain.comment.domain.entity.Comment;
import com.example.demo.domain.recruitment_board.domain.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

Expand All @@ -12,5 +12,5 @@ public interface CustomCommentRepository {

List<Comment> findByRecruitmentBoard_idOrderByCreatedAtAsc(Long recruitmentBoardId);

Page<Comment> findPageByUser_idOrderByCreatedAtDsc(Long userId, Pageable pageable, BoardType boardType);
Page<Comment> findPageByUser_idOrderByCreatedAtDsc(Long userId, Pageable pageable, EntireBoardType entireBoardType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import com.example.demo.domain.comment.domain.entity.Comment;
import com.example.demo.domain.comment.domain.entity.QComment;
import com.example.demo.domain.recruitment_board.domain.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import com.example.demo.domain.recruitment_board.domain.vo.RecruitmentBoardType;
import com.querydsl.jpa.impl.JPAQueryFactory;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -49,15 +49,15 @@ public List<Comment> findByRecruitmentBoard_idOrderByCreatedAtAsc(Long recruitme
}

@Override
public Page<Comment> findPageByUser_idOrderByCreatedAtDsc(Long userId, Pageable pageable, BoardType boardType) {
if (boardType == BoardType.SEMINAR_NOTICE || boardType == BoardType.SEMINAR_SUMMARY) {
return findSeminarCommentPage(pageable, userId, boardType);
public Page<Comment> findPageByUser_idOrderByCreatedAtDsc(Long userId, Pageable pageable, EntireBoardType entireBoardType) {
if (entireBoardType == EntireBoardType.SEMINAR_NOTICE || entireBoardType == EntireBoardType.SEMINAR_SUMMARY) {
return findSeminarCommentPage(pageable, userId, entireBoardType);
} else {
return findRecruitmentCommentPage(pageable, userId, boardType);
return findRecruitmentCommentPage(pageable, userId, entireBoardType);
}
}

public Page<Comment> findSeminarCommentPage(Pageable pageable, Long userId, BoardType boardType) {
public Page<Comment> findSeminarCommentPage(Pageable pageable, Long userId, EntireBoardType entireBoardType) {
List<Comment> content = jpaQueryFactory
.selectFrom(comment)
.join(comment.board, board).fetchJoin()
Expand All @@ -82,12 +82,12 @@ public Page<Comment> findSeminarCommentPage(Pageable pageable, Long userId, Boar
return new PageImpl<>(content, pageable, totalCount);
}

public Page<Comment> findRecruitmentCommentPage(Pageable pageable, Long userId, BoardType boardType) {
public Page<Comment> findRecruitmentCommentPage(Pageable pageable, Long userId, EntireBoardType entireBoardType) {
List<Comment> content = jpaQueryFactory
.selectFrom(comment)
.join(comment.recruitmentBoard, recruitmentBoard).fetchJoin()
.where(comment.recruitmentBoard.isNotNull(),
comment.recruitmentBoard.type.eq(RecruitmentBoardType.valueOf(boardType.toString())),
comment.recruitmentBoard.type.eq(RecruitmentBoardType.valueOf(entireBoardType.toString())),
comment.user.id.eq(userId),
comment.deletedAt.isNull())
.orderBy(comment.createdAt.desc())
Expand All @@ -98,7 +98,7 @@ public Page<Comment> findRecruitmentCommentPage(Pageable pageable, Long userId,
.select(comment.count())
.from(comment)
.where(comment.recruitmentBoard.isNotNull(),
comment.recruitmentBoard.type.eq(RecruitmentBoardType.valueOf(boardType.toString())),
comment.recruitmentBoard.type.eq(RecruitmentBoardType.valueOf(entireBoardType.toString())),
comment.user.id.eq(userId),
comment.deletedAt.isNull())
.fetchOne();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import com.example.demo.domain.comment.domain.vo.CommentTargetBoardType;
import com.example.demo.domain.comment.repository.CommentRepository;
import com.example.demo.domain.recruitment_board.domain.entity.RecruitmentBoard;
import com.example.demo.domain.recruitment_board.domain.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.vo.EntireBoardType;
import com.example.demo.domain.recruitment_board.repository.RecruitmentBoardRepository;
import com.example.demo.domain.user.domain.User;
import com.example.demo.domain.user.service.UserService;
Expand All @@ -35,25 +35,25 @@ public class CommentService {
private final RecruitmentBoardRepository recruitmentBoardRepository;

@Transactional(readOnly = true)
public CommentResponse findCommentsByBoardId(Long boardId, CommentTargetBoardType boardType) {
List<Comment> commentList = findCommentsFactory(boardId, boardType);
public CommentResponse findCommentsByBoardId(Long boardId, CommentTargetBoardType commentTargetBoardType) {
List<Comment> commentList = findCommentsFactory(boardId, commentTargetBoardType);

return CommentResponse.from(commentList);
}

@Transactional(readOnly = true)
public CommentPageResponse findCommentsByUserId(Long userId, Pageable pageable, BoardType boardType) {
public CommentPageResponse findCommentsByUserId(Long userId, Pageable pageable, EntireBoardType entireBoardType) {
userService.validateUser(userId);

Page<Comment> commentPage = commentRepository.findPageByUser_idOrderByCreatedAtDsc(userId, pageable, boardType);
return CommentPageResponse.from(commentPage, boardType);
Page<Comment> commentPage = commentRepository.findPageByUser_idOrderByCreatedAtDsc(userId, pageable, entireBoardType);
return CommentPageResponse.from(commentPage, entireBoardType);
}

@Transactional
public CommentInfoResponse saveComment(Long userId, Long boardId, CommentTargetBoardType boardType, CommentRequest commentRequest) {
public CommentInfoResponse saveComment(Long userId, Long boardId, CommentTargetBoardType commentTargetBoardType, CommentRequest commentRequest) {
User commentUser = userService.validateUser(userId);

Comment requestComment = transformToEntityFactory(commentUser, boardId, boardType, commentRequest);
Comment requestComment = transformToEntityFactory(commentUser, boardId, commentTargetBoardType, commentRequest);
Comment saved = commentRepository.save(requestComment);

return CommentInfoResponse.from(saved);
Expand Down Expand Up @@ -88,8 +88,8 @@ public void deleteComment(Long userId, Long commentId, boolean isAuthorized) {
commentRepository.delete(comment);
}

public List<Comment> findCommentsFactory(Long boardId, CommentTargetBoardType boardType) {
switch (boardType) {
public List<Comment> findCommentsFactory(Long boardId, CommentTargetBoardType commentTargetBoardType) {
switch (commentTargetBoardType) {
case BASIC -> {
boardRepository.findById(boardId).orElseThrow(() ->
new ServiceException(ErrorCode.BOARD_NOT_FOUND)
Expand All @@ -105,15 +105,15 @@ public List<Comment> findCommentsFactory(Long boardId, CommentTargetBoardType bo
}
}

public Comment transformToEntityFactory(User commentUser, Long boardId, CommentTargetBoardType boardType, CommentRequest commentRequest) {
public Comment transformToEntityFactory(User commentUser, Long boardId, CommentTargetBoardType commentTargetBoardType, CommentRequest commentRequest) {
Comment parentComment = null;
if (commentRequest.getGroupId() != null) {
parentComment = commentRepository.findNotDeleteCommentById(commentRequest.getGroupId()).orElseThrow(() ->
new ServiceException(ErrorCode.PARENT_NOT_FOUND));
}

Comment requestComment = null;
switch (boardType) {
switch (commentTargetBoardType) {
case BASIC -> {
Board commentBoard = boardRepository.findById(boardId).orElseThrow(() ->
new ServiceException(ErrorCode.BOARD_NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,15 @@
package com.example.demo.domain.newsletter.controller;

import com.example.demo.domain.board.domain.dto.vo.Status;
import com.example.demo.domain.board.domain.entity.Board;
import com.example.demo.domain.newsletter.domain.dto.request.NewsletterSubscribeRequest;
import com.example.demo.domain.newsletter.domain.dto.request.NewsletterUpdateEmailRequest;
import com.example.demo.domain.newsletter.domain.dto.request.NewsletterUpdateNotifyRequest;
import com.example.demo.domain.newsletter.domain.dto.response.NewsletterInfo;
import com.example.demo.domain.newsletter.event.EmailNotificationEvent;
import com.example.demo.domain.newsletter.service.NewsletterService;
import com.example.demo.domain.newsletter.strategy.*;
import com.example.demo.domain.recruitment_board.domain.dto.vo.BoardType;
import com.example.demo.domain.recruitment_board.domain.dto.vo.RecruitmentBoardTag;
import com.example.demo.domain.recruitment_board.domain.dto.vo.RecruitmentBoardType;
import com.example.demo.domain.recruitment_board.domain.entity.RecruitmentBoard;
import com.example.demo.domain.user.domain.User;
import com.example.demo.domain.user.domain.vo.Role;
import com.example.demo.global.aop.AssignUserId;
import com.example.demo.global.base.dto.ResponseBody;
import com.example.demo.global.oauth.user.OAuth2Provider;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

import static com.example.demo.global.base.dto.ResponseUtil.createSuccessResponse;
import static com.example.demo.global.regex.UserRegex.EMAIL_REGEXP;

Expand Down
Loading

0 comments on commit 211e7bc

Please sign in to comment.