Skip to content

Commit

Permalink
Merge pull request #19 from YogitTeam/feat/board
Browse files Browse the repository at this point in the history
#9 feat: Board 이미지 업로드
  • Loading branch information
xhaktmchl authored Oct 19, 2022
2 parents d8b4104 + 1f42549 commit 0376c3e
Show file tree
Hide file tree
Showing 14 changed files with 252 additions and 11 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
Expand Up @@ -10,10 +10,12 @@
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -74,6 +76,10 @@ public class CreateBoardReq {
@ApiParam(value = "그룹 카테고리 ID", required = true)
private Long categoryId;

@ApiModelProperty(example = "\"https://yogit.s3.ap-northeast-2.amazonaws.com/boardimguuid2\"")
@ApiParam(value = "게시글 이미지 데이터", required = false)
private List<MultipartFile> images; // 최대 5개

// public Board toEntity(CreateBoardReq dto, User host){
// return Board.builder()
// .title(dto.getTitle())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -72,4 +74,8 @@ public class PatchBoardReq {
@ApiModelProperty(example = "1")
@ApiParam(value = "그룹 카테고리 ID", required = false)
private Long categoryId;

@ApiModelProperty(example = "\"https://yogit.s3.ap-northeast-2.amazonaws.com/boardimguuid2\"")
@ApiParam(value = "게시글 이미지 데이터", required = false)
private List<MultipartFile> images; // 최대 5개
}
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
Expand Up @@ -9,6 +9,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -69,6 +70,10 @@ public class BoardRes {
@ApiParam(value = "모임 카테고리 ID")
private Long categoryId;

@ApiModelProperty(example = "이미지 url 리스트 예제 넣을 예정")
@ApiParam(value = "게시글 이미지 url 리스트")
private List<String> imageUrls;

@ApiModelProperty(example = "ACTIVE")
@ApiParam(value = "객체 상태")
private BaseStatus status;
Expand All @@ -82,7 +87,7 @@ public class BoardRes {
private String updatedAt;


public static BoardRes toDto(Board board){
public static BoardRes toDto(Board board, List<String> imageUrls){
return BoardRes.builder()
.boardId(board.getId())
.cityId(board.getCity().getId())
Expand All @@ -98,14 +103,15 @@ public static BoardRes toDto(Board board){
.currentMember(board.getCurrentMember())
.totalMember(board.getTotalMember())
.categoryId(board.getCategory().getId())
.imageUrls(imageUrls)
.status(board.getStatus())
.createdAt(board.getCreatedAt())
.updatedAt(board.getUpdatedAt())
.build();
}

@Builder
public BoardRes(Long boardId, Long cityId, Long hostId, String title, String address, float longitute, float latitude, LocalDateTime date, String notice, String introduction, String kindOfPerson, int currentMember, int totalMember, Long categoryId, BaseStatus status, String createdAt, String updatedAt) {
public BoardRes(Long boardId, Long cityId, Long hostId, String title, String address, float longitute, float latitude, LocalDateTime date, String notice, String introduction, String kindOfPerson, int currentMember, int totalMember, Long categoryId, List<String> imageUrls, BaseStatus status, String createdAt, String updatedAt) {
this.boardId = boardId;
this.cityId = cityId;
this.hostId = hostId;
Expand All @@ -120,6 +126,7 @@ public BoardRes(Long boardId, Long cityId, Long hostId, String title, String add
this.currentMember = currentMember;
this.totalMember = totalMember;
this.categoryId = categoryId;
this.imageUrls = imageUrls;
this.status = status;
this.createdAt = createdAt;
this.updatedAt = updatedAt;
Expand Down
12 changes: 12 additions & 0 deletions server/src/main/java/com/yogit/server/board/entity/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Entity
@Getter
Expand Down Expand Up @@ -128,6 +129,10 @@ public void addBoardUser(BoardUser boardUser){
this.boardUsers.add(boardUser);
}

public void addBoardImage(BoardImage boardImage){
this.boardImages.add(boardImage);
}

public void updateBoard(PatchBoardReq dto, City city, Category category){
this.city = city;
this.title = dto.getTitle();
Expand All @@ -146,4 +151,11 @@ public void updateBoard(PatchBoardReq dto, City city, Category category){
public void deleteBoard(){
this.setStatus(BaseStatus.INACTIVE);
}

// BoardImage리스트의 UUID리스트만 String으로 반환
public List<String> getBoardImagesUUids(){
return this.boardImages.stream()
.map(boardImage -> boardImage.getImgUUid())
.collect(Collectors.toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
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;
import lombok.NoArgsConstructor;

Expand All @@ -20,5 +22,19 @@ public class BoardImage extends BaseEntity {
@JoinColumn(name = "board_id")
private Board board;

private String imgUrl;
private String imgUUid;

@Builder
public BoardImage(Board board, String imgUUid) {
this.board = board;
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
@@ -0,0 +1,17 @@
package com.yogit.server.board.repository;

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);
}
Loading

0 comments on commit 0376c3e

Please sign in to comment.