Skip to content

Commit

Permalink
Merge pull request #29 from YogitTeam/feat/board
Browse files Browse the repository at this point in the history
Feat/board
  • Loading branch information
xhaktmchl authored Nov 3, 2022
2 parents 555ed51 + 3ad4651 commit 46e2bd1
Show file tree
Hide file tree
Showing 11 changed files with 237 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.yogit.server.board.controller;

import com.yogit.server.board.dto.request.comment.CreateCommentReq;
import com.yogit.server.board.dto.request.comment.DeleteCommentReq;
import com.yogit.server.board.dto.request.comment.PatchCommentReq;
import com.yogit.server.board.dto.response.comment.CommentRes;
import com.yogit.server.board.dto.response.comment.DeleteCommentRes;
import com.yogit.server.board.service.comment.CommentService;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.user.entity.Gender;
Expand Down Expand Up @@ -57,4 +60,48 @@ public ApplicationResponse<CommentRes> createComment(@RequestBody @Validated Cre
public ApplicationResponse<List<CommentRes>> findAllComments(@PathVariable("clipBoardId") Long clipBoardId, @PathVariable("userId") Long userId){
return commentService.findAllComments(clipBoardId, userId);
}


/**
* 코멘트 삭제
* @author 토마스
*/
@ApiOperation(value = "코멘트 삭제", notes = "코멘트 아이디를 입력해 코멘트 삭제 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 코멘트입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"),
@ApiImplicitParam(name = "commentId", required = true, dataTypeClass = Long.class, example = "1")
})
@PatchMapping("/{commentId}")
public ApplicationResponse<DeleteCommentRes> deleteComment(@PathVariable("commentId") Long commentId, @RequestBody @Validated DeleteCommentReq deleteCommentReq){
return commentService.deleteComment(deleteCommentReq, commentId);
}


/**
* 코멘트 수정
* @author 토마스
*/
@ApiOperation(value = "코멘트 수정", notes = "코멘트 아이디, 수정할 내용을 입력해 코멘트 수정 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 코멘트입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@ApiImplicitParams({
@ApiImplicitParam(name = "userId", required = true, dataTypeClass = Long.class, example = "1"),
@ApiImplicitParam(name = "commentId", required = true, dataTypeClass = Long.class, example = "1")
})
@PatchMapping("/{commentId}/content")
public ApplicationResponse<CommentRes> updateComment(@PathVariable("commentId") Long commentId, @RequestBody @Validated PatchCommentReq patchCommentReq){
return commentService.updateComment(patchCommentReq, commentId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.yogit.server.board.dto.request.comment;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class DeleteCommentReq {

@ApiModelProperty(example = "1")
@ApiParam(value = "유저 ID", required = true)
private Long userId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.yogit.server.board.dto.request.comment;

import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.validation.constraints.NotBlank;

@Data
@NoArgsConstructor
public class PatchCommentReq {

@ApiModelProperty(example = "1")
@ApiParam(value = "유저 ID", required = true)
private Long userId;

@ApiModelProperty(example = "1")
@ApiParam(value = "코멘트 ID", required = true)
private Long commentId;

@ApiModelProperty(example = "경복궁역 몇 번 출구인가요?")
@ApiParam(value = "클립보드 상세 내용", required = true)
@NotBlank
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.yogit.server.board.entity.ClipBoard;
import com.yogit.server.board.entity.Comment;
import com.yogit.server.config.domain.BaseStatus;
import com.yogit.server.user.entity.User;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
Expand Down Expand Up @@ -37,6 +38,18 @@ public class CommentRes {
@ApiParam(value = "ClipBoard ID")
private Long clipBoardId;

@ApiModelProperty(example = "ACTIVE")
@ApiParam(value = "객체 상태")
private BaseStatus status;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "생성 시각")
private String createdAt;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "마지막 업데이트 시각")
private String updatedAt;

public static CommentRes toDto(Comment comment){
return CommentRes.builder()
.commentId(comment.getId())
Expand All @@ -45,16 +58,22 @@ public static CommentRes toDto(Comment comment){
.userName(comment.getUser().getName())
.profileImg(comment.getUser().getProfileImg())
.clipBoardId(comment.getClipBoard().getId())
.status(comment.getStatus())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.build();
}

@Builder
public CommentRes(Long commentId, String content, Long userId, String userName, String profileImg, Long clipBoardId) {
public CommentRes(Long commentId, String content, Long userId, String userName, String profileImg, Long clipBoardId, BaseStatus status, String createdAt, String updatedAt) {
this.commentId = commentId;
this.content = content;
this.userId = userId;
this.userName = userName;
this.profileImg = profileImg;
this.clipBoardId = clipBoardId;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.yogit.server.board.dto.response.comment;

import com.yogit.server.board.entity.Comment;
import com.yogit.server.config.domain.BaseStatus;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
public class DeleteCommentRes {

@ApiModelProperty(example = "1")
@ApiParam(value = "코멘트 ID")
private Long commentId;

@ApiModelProperty(example = "ACTIVE")
@ApiParam(value = "객체 상태")
private BaseStatus status;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "생성 시각")
private String createdAt;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "마지막 업데이트 시각")
private String updatedAt;

/*
연관관계 편의 메서드
*/
public static DeleteCommentRes toDto(Comment comment){
return DeleteCommentRes.builder()
.commentId(comment.getId())
.status(comment.getStatus())
.createdAt(comment.getCreatedAt())
.updatedAt(comment.getUpdatedAt())
.build();
}

@Builder
public DeleteCommentRes(Long commentId, BaseStatus status, String createdAt, String updatedAt) {
this.commentId = commentId;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
}
}
10 changes: 10 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/Comment.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.yogit.server.board.entity;

import com.yogit.server.board.dto.request.comment.CreateCommentReq;
import com.yogit.server.board.dto.request.comment.PatchCommentReq;
import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.config.domain.BaseStatus;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
Expand Down Expand Up @@ -37,4 +39,12 @@ public Comment(CreateCommentReq dto, User user, ClipBoard clipBoard) {
this.user = user;
this.clipBoard = clipBoard;
}

public void deleteComment(){
this.setStatus(BaseStatus.INACTIVE);
}

public void updateComment(PatchCommentReq dto){
this.content = dto.getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@RequiredArgsConstructor
public enum CommentExceptionList {

NOT_FOUND_COMMENT("CM0001", NOT_FOUND, "존재하지 않는 Comment입니다.");
NOT_FOUND_COMMENT("CM0001", NOT_FOUND, "존재하지 않는 Comment입니다."),
NOT_HOST_OF_COMMENT("CM0002", HttpStatus.BAD_REQUEST, "요청한 유저가 코멘트의 호스트가 아닙니다.");

private final String CODE;
private final HttpStatus HTTPSTATUS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.yogit.server.board.exception.comment;


public class NotHostOfCommentException extends CommentException{
public NotHostOfCommentException(){
super(CommentExceptionList.NOT_FOUND_COMMENT.getCODE(), CommentExceptionList.NOT_FOUND_COMMENT.getHTTPSTATUS(), CommentExceptionList.NOT_FOUND_COMMENT.getMESSAGE());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@
import org.springframework.data.repository.query.Param;

import java.util.List;
import java.util.Optional;

public interface CommentRepository extends JpaRepository<Comment, Long> {

@Query("select cm from Comment cm where cm.clipBoard.id = :clipBoardId and cm.status = 'ACTIVE'")
List<Comment> findAllCommentsByClipBoardId(@Param("clipBoardId") Long clipBoardId);

@Query("select cm from Comment cm where cm.id = :commentId and cm.status='ACTIVE'")
Optional<Comment> findCommentById(@Param("commentId") Long commentId);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.yogit.server.board.service.comment;

import com.yogit.server.board.dto.request.comment.CreateCommentReq;
import com.yogit.server.board.dto.request.comment.DeleteCommentReq;
import com.yogit.server.board.dto.request.comment.PatchCommentReq;
import com.yogit.server.board.dto.response.comment.CommentRes;
import com.yogit.server.board.dto.response.comment.DeleteCommentRes;
import com.yogit.server.global.dto.ApplicationResponse;

import java.util.List;
Expand All @@ -11,4 +14,8 @@ public interface CommentService {
ApplicationResponse<CommentRes> createComment(CreateCommentReq createCommentReq);

ApplicationResponse<List<CommentRes>> findAllComments(Long clipBoardId, Long userId);

ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq deleteCommentReq, Long commentId);

ApplicationResponse<CommentRes> updateComment(PatchCommentReq patchCommentReq, Long commentId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package com.yogit.server.board.service.comment;

import com.yogit.server.board.dto.request.comment.CreateCommentReq;
import com.yogit.server.board.dto.request.comment.DeleteCommentReq;
import com.yogit.server.board.dto.request.comment.PatchCommentReq;
import com.yogit.server.board.dto.response.comment.DeleteCommentRes;
import com.yogit.server.board.dto.response.comment.CommentRes;
import com.yogit.server.board.entity.ClipBoard;
import com.yogit.server.board.entity.Comment;
import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException;
import com.yogit.server.board.exception.comment.NotFoundCommentException;
import com.yogit.server.board.exception.comment.NotHostOfCommentException;
import com.yogit.server.board.repository.ClipBoardRepository;
import com.yogit.server.board.repository.CommentRepository;
import com.yogit.server.global.dto.ApplicationResponse;
Expand Down Expand Up @@ -61,4 +66,46 @@ public ApplicationResponse<List<CommentRes>> findAllComments(Long clipBoardId, L

return ApplicationResponse.ok(commentResList);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<DeleteCommentRes> deleteComment(DeleteCommentReq dto, Long commentId){

User user = userRepository.findById(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());

Comment comment = commentRepository.findCommentById(commentId)
.orElseThrow(() -> new NotFoundCommentException());

//검증: 요청 유저가 코멘트를 생성한 사람인지
if(!user.getId().equals(comment.getUser().getId())){
throw new NotHostOfCommentException();
}

comment.deleteComment();
DeleteCommentRes deleteCommentRes = DeleteCommentRes.toDto(comment);
return ApplicationResponse.ok(deleteCommentRes);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<CommentRes> updateComment(PatchCommentReq dto, Long commentId){

User user = userRepository.findById(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());

Comment comment = commentRepository.findCommentById(dto.getCommentId())
.orElseThrow(() -> new NotFoundCommentException());

//검증: 요청 유저가 코멘트를 생성한 사람인지
if(!user.getId().equals(comment.getUser().getId())){
throw new NotHostOfCommentException();
}

comment.updateComment(dto);
CommentRes commentRes = CommentRes.toDto(comment);
return ApplicationResponse.ok(commentRes);
}
}

0 comments on commit 46e2bd1

Please sign in to comment.