Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[develop] main merge #50

Merged
merged 9 commits into from
Mar 23, 2024
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package com.hatcher.haemo.comment.application;

import com.hatcher.haemo.comment.domain.Comment;
import com.hatcher.haemo.comment.dto.CommentEditRequest;
import com.hatcher.haemo.comment.dto.CommentPostRequest;
import com.hatcher.haemo.comment.repository.CommentRepository;
import com.hatcher.haemo.common.BaseResponse;
import com.hatcher.haemo.common.exception.BaseException;
import com.hatcher.haemo.recruitment.domain.Recruitment;
import com.hatcher.haemo.recruitment.repository.RecruitmentRepository;
import com.hatcher.haemo.user.application.UserService;
import com.hatcher.haemo.user.domain.User;
import com.hatcher.haemo.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.hatcher.haemo.common.constants.Constant.INACTIVE;
import static com.hatcher.haemo.common.enums.BaseResponseStatus.*;

@Service
@RequiredArgsConstructor
public class CommentService {

private final UserRepository userRepository;
private final UserService userService;
private final RecruitmentRepository recruitmentRepository;
private final CommentRepository commentRepository;

// 댓글 등록
@Transactional(rollbackFor = Exception.class)
public BaseResponse<String> postComment(CommentPostRequest commentPostRequest) throws BaseException {
try {
User writer = userRepository.findByUserIdx(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
Recruitment recruitment = recruitmentRepository.findById(commentPostRequest.recruitmentIdx()).orElseThrow(() -> new BaseException(INVALID_RECRUITMENT_IDX));
Comment comment = new Comment(recruitment, writer, commentPostRequest.content());
commentRepository.save(comment);
comment.setRecruitment(recruitment);
comment.setWriter(writer);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

// [작성자] 댓글 수정
@Transactional(rollbackFor = Exception.class)
public BaseResponse<String> editComment(Long commentIdx, CommentEditRequest commentEditRequest) throws BaseException {
try {
Comment comment = commentRepository.findById(commentIdx).orElseThrow(() -> new BaseException(INVALID_COMMENT_IDX));
User writer = userRepository.findByUserIdx(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
validateWriter(writer, comment);

if (commentEditRequest.content() != null) {
if (!commentEditRequest.content().equals("") && !commentEditRequest.content().equals(" "))
comment.modifyContent(commentEditRequest.content());
else throw new BaseException(BLANK_COMMENT_CONTENT);
}
commentRepository.save(comment);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

// [작성자] 댓글 삭제
public BaseResponse<String> deleteComment(Long commentIdx) throws BaseException {
try {
Comment comment = commentRepository.findById(commentIdx).orElseThrow(() -> new BaseException(INVALID_COMMENT_IDX));
User writer = userRepository.findByUserIdx(userService.getUserIdxWithValidation()).orElseThrow(() -> new BaseException(INVALID_USER_IDX));
validateWriter(writer, comment);

comment.delete();
commentRepository.save(comment);
return new BaseResponse<>(SUCCESS);
} catch (BaseException e) {
throw e;
} catch (Exception e) {
throw new BaseException(INTERNAL_SERVER_ERROR);
}
}

private static void validateWriter(User user, Comment comment) throws BaseException {
if (!comment.getWriter().equals(user)) throw new BaseException(NO_COMMENT_WRITER);
if (comment.getStatus().equals(INACTIVE)) throw new BaseException(ALREADY_DELETED_COMMENT);
}
}
10 changes: 10 additions & 0 deletions src/main/java/com/hatcher/haemo/comment/domain/Comment.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;

import static com.hatcher.haemo.common.constants.Constant.INACTIVE;

@Entity
@Getter
@DynamicInsert
Expand Down Expand Up @@ -47,4 +49,12 @@ public void setWriter(User writer) {
this.writer = writer;
writer.getComments().add(this);
}

public void modifyContent(String content) {
this.content = content;
}

public void delete() {
this.setStatus(INACTIVE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.hatcher.haemo.comment.dto;

public record CommentEditRequest(String content) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.hatcher.haemo.comment.dto;

public record CommentPostRequest(Long recruitmentIdx,
String content) {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hatcher.haemo.comment.presentation;

import com.hatcher.haemo.comment.application.CommentService;
import com.hatcher.haemo.comment.dto.CommentEditRequest;
import com.hatcher.haemo.comment.dto.CommentPostRequest;
import com.hatcher.haemo.common.BaseResponse;
import com.hatcher.haemo.common.exception.BaseException;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import static com.hatcher.haemo.common.constants.RequestURI.comment;

@RestController
@RequiredArgsConstructor
@RequestMapping(comment)
public class CommentController {

private final CommentService commentService;

// 댓글 등록
@PostMapping("")
public BaseResponse<String> postComment(@RequestBody CommentPostRequest commentPostRequest) {
try {
return commentService.postComment(commentPostRequest);
} catch(BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

// 댓글 수정
@PatchMapping("/{commentIdx}/edit")
public BaseResponse<String> editComment(@PathVariable Long commentIdx, @RequestBody CommentEditRequest commentEditRequest) {
try {
return commentService.editComment(commentIdx, commentEditRequest);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}

@PatchMapping("/{commentIdx}/delete")
public BaseResponse<String> deleteComment(@PathVariable Long commentIdx) {
try {
return commentService.deleteComment(commentIdx);
} catch (BaseException e) {
return new BaseResponse<>(e.getStatus());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ public class RequestURI {
public final static String user = "/users";
public final static String recruitment = "/recruitments";
public final static String notification = "/notifications";
public final static String comment = "/comments";
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ public enum BaseResponseStatus {
NOT_MEMBER_ROLE(false, HttpStatus.BAD_REQUEST, "해당 띱의 멤버가 아닙니다."),

// comment(2200-2299)
INVALID_COMMENT_IDX(false, HttpStatus.NOT_FOUND, "해당 comment idx로 comment를 찾을 수 없습니다."),
NO_COMMENT_WRITER(false, HttpStatus.BAD_REQUEST, "해당 댓글 작성자가 아닙니다."),
ALREADY_DELETED_COMMENT(false, HttpStatus.BAD_REQUEST, "이미 삭제된 댓글입니다."),
BLANK_COMMENT_CONTENT(false, HttpStatus.BAD_REQUEST, "댓글 내용이 비었습니다."),

// notification(2300-2399)
INVALID_NOTIFICATION_IDX(false, HttpStatus.NOT_FOUND, "해당 notification idx로 notification을 찾을 수 없습니다."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ public BaseResponse<RecruitResponse> getRecruitment(Long recruitmentIdx) throws
isLeader, recruitment.getStatus().equals(RECRUITING));
Integer commentCount = recruitment.getComments().size();
List<CommentDto> commentList = recruitment.getComments().stream()
.filter(comment -> comment.getStatus().equals(ACTIVE))
.map(comment -> new CommentDto(comment.getCommentIdx(), comment.getWriter().getNickname(), comment.getCreatedDate(), comment.getContent())).toList();

RecruitResponse recruitResponse = new RecruitResponse(recruitmentDetailDto, commentCount, commentList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.Where;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -45,11 +44,9 @@ public class Recruitment extends BaseEntity {
private String description;

@OneToMany(mappedBy = "recruitment")
@Where(clause = "status = 'ACTIVE'")
private List<Comment> comments = new ArrayList<>();

@OneToMany(mappedBy = "recruitment")
@Where(clause = "status = 'ACTIVE'")
private List<Participant> participants = new ArrayList<>(); // 띱을 나가도 이 리스트에는 있고 participant inactive 처리

@Builder
Expand Down
Loading