Skip to content

Commit

Permalink
#23 클립보드 삭제API구현(deleteClipBoard)
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Nov 8, 2022
1 parent e504ea6 commit 31ac457
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yogit.server.board.controller;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.DeleteClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
Expand All @@ -13,10 +14,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 @@ -73,4 +71,20 @@ public ApplicationResponse<GetClipBoardRes> findClipBoard(@RequestBody @Validate
public ApplicationResponse<List<GetClipBoardRes>> findAllClipBoards(@RequestBody @Validated GetAllClipBoardsReq getAllClipBoardsReq){
return clipBoardService.findAllClipBoards(getAllClipBoardsReq);
}


/**
* 클립보드 삭제
* @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
Expand Up @@ -2,6 +2,7 @@

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
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 +42,11 @@ public ClipBoard(CreateClipBoardReq dto, User user, Board board) {
this.title = dto.getTitle();
this.content = dto.getContent();
}

/*
연관관계 편의 메서드
*/
public void deleteClipBoard(){
this.setStatus(BaseStatus.INACTIVE);
}
}
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,6 +1,7 @@
package com.yogit.server.board.service.clipboard;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.DeleteClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
Expand All @@ -16,4 +17,6 @@ public interface ClipBoardService {
ApplicationResponse<GetClipBoardRes> findClipBoard(GetClipBoardReq getClipBoardReq);

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

ApplicationResponse<ClipBoardRes> deleteClipBoard(DeleteClipBoardReq deleteClipBoardReq);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.yogit.server.board.service.clipboard;

import com.yogit.server.board.dto.request.clipboard.CreateClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.DeleteClipBoardReq;
import com.yogit.server.board.dto.request.clipboard.GetAllClipBoardsReq;
import com.yogit.server.board.dto.request.clipboard.GetClipBoardReq;
import com.yogit.server.board.dto.response.clipboard.ClipBoardRes;
Expand All @@ -11,6 +12,7 @@
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;
Expand Down Expand Up @@ -96,4 +98,24 @@ public ApplicationResponse<List<GetClipBoardRes>> findAllClipBoards(GetAllClipBo

return ApplicationResponse.ok(getClipBoardResList);
}


@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 31ac457

Please sign in to comment.