Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.back.catchmate.domain.board.controller;

import com.back.catchmate.domain.board.dto.BoardRequest.CreateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.CreateOrUpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardDeleteInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
Expand Down Expand Up @@ -39,8 +38,8 @@ public class BoardController {
@PostMapping
@Operation(summary = "게시글 등록 API", description = "게시글을 등록합니다.")
public BoardInfo createBoard(@JwtValidation Long userId,
@Valid @RequestBody CreateBoardRequest request) {
return boardService.createBoard(userId, request);
@Valid @RequestBody CreateOrUpdateBoardRequest request) {
return boardService.createOrUpdateBoard(userId, null, request);
}

@GetMapping("/{boardId}")
Expand Down Expand Up @@ -96,8 +95,8 @@ public StateResponse removeBookMark(@JwtValidation Long userId,
@Operation(summary = "게시글 수정 API", description = "게시글을 수정합니다.")
public BoardInfo updateBoard(@JwtValidation Long userId,
@PathVariable Long boardId,
@Valid @RequestBody UpdateBoardRequest request) {
return boardService.updateBoard(userId, boardId, request);
@Valid @RequestBody CreateOrUpdateBoardRequest request) {
return boardService.createOrUpdateBoard(userId, boardId, request);
}

@DeleteMapping("/{boardId}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
public class BoardConverter {
private final GameConverter gameConverter;

public Board toEntity(User user, Game game, Club cheerClub, CreateBoardRequest boardRequest) {
public Board toEntity(User user, Game game, Club cheerClub, CreateOrUpdateBoardRequest boardRequest) {
return Board.builder()
.title(boardRequest.getTitle())
.content(boardRequest.getContent())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public abstract class BoardRequest {
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class CreateBoardRequest {
public static class CreateOrUpdateBoardRequest {
@NotNull
private String title;
@NotNull
Expand All @@ -34,27 +34,4 @@ public static class CreateBoardRequest {
@NotNull
private Boolean isCompleted;
}

@Getter
@Builder
@NoArgsConstructor
@AllArgsConstructor
public static class UpdateBoardRequest {
@NotNull
private String title;
@NotNull
private String content;
@Range(min = 1, max = 8)
private int maxPerson;
@NotNull
private Long cheerClubId;
@NotNull
private String preferredGender;
@NotNull
private List<String> preferredAgeRange;
@NotNull
private CreateGameRequest gameRequest;
@NotNull
private Boolean isCompleted;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.back.catchmate.domain.board.entity;

import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.CreateOrUpdateBoardRequest;
import com.back.catchmate.domain.club.entity.Club;
import com.back.catchmate.domain.enroll.entity.Enroll;
import com.back.catchmate.domain.game.entity.Game;
Expand Down Expand Up @@ -78,7 +78,7 @@ public boolean isWriterSameAsLoginUser(User user) {
return this.user.getId().equals(user.getId());
}

public void updateBoard(Club cheerClub, Game game, UpdateBoardRequest boardRequest) {
public void updateBoard(Club cheerClub, Game game, CreateOrUpdateBoardRequest boardRequest) {
this.title = boardRequest.getTitle();
this.content = boardRequest.getContent();
this.maxPerson = boardRequest.getMaxPerson();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package com.back.catchmate.domain.board.service;

import com.back.catchmate.domain.board.dto.BoardRequest.CreateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardDeleteInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
import org.springframework.data.domain.Pageable;

import java.time.LocalDate;

import static com.back.catchmate.domain.board.dto.BoardRequest.*;

public interface BoardService {
BoardInfo createBoard(Long userId, CreateBoardRequest request);
BoardInfo createOrUpdateBoard(Long userId, Long boardId, CreateOrUpdateBoardRequest request);

BoardInfo getBoard(Long userId, Long boardId);

Expand All @@ -19,6 +20,4 @@ public interface BoardService {
PagedBoardInfo getBoardListByUserId(Long loginUserId, Long userId, Pageable pageable);

BoardDeleteInfo deleteBoard(Long userId, Long boardId);

BoardInfo updateBoard(Long userId, Long boardId, UpdateBoardRequest boardRequest);
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.back.catchmate.domain.board.service;

import com.back.catchmate.domain.board.converter.BoardConverter;
import com.back.catchmate.domain.board.dto.BoardRequest.CreateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.UpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardRequest.CreateOrUpdateBoardRequest;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardDeleteInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.BoardInfo;
import com.back.catchmate.domain.board.dto.BoardResponse.PagedBoardInfo;
Expand Down Expand Up @@ -40,23 +39,45 @@ public class BoardServiceImpl implements BoardService {

@Override
@Transactional
public BoardInfo createBoard(Long userId, CreateBoardRequest request) {
public BoardInfo createOrUpdateBoard(Long userId, Long boardId, CreateOrUpdateBoardRequest request) {
// 유저 정보 조회
User user = userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

// Cheer Club 조회
Club cheerClub = clubRepository.findById(request.getCheerClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

// Home Club 및 Away Club 조회
Club homeClub = clubRepository.findById(request.getGameRequest().getHomeClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

Club awayClub = clubRepository.findById(request.getGameRequest().getAwayClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

// Game 조회 또는 생성
Game game = findOrCreateGame(homeClub, awayClub, request.getGameRequest());

Board board = boardConverter.toEntity(user, game, cheerClub, request);
this.boardRepository.save(board);
Board board;

// note: 최초 임시저장은 create, 이후의 임시저장은 update endpoint를 호출하도록 한다.
if (boardId != null) {
// 기존 Board 업데이트
board = boardRepository.findByIdAndDeletedAtIsNull(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

// 작성자가 동일하지 않은 경우 예외 처리
if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.BOARD_BAD_REQUEST);
}

// Board 업데이트
board.updateBoard(cheerClub, game, request);
} else {
// 새로운 Board 생성
board = boardConverter.toEntity(user, game, cheerClub, request);
boardRepository.save(board);
}

return boardConverter.toBoardInfo(board, game);
}
Expand Down Expand Up @@ -113,34 +134,6 @@ public PagedBoardInfo getBoardListByUserId(Long loginUserId, Long userId, Pageab
return boardConverter.toPagedBoardInfoFromBoardList(boardList);
}

@Override
@Transactional
public BoardInfo updateBoard(Long userId, Long boardId, UpdateBoardRequest request) {
Board board = this.boardRepository.findByIdAndDeletedAtIsNull(boardId)
.orElseThrow(() -> new BaseException(ErrorCode.BOARD_NOT_FOUND));

User user = this.userRepository.findById(userId)
.orElseThrow(() -> new BaseException(ErrorCode.USER_NOT_FOUND));

if (user.isDifferentUserFrom(board.getUser())) {
throw new BaseException(ErrorCode.BOARD_BAD_REQUEST);
}

Club cheerClub = clubRepository.findById(request.getCheerClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

Club homeClub = clubRepository.findById(request.getGameRequest().getHomeClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

Club awayClub = clubRepository.findById(request.getGameRequest().getAwayClubId())
.orElseThrow(() -> new BaseException(ErrorCode.CLUB_NOT_FOUND));

Game game = findOrCreateGame(homeClub, awayClub, request.getGameRequest());

board.updateBoard(cheerClub, game, request);
return boardConverter.toBoardInfo(board, game);
}

@Override
@Transactional
public BoardDeleteInfo deleteBoard(Long userId, Long boardId) {
Expand Down
Loading