Skip to content

Commit

Permalink
Merge pull request #38 from YogitTeam/feat/board
Browse files Browse the repository at this point in the history
Feat Comment, ClipBoard 관련 API구현, 리팩토링 완료
  • Loading branch information
xhaktmchl authored Nov 8, 2022
2 parents d70bd54 + 26a869b commit a37a8ce
Show file tree
Hide file tree
Showing 10 changed files with 193 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class BoardController {
* 게시글 등록
* @author 토마스
*/
@ApiOperation(value = "게시글 등록", notes = "그룹 게시글에 필요한 정보를 입력받아 게시글 생성.")
@ApiOperation(value = "게시글 등록", notes = "그룹 게시글에 필요한 정보를 입력받아 게시글 생성. , swagger 에서 이미지(multipartfile)처리가 잘 되지 않으므로, postman으로 테스트 바랍니다. https://solar-desert-882435.postman.co/workspace/Yogit~3e0fe8f2-15e0-41c4-9fcd-b614a975c12a/request/23528495-7fcef771-fae5-486b-a423-b47daf2d0514")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.yogit.server.board.controller;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.*;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
import com.yogit.server.board.dto.response.clipboard.GetClipBoardRes;
import com.yogit.server.board.service.clipboard.ClipBoardService;
Expand All @@ -13,10 +11,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

Expand Down Expand Up @@ -70,7 +65,40 @@ public ApplicationResponse<GetClipBoardRes> findClipBoard(@RequestBody @Validate
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PostMapping("/get/all")
public ApplicationResponse<List<ClipBoardRes>> findAllClipBoards(@RequestBody @Validated GetAllClipBoardsReq getAllClipBoardsReq){
public ApplicationResponse<List<GetClipBoardRes>> findAllClipBoards(@RequestBody @Validated GetAllClipBoardsReq getAllClipBoardsReq){
return clipBoardService.findAllClipBoards(getAllClipBoardsReq);
}


/**
* 클립보드 수정
* @author 토마스
*/
@ApiOperation(value = "클립보드 수정", notes = "클립보드 ID, title, content로 클립보드 수정 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 클립보드입니다."),
@ApiResponse(code= 404, message = "요청한 유저가 클립보드의 호스트가 아닙니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PatchMapping("/{clipBoardId}")
public ApplicationResponse<ClipBoardRes> updateClipBoard(@PathVariable("clipBoardId") Long clipBoardid, @RequestBody @Validated PatchClipBoardReq patchClipBoardReq){
return clipBoardService.updateClipBoard(patchClipBoardReq);
}

/**
* 클립보드 삭제
* @author 토마스
*/
@ApiOperation(value = "클립보드 삭제", notes = "클립보드 ID로 클립보드 삭제 요청.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PatchMapping("/{clipBoardId}/status")
public ApplicationResponse<ClipBoardRes> deleteClipBoard(@PathVariable("clipBoardId") Long clipBoardId, @RequestBody @Validated DeleteClipBoardReq deleteClipBoardReq){
return clipBoardService.deleteClipBoard(deleteClipBoardReq);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.yogit.server.board.dto.request.clipboard;

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

@Data
@NoArgsConstructor
public class DeleteClipBoardReq {

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

@ApiModelProperty(example = "1")
@ApiParam(value = "clipBoard ID", required = true)
private Long clipBoardId;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.yogit.server.board.dto.request.clipboard;

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 PatchClipBoardReq {

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

@ApiModelProperty(example = "1")
@ApiParam(value = "clipBoard ID", required = true)
private Long clipBoardId;

@ApiModelProperty(example = "질문이 있습니다.")
@ApiParam(value = "클립보드 제목", required = true)
@NotBlank
private String title;

@ApiModelProperty(example = "경복궁역 몇 번 출구인가요?")
@ApiParam(value = "클립보드 상세 내용", required = false)
@NotBlank
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ public class GetClipBoardRes {
@ApiParam(value = "유저 ID")
private Long userId;

@ApiModelProperty(example = "Park jun")
@ApiParam(value = "유저 이름")
private String userName;

@ApiModelProperty(example = "추가 예정")
@ApiParam(value = "유저 프로필 이미지 url")
private String profileImgUrl;

@ApiModelProperty(example = "1")
@ApiParam(value = "Board ID")
private Long boardId;
Expand All @@ -40,6 +48,10 @@ public class GetClipBoardRes {
@ApiParam(value = "코멘트들")
private List<CommentRes> commentResList;

@ApiModelProperty(example = "2")
@ApiParam(value = "클립보드에 달린 코멘트 갯수")
private Integer commentCnt;

@ApiModelProperty(example = "ACTIVE")
@ApiParam(value = "객체 상태")
private BaseStatus status;
Expand All @@ -52,29 +64,34 @@ public class GetClipBoardRes {
@ApiParam(value = "마지막 업데이트 시각")
private String updatedAt;

public static GetClipBoardRes toDto(ClipBoard clipBoard, List<CommentRes> commentResList){
public static GetClipBoardRes toDto(ClipBoard clipBoard, List<CommentRes> commentResList, String profileImgUrl){
return GetClipBoardRes.builder()
.clipBoardId(clipBoard.getId())
.userId(clipBoard.getUser().getId())
.userName(clipBoard.getUser().getName())
.profileImgUrl(profileImgUrl)
.boardId(clipBoard.getBoard().getId())
.title(clipBoard.getTitle())
.content(clipBoard.getContent())
.commentResList(commentResList)
.commentCnt(commentResList.size())
.status(clipBoard.getStatus())
.createdAt(clipBoard.getCreatedAt())
.updatedAt(clipBoard.getUpdatedAt())
.build();
}

@Builder

public GetClipBoardRes(Long clipBoardId, Long userId, Long boardId, String title, String content, List<CommentRes> commentResList, BaseStatus status, String createdAt, String updatedAt) {
public GetClipBoardRes(Long clipBoardId, Long userId, String userName, String profileImgUrl, Long boardId, String title, String content, List<CommentRes> commentResList, Integer commentCnt, BaseStatus status, String createdAt, String updatedAt) {
this.clipBoardId = clipBoardId;
this.userId = userId;
this.userName = userName;
this.profileImgUrl = profileImgUrl;
this.boardId = boardId;
this.title = title;
this.content = content;
this.commentResList = commentResList;
this.commentCnt = commentCnt;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
Expand Down
14 changes: 14 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/ClipBoard.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.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.PatchClipBoardReq;
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.Builder;
Expand Down Expand Up @@ -41,4 +43,16 @@ public ClipBoard(CreateClipBoardReq dto, User user, Board board) {
this.title = dto.getTitle();
this.content = dto.getContent();
}

/*
연관관계 편의 메서드
*/
public void deleteClipBoard(){
this.setStatus(BaseStatus.INACTIVE);
}

public void updateClipBoard(PatchClipBoardReq dto){
this.title = dto.getTitle();
this.content = dto.getContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@RequiredArgsConstructor
public enum ClipBoardExceptionList {

NOT_FOUND_CLIP_BOARD("CB0001", NOT_FOUND, "존재하지 않는 ClipBoard아이디입니다.");
NOT_FOUND_CLIP_BOARD("CB0001", NOT_FOUND, "존재하지 않는 ClipBoard아이디입니다."),
NOT_USER_OF_CLIPBOARD("CB0002", 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,8 @@
package com.yogit.server.board.exception.clipboard;

public class NotUserOfClipBoardException extends ClipBoardException{
public NotUserOfClipBoardException(){
super(ClipBoardExceptionList.NOT_USER_OF_CLIPBOARD.getCODE(), ClipBoardExceptionList.NOT_USER_OF_CLIPBOARD.getHTTPSTATUS(), ClipBoardExceptionList.NOT_USER_OF_CLIPBOARD.getMESSAGE());
}

}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.yogit.server.board.service.clipboard;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.*;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
import com.yogit.server.board.dto.response.clipboard.GetClipBoardRes;
import com.yogit.server.global.dto.ApplicationResponse;
Expand All @@ -15,5 +13,9 @@ public interface ClipBoardService {

ApplicationResponse<GetClipBoardRes> findClipBoard(GetClipBoardReq getClipBoardReq);

ApplicationResponse<List<ClipBoardRes>> findAllClipBoards(GetAllClipBoardsReq getAllClipBoardsReq);
ApplicationResponse<List<GetClipBoardRes>> findAllClipBoards(GetAllClipBoardsReq getAllClipBoardsReq);

ApplicationResponse<ClipBoardRes> deleteClipBoard(DeleteClipBoardReq deleteClipBoardReq);

ApplicationResponse<ClipBoardRes> updateClipBoard(PatchClipBoardReq patchClipBoardReq);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.yogit.server.board.service.clipboard;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.*;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
import com.yogit.server.board.dto.response.clipboard.GetClipBoardRes;
import com.yogit.server.board.dto.response.comment.CommentRes;
Expand All @@ -11,11 +9,13 @@
import com.yogit.server.board.entity.Comment;
import com.yogit.server.board.exception.NotFoundBoardException;
import com.yogit.server.board.exception.clipboard.NotFoundClipBoardException;
import com.yogit.server.board.exception.clipboard.NotUserOfClipBoardException;
import com.yogit.server.board.exception.comment.NotFoundCommentException;
import com.yogit.server.board.repository.BoardRepository;
import com.yogit.server.board.repository.ClipBoardRepository;
import com.yogit.server.board.repository.CommentRepository;
import com.yogit.server.global.dto.ApplicationResponse;
import com.yogit.server.s3.AwsS3Service;
import com.yogit.server.user.entity.User;
import com.yogit.server.user.exception.NotFoundUserException;
import com.yogit.server.user.repository.UserRepository;
Expand All @@ -35,6 +35,7 @@ public class ClipBoardServiceImpl implements ClipBoardService{
private final UserRepository userRepository;
private final BoardRepository boardRepository;
private final CommentRepository commentRepository;
private final AwsS3Service awsS3Service;

@Transactional(readOnly = false)
@Override
Expand Down Expand Up @@ -71,26 +72,73 @@ public ApplicationResponse<GetClipBoardRes> findClipBoard(GetClipBoardReq dto){
.map(comment -> CommentRes.toDto(comment))
.collect(Collectors.toList());

String profileImgUrl = awsS3Service.makeUrlOfFilename(user.getProfileImg());// 유저 프로필 사진 multipart -> url 로 변환

// 코멘트 추가
GetClipBoardRes getClipBoardRes = GetClipBoardRes.toDto(clipBoard, comments);
GetClipBoardRes getClipBoardRes = GetClipBoardRes.toDto(clipBoard, comments, profileImgUrl);
return ApplicationResponse.ok(getClipBoardRes);
}


@Transactional(readOnly = true)
@Override
public ApplicationResponse<List<ClipBoardRes>> findAllClipBoards(GetAllClipBoardsReq dto){
public ApplicationResponse<List<GetClipBoardRes>> findAllClipBoards(GetAllClipBoardsReq dto){

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

Board board = boardRepository.findBoardById(dto.getBoardId())
.orElseThrow(() -> new NotFoundBoardException());

List<ClipBoardRes> clipBoardResList = clipBoardRepository.findAllByBoardId(dto.getBoardId()).stream()
.map(clipBoard -> ClipBoardRes.toDto(clipBoard))
// 클립보드 res안에 해당하는 코멘트 리스트까지 조회 및 포함
// 유저 profileImgUrl 또한 img uuid -> s3 url로 변환
List<GetClipBoardRes> getClipBoardResList = clipBoardRepository.findAllByBoardId(dto.getBoardId()).stream()
.map(clipBoard -> GetClipBoardRes.toDto(clipBoard, commentRepository.findAllCommentsByClipBoardId(clipBoard.getId()).stream()
.map(comment -> CommentRes.toDto(comment))
.collect(Collectors.toList()),
awsS3Service.makeUrlOfFilename(clipBoard.getUser().getProfileImg())))
.collect(Collectors.toList());

return ApplicationResponse.ok(clipBoardResList);
return ApplicationResponse.ok(getClipBoardResList);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<ClipBoardRes> updateClipBoard(PatchClipBoardReq dto){
//필요 객체 조회
User user = userRepository.findById(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());

ClipBoard clipBoard = clipBoardRepository.findClipBoardById(dto.getClipBoardId())
.orElseThrow(() -> new NotFoundClipBoardException());

//Validation: 요청 사용자와 클립보드 생성자가 같은지 검증
if(!user.getId().equals(clipBoard.getUser().getId())){
throw new NotUserOfClipBoardException();
}

clipBoard.updateClipBoard(dto); // 업데이트
ClipBoardRes clipBoardRes = ClipBoardRes.toDto(clipBoard);
return ApplicationResponse.ok(clipBoardRes);
}

@Transactional(readOnly = false)
@Override
public ApplicationResponse<ClipBoardRes> deleteClipBoard(DeleteClipBoardReq dto){
User user = userRepository.findById(dto.getUserId())
.orElseThrow(() -> new NotFoundUserException());

ClipBoard clipBoard = clipBoardRepository.findClipBoardById(dto.getClipBoardId())
.orElseThrow(() -> new NotFoundClipBoardException());

//Validation: 요청 사용자와 클립보드 생성자가 같은지 검증
if(!user.getId().equals(clipBoard.getUser().getId())){
throw new NotUserOfClipBoardException();
}

clipBoard.deleteClipBoard();// 삭제
ClipBoardRes clipBoardRes = ClipBoardRes.toDto(clipBoard);
return ApplicationResponse.ok(clipBoardRes);
}
}

0 comments on commit a37a8ce

Please sign in to comment.