Skip to content

Commit

Permalink
#9 feat: BoardImage삭제 API구현, feat: BoadImage Exception구조 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
xhaktmchl committed Oct 19, 2022
1 parent e4d9d24 commit c293306
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,4 +110,20 @@ public ApplicationResponse<List<BoardRes>> findAllBoards(@RequestBody @Validated
public ApplicationResponse<BoardRes> 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<DeleteBoardImageRes> deleteBoardImage(@RequestBody @Validated DeleteBoardImageReq deleteBoardImageReq){
return boardService.deleteBoardImage(deleteBoardImageReq);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,4 +30,11 @@ public BoardImage(Board board, String imgUUid) {
board.addBoardImage(this);
this.imgUUid = imgUUid;
}

/*
연관관계 편의 메서드
*/
public void deleteBoardImage(){
this.setStatus(BaseStatus.INACTIVE);
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<BoardImage, Long> {

@Query("select bi from BoardImage bi where bi.id=:boardImageId and bi.status='ACTIVE'")
Optional<BoardImage> findBoardImageById(@Param("boardImageId") Long boardImageId);

}
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -17,4 +19,6 @@ public interface BoardService {
ApplicationResponse<List<BoardRes>> findAllBoards(GetAllBoardsReq getAllBoardsReq);

ApplicationResponse<BoardRes> findBoard(GetBoardReq getBoardReq);

ApplicationResponse<DeleteBoardImageRes> deleteBoardImage(DeleteBoardImageReq deleteBoardImageReq);
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -176,4 +179,28 @@ public ApplicationResponse<BoardRes> findBoard(GetBoardReq dto){
return ApplicationResponse.ok(boardRes);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<DeleteBoardImageRes> 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);
}

}

0 comments on commit c293306

Please sign in to comment.