From c2933061ef0c28c6c5883e00c800b179e01af10d Mon Sep 17 00:00:00 2001 From: xhaktmchl Date: Wed, 19 Oct 2022 17:19:21 +0900 Subject: [PATCH] =?UTF-8?q?#9=20feat:=20BoardImage=EC=82=AD=EC=A0=9C=20API?= =?UTF-8?q?=EA=B5=AC=ED=98=84,=20feat:=20BoadImage=20Exception=EA=B5=AC?= =?UTF-8?q?=EC=A1=B0=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../board/controller/BoardController.java | 19 +++++++++ .../boardimage/DeleteBoardImageReq.java | 23 +++++++++++ .../boardimage/DeleteBoardImageRes.java | 40 +++++++++++++++++++ .../yogit/server/board/entity/BoardImage.java | 8 ++++ .../boardimage/BoardImageExcepionList.java | 18 +++++++++ .../boardimage/BoardImageExeption.java | 11 +++++ .../NotFoundBoardImageException.java | 8 ++++ .../repository/BoardImageRepository.java | 8 ++++ .../server/board/service/BoardService.java | 4 ++ .../board/service/BoardServiceImpl.java | 27 +++++++++++++ 10 files changed, 166 insertions(+) create mode 100644 server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageReq.java create mode 100644 server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageRes.java create mode 100644 server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExcepionList.java create mode 100644 server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExeption.java create mode 100644 server/src/main/java/com/yogit/server/board/exception/boardimage/NotFoundBoardImageException.java diff --git a/server/src/main/java/com/yogit/server/board/controller/BoardController.java b/server/src/main/java/com/yogit/server/board/controller/BoardController.java index f10b32a..e377423 100644 --- a/server/src/main/java/com/yogit/server/board/controller/BoardController.java +++ b/server/src/main/java/com/yogit/server/board/controller/BoardController.java @@ -2,7 +2,10 @@ import com.yogit.server.board.dto.request.*; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes; import com.yogit.server.board.dto.response.BoardRes; +import com.yogit.server.board.entity.Board; import com.yogit.server.board.service.BoardService; import com.yogit.server.global.dto.ApplicationResponse; import io.swagger.annotations.ApiOperation; @@ -107,4 +110,20 @@ public ApplicationResponse> findAllBoards(@RequestBody @Validated public ApplicationResponse findBoard(@RequestBody @Validated GetBoardReq getBoardReq){ return boardService.findBoard(getBoardReq); } + + + /** + * 게시글 이미지 삭제 + * @author 토마스 + */ + @ApiOperation(value = "게시글 이미지 삭제", notes = "게시물 이미지 url을 입력해 이미지 삭제 요청.") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @PatchMapping("/boardimage") + public ApplicationResponse deleteBoardImage(@RequestBody @Validated DeleteBoardImageReq deleteBoardImageReq){ + return boardService.deleteBoardImage(deleteBoardImageReq); + } } diff --git a/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageReq.java b/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageReq.java new file mode 100644 index 0000000..fff44e3 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageReq.java @@ -0,0 +1,23 @@ +package com.yogit.server.board.dto.request.boardimage; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class DeleteBoardImageReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "Board ID", required = true) + private Long boardId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "유저 ID", required = true) + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "BoardImage ID", required = true) + private Long boardImageId; +} diff --git a/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageRes.java b/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageRes.java new file mode 100644 index 0000000..45791eb --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/boardimage/DeleteBoardImageRes.java @@ -0,0 +1,40 @@ +package com.yogit.server.board.dto.request.boardimage; + +import com.yogit.server.board.entity.BoardImage; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class DeleteBoardImageRes { + + @ApiModelProperty(example = "1") + @ApiParam(value = "게시글 이미지 id") + private Long boardImageId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "게시글 id") + private Long boardId; + + @ApiModelProperty(example = "예제 넣을 예정") + @ApiParam(value = "게시글 이미지 url") + private String imgUrl; + + @Builder + public DeleteBoardImageRes(Long boardImageId, Long boardId, String imgUrl) { + this.boardImageId = boardImageId; + this.boardId = boardId; + this.imgUrl = imgUrl; + } + + public static DeleteBoardImageRes toDto(BoardImage boardImage, String imgUrl) { + return DeleteBoardImageRes.builder() + .boardImageId(boardImage.getId()) + .boardId(boardImage.getBoard().getId()) + .imgUrl(imgUrl) + .build(); + } +} diff --git a/server/src/main/java/com/yogit/server/board/entity/BoardImage.java b/server/src/main/java/com/yogit/server/board/entity/BoardImage.java index 3c7bb73..60f0ab4 100644 --- a/server/src/main/java/com/yogit/server/board/entity/BoardImage.java +++ b/server/src/main/java/com/yogit/server/board/entity/BoardImage.java @@ -1,6 +1,7 @@ package com.yogit.server.board.entity; import com.yogit.server.config.domain.BaseEntity; +import com.yogit.server.config.domain.BaseStatus; import lombok.AccessLevel; import lombok.Builder; import lombok.Getter; @@ -29,4 +30,11 @@ public BoardImage(Board board, String imgUUid) { board.addBoardImage(this); this.imgUUid = imgUUid; } + + /* + 연관관계 편의 메서드 + */ + public void deleteBoardImage(){ + this.setStatus(BaseStatus.INACTIVE); + } } diff --git a/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExcepionList.java b/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExcepionList.java new file mode 100644 index 0000000..f4e0006 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExcepionList.java @@ -0,0 +1,18 @@ +package com.yogit.server.board.exception.boardimage; + +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.springframework.http.HttpStatus; + +import static org.springframework.http.HttpStatus.NOT_FOUND; + +@Getter +@RequiredArgsConstructor +public enum BoardImageExcepionList { + + NOT_FOUND_BOARDIMAGE("BI0001", NOT_FOUND,"존재하지 않는 BoardImage아이디입니다."); + + private final String CODE; + private final HttpStatus HTTPSTATUS; + private final String MESSAGE; +} diff --git a/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExeption.java b/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExeption.java new file mode 100644 index 0000000..ca9521d --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/boardimage/BoardImageExeption.java @@ -0,0 +1,11 @@ +package com.yogit.server.board.exception.boardimage; + +import com.yogit.server.global.exception.ApplicationException; +import org.springframework.http.HttpStatus; + +public class BoardImageExeption extends ApplicationException { + + protected BoardImageExeption (String errorCode, HttpStatus httpStatus, String message) { + super(errorCode, httpStatus, message); + } +} diff --git a/server/src/main/java/com/yogit/server/board/exception/boardimage/NotFoundBoardImageException.java b/server/src/main/java/com/yogit/server/board/exception/boardimage/NotFoundBoardImageException.java new file mode 100644 index 0000000..fc7a9db --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/boardimage/NotFoundBoardImageException.java @@ -0,0 +1,8 @@ +package com.yogit.server.board.exception.boardimage; + +public class NotFoundBoardImageException extends BoardImageExeption{ + + public NotFoundBoardImageException(){ + super(BoardImageExcepionList.NOT_FOUND_BOARDIMAGE.getCODE(), BoardImageExcepionList.NOT_FOUND_BOARDIMAGE.getHTTPSTATUS(), BoardImageExcepionList.NOT_FOUND_BOARDIMAGE.getMESSAGE()); + } +} diff --git a/server/src/main/java/com/yogit/server/board/repository/BoardImageRepository.java b/server/src/main/java/com/yogit/server/board/repository/BoardImageRepository.java index 81d5a09..541e579 100644 --- a/server/src/main/java/com/yogit/server/board/repository/BoardImageRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/BoardImageRepository.java @@ -2,8 +2,16 @@ import com.yogit.server.board.entity.BoardImage; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.Optional; + @Repository public interface BoardImageRepository extends JpaRepository { + + @Query("select bi from BoardImage bi where bi.id=:boardImageId and bi.status='ACTIVE'") + Optional findBoardImageById(@Param("boardImageId") Long boardImageId); + } diff --git a/server/src/main/java/com/yogit/server/board/service/BoardService.java b/server/src/main/java/com/yogit/server/board/service/BoardService.java index 4899ce1..14c9d0f 100644 --- a/server/src/main/java/com/yogit/server/board/service/BoardService.java +++ b/server/src/main/java/com/yogit/server/board/service/BoardService.java @@ -1,6 +1,8 @@ package com.yogit.server.board.service; import com.yogit.server.board.dto.request.*; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes; import com.yogit.server.board.dto.response.BoardRes; import com.yogit.server.global.dto.ApplicationResponse; @@ -17,4 +19,6 @@ public interface BoardService { ApplicationResponse> findAllBoards(GetAllBoardsReq getAllBoardsReq); ApplicationResponse findBoard(GetBoardReq getBoardReq); + + ApplicationResponse deleteBoardImage(DeleteBoardImageReq deleteBoardImageReq); } diff --git a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java index d22d9bc..7ed0056 100644 --- a/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java +++ b/server/src/main/java/com/yogit/server/board/service/BoardServiceImpl.java @@ -1,6 +1,8 @@ package com.yogit.server.board.service; import com.yogit.server.board.dto.request.*; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageReq; +import com.yogit.server.board.dto.request.boardimage.DeleteBoardImageRes; import com.yogit.server.board.dto.response.BoardRes; import com.yogit.server.board.entity.Board; import com.yogit.server.board.entity.BoardImage; @@ -9,6 +11,7 @@ import com.yogit.server.board.exception.NotFoundBoardException; import com.yogit.server.board.exception.NotHostOfBoardExcepion; import com.yogit.server.board.exception.boardCategory.NotFoundCategoryException; +import com.yogit.server.board.exception.boardimage.NotFoundBoardImageException; import com.yogit.server.board.repository.BoardImageRepository; import com.yogit.server.board.repository.CategoryRepository; import com.yogit.server.board.repository.BoardRepository; @@ -176,4 +179,28 @@ public ApplicationResponse findBoard(GetBoardReq dto){ return ApplicationResponse.ok(boardRes); } + + @Transactional(readOnly = false) + @Override + public ApplicationResponse deleteBoardImage(DeleteBoardImageReq dto){ + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + Board board = boardRepository.findBoardById(dto.getBoardId()) + .orElseThrow(() -> new NotFoundBoardException()); + + //validation: 요청자와 host 비교 + if (!board.getHost().equals(user)) { + throw new NotHostOfBoardExcepion(); + } + + BoardImage boardImage = boardImageRepository.findBoardImageById(dto.getBoardImageId()) + .orElseThrow(() -> new NotFoundBoardImageException()); + String imgUrl = awsS3Service.makeUrlOfFilename(boardImage.getImgUUid()); + + boardImage.deleteBoardImage(); + DeleteBoardImageRes res = DeleteBoardImageRes.toDto(boardImage, imgUrl); + return ApplicationResponse.ok(res); + } + }