Skip to content

Commit

Permalink
#9 feat:모든 엔티티 BaseEntity 상속 / fix: User.status-> userStatus로 변수명 수정/…
Browse files Browse the repository at this point in the history
… feat: Board수정 api 구현 및 호스트 판별 validation 추가
  • Loading branch information
xhaktmchl committed Oct 10, 2022
1 parent 79dc8ea commit 25557ad
Show file tree
Hide file tree
Showing 21 changed files with 197 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@


import com.yogit.server.board.dto.request.CreateBoardReq;
import com.yogit.server.board.dto.request.PatchBoardReq;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.service.BoardServiceImpl;
import com.yogit.server.board.service.BoardService;
import com.yogit.server.global.dto.ApplicationResponse;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
Expand All @@ -12,18 +13,15 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
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.*;

@Slf4j
@RestController
@RequiredArgsConstructor // private final DI의존주입
@RequestMapping("/boards")
public class BoardController {

private final BoardServiceImpl boardServiceImpl;
private final BoardService boardService;

/**
* 게시글 등록
Expand All @@ -36,7 +34,20 @@ public class BoardController {
})
@PostMapping("")
public ApplicationResponse<BoardRes> registerBoard(@RequestBody @Validated CreateBoardReq createBoardReq){
return boardServiceImpl.createBoard(createBoardReq);
return boardService.createBoard(createBoardReq);
}

/**
* 게시글 수정
* @author 토마스
*/
@ApiOperation(value = "게시글 수정", notes = "그룹 게시글에 필요한 정보를 입력받아 게시글 수정.")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PatchMapping("")
public ApplicationResponse<BoardRes> updateBoard(@RequestBody @Validated PatchBoardReq patchBoardReq){
return boardService.updateBoard(patchBoardReq);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.yogit.server.board.dto.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiParam;
import lombok.Data;
import lombok.NoArgsConstructor;

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

@Data
@NoArgsConstructor
public class PatchBoardReq {

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

@ApiModelProperty(example = "1")
@ApiParam(value = "도시 ID", required = true)
private Long cityId;

@ApiModelProperty(example = "1")
@ApiParam(value = "호스트 ID", required = true)
private Long hostId;

@ApiModelProperty(example = "경복궁 탐사입니다.")
@ApiParam(value = "게시글 제목", required = true)
@Size(min = 1, max=51)
@NotBlank
private String title;

@ApiModelProperty(example = "서울특별시 종로구 사직로 130")
@ApiParam(value = "모임 주소", required = true)
@NotBlank
private String address;

@ApiModelProperty(example = "37.1")
@ApiParam(value = "위도", required = true)
private float longitute;

@ApiModelProperty(example = "37")
@ApiParam(value = "경도", required = true)
private float latitude;

@ApiModelProperty(example = "2022-07-13 16:29:30")
@ApiParam(value = "사용자 ID", required = true)
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Seoul")
private LocalDateTime date; // 모임 시각

@ApiModelProperty(example = "시간에 맞춰오시기 바랍니다.")
@ApiParam(value = "모임 공지사항", required = false)
@Size(max = 1000)
private String notice;

@ApiModelProperty(example = "3시에 모여서 경복궁역에서 경복궁으로 출발합니다.")
@ApiParam(value = "모임 상세설명", required = false)
@Size(max = 1000)
private String introduction; // 게시글 내용 상세설명

@ApiModelProperty(example = "활발한 사람이 오면 좋습니다.")
@ApiParam(value = "원하는 사람 설명", required = false)
@Size(max = 1000)
private String kindOfPerson; // 이런 사람을 원합니다 설명 글.

@ApiModelProperty(example = "5")
@ApiParam(value = "총 인원수", required = true)
private int totalMember;

@ApiModelProperty(example = "1")
@ApiParam(value = "그룹 카테고리 ID", required = false)
private Long categoryId;
}
21 changes: 19 additions & 2 deletions server/src/main/java/com/yogit/server/board/entity/Board.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.yogit.server.board.entity;

import com.yogit.server.board.dto.request.CreateBoardReq;
import com.yogit.server.board.dto.request.PatchBoardReq;
import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.City;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
Expand All @@ -16,7 +18,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Board {
public class Board extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_id")
Expand Down Expand Up @@ -54,7 +56,7 @@ public class Board {
private List<BoardUser> boardUsers = new ArrayList<>();

@OneToMany(mappedBy = "board")
private List<BoardImage> boardImages;
private List<BoardImage> boardImages = new ArrayList<>();

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "category_id")
Expand Down Expand Up @@ -123,4 +125,19 @@ public void changeBoardCurrentMember(int currentMember){
public void addBoardUser(BoardUser boardUser){
this.boardUsers.add(boardUser);
}

public void updateBoard(PatchBoardReq dto, City city, Category category){
this.city = city;
this.title = dto.getTitle();
this.address = dto.getAddress();
this.longitute = dto.getLongitute();
this.latitude = dto.getLatitude();
this.date = dto.getDate();
this.notice = dto.getNotice();
this.introduction = dto.getIntroduction();
this.kindOfPerson = dto.getKindOfPerson();
this.totalMember = dto.getTotalMember();
// this.boardImages = boardImages;
this.category = category;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -9,7 +10,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BoardImage {
public class BoardImage extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_image_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -10,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BoardUser {
public class BoardUser extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "board_user_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -10,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BookMark {
public class BookMark extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "book_mark_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
Expand All @@ -9,7 +10,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Category {
public class Category extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "category_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -11,7 +12,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class ClipBoard {
public class ClipBoard extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "clip_board_id")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.yogit.server.board.entity;

import com.yogit.server.config.domain.BaseEntity;
import com.yogit.server.user.entity.User;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -10,7 +11,7 @@
@Entity
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Comment {
public class Comment extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "comment_id")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
@RequiredArgsConstructor
public enum BoardExceptionList {

NOT_FOUND_BOARD("B0001", NOT_FOUND,"존재하지 않는 Board아이디입니다.");
NOT_FOUND_BOARD("B0001", NOT_FOUND,"존재하지 않는 Board아이디입니다."),
NOT_HOST_OF_BOARD("B0002",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;

public class NotHostOfBoardExcepion extends BoardException{

public NotHostOfBoardExcepion(){
super(BoardExceptionList.NOT_HOST_OF_BOARD.getCODE(), BoardExceptionList.NOT_HOST_OF_BOARD.getHTTPSTATUS(), BoardExceptionList.NOT_HOST_OF_BOARD.getMESSAGE());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

import com.yogit.server.board.entity.Board;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

import java.util.Optional;

public interface BoardRepository extends JpaRepository<Board, Long> {

@Query("select b from Board b where b.id = :boardId and b.status = 'ACTIVE'")
Optional<Board> findBoardById(@Param("boardId") Long boardId);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.yogit.server.board.service;

import com.yogit.server.board.dto.request.CreateBoardReq;
import com.yogit.server.board.dto.request.PatchBoardReq;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.global.dto.ApplicationResponse;

public interface BoardService {

ApplicationResponse<BoardRes> createBoard(CreateBoardReq createBoardReq);

ApplicationResponse<BoardRes> updateBoard(PatchBoardReq patchBoardReq);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.yogit.server.board.service;

import com.yogit.server.board.dto.request.CreateBoardReq;
import com.yogit.server.board.dto.request.PatchBoardReq;
import com.yogit.server.board.dto.response.BoardRes;
import com.yogit.server.board.entity.Board;
import com.yogit.server.board.entity.BoardUser;
import com.yogit.server.board.entity.Category;
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.repository.CategoryRepository;
import com.yogit.server.board.repository.BoardRepository;
Expand All @@ -18,6 +21,9 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import javax.sound.midi.Patch;

@Service
@Transactional(readOnly = true)
Expand Down Expand Up @@ -50,12 +56,39 @@ public ApplicationResponse<BoardRes> createBoard(CreateBoardReq dto){
// 호스트 boardUser 생성 및 board에 추가
board.addBoardUser(new BoardUser(host, board));

//TODO: BoardImages aws s3에 저장 , url board엔티티에 추가

// board 저장
Board savedBoard = boardRepository.save(board);
// resDto 벼환
BoardRes boardRes = BoardRes.toDto(savedBoard);
// 반환
BoardRes boardRes = BoardRes.toDto(savedBoard); // resDto 벼환
return ApplicationResponse.create("요청에 성공하였습니다.", boardRes);
}


@Transactional(readOnly = false)
@Override
public ApplicationResponse<BoardRes> updateBoard(PatchBoardReq dto){

User host = userRepository.findById(dto.getHostId())
.orElseThrow(() -> new NotFoundUserException());

City city = cityRepository.findById(dto.getCityId())
.orElseThrow(() -> new NotFoundCityException());

Category category = categoryRepository.findById(dto.getCategoryId())
.orElseThrow(() -> new NotFoundCategoryException());

//validation: board 존재 여부
Board board = boardRepository.findBoardById(dto.getBoardId())
.orElseThrow(() -> new NotFoundBoardException());
//validation: 요청자와 host 비교
if(!board.getHost().getId().equals(dto.getHostId())){
throw new NotHostOfBoardExcepion();
}

board.updateBoard(dto, city, category);
BoardRes boardRes = BoardRes.toDto(board);
return ApplicationResponse.ok(boardRes);
}

}
Loading

0 comments on commit 25557ad

Please sign in to comment.