diff --git a/server/src/main/java/com/yogit/server/board/controller/BoardUserController.java b/server/src/main/java/com/yogit/server/board/controller/BoardUserController.java new file mode 100644 index 0000000..2a721d4 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/controller/BoardUserController.java @@ -0,0 +1,41 @@ +package com.yogit.server.board.controller; + +import com.yogit.server.board.dto.request.boarduser.CreateBoardUserReq; +import com.yogit.server.board.dto.response.boarduser.BoardUserRes; +import com.yogit.server.board.service.boarduser.BoardUserService; +import com.yogit.server.global.dto.ApplicationResponse; +import io.swagger.annotations.ApiOperation; +import io.swagger.annotations.ApiResponse; +import io.swagger.annotations.ApiResponses; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +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; + +@Slf4j +@RestController +@RequiredArgsConstructor // private final DI의존주입 +@RequestMapping("/boardusers") +public class BoardUserController { + + private final BoardUserService boardUserService; + + /** + * 보드 멤버 추가 + * @author 토마스 + */ + @ApiOperation(value = "보드 멤버 추가", notes = "보드 모임 가입 요청") + @ApiResponses({ + @ApiResponse(code= 201, message = "요청에 성공하였습니다."), + @ApiResponse(code= 404, message = "존재하지 않는 유저입니다."), + @ApiResponse(code= 404, message = "존재하지 않는 Board아이디입니다."), + @ApiResponse(code = 4000 , message = "서버 오류입니다.") + }) + @PostMapping + public ApplicationResponse joinBoardUser(@RequestBody @Validated CreateBoardUserReq dto){ + return boardUserService.joinBoardUser(dto); + } +} diff --git a/server/src/main/java/com/yogit/server/board/dto/request/boarduser/CreateBoardUserReq.java b/server/src/main/java/com/yogit/server/board/dto/request/boarduser/CreateBoardUserReq.java new file mode 100644 index 0000000..bbd4f99 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/request/boarduser/CreateBoardUserReq.java @@ -0,0 +1,19 @@ +package com.yogit.server.board.dto.request.boarduser; + +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class CreateBoardUserReq { + + @ApiModelProperty(example = "1") + @ApiParam(value = "userId,유저 PK", required = true) + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "boardId, 보드 PK", required = true) + private Long boardId; +} diff --git a/server/src/main/java/com/yogit/server/board/dto/response/boarduser/BoardUserRes.java b/server/src/main/java/com/yogit/server/board/dto/response/boarduser/BoardUserRes.java new file mode 100644 index 0000000..105040b --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/dto/response/boarduser/BoardUserRes.java @@ -0,0 +1,42 @@ +package com.yogit.server.board.dto.response.boarduser; + +import com.yogit.server.board.entity.Board; +import com.yogit.server.board.entity.BoardUser; +import com.yogit.server.user.entity.User; +import io.swagger.annotations.ApiModelProperty; +import io.swagger.annotations.ApiParam; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +public class BoardUserRes { + + @ApiModelProperty(example = "1") + @ApiParam(value = "userId,유저 PK") + private Long userId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "boardId, 보드 PK") + private Long boardId; + + @ApiModelProperty(example = "1") + @ApiParam(value = "boardUserId, 보드 멤버 PK") + private Long boardUserId; + + public static BoardUserRes toDto(BoardUser boardUser, User user, Board board){ + return BoardUserRes.builder() + .userId(user.getId()) + .boardId(board.getId()) + .boardUserId(boardUser.getId()) + .build(); + } + + @Builder + public BoardUserRes(Long userId, Long boardId, Long boardUserId) { + this.userId = userId; + this.boardId = boardId; + this.boardUserId = boardUserId; + } +} diff --git a/server/src/main/java/com/yogit/server/board/entity/Board.java b/server/src/main/java/com/yogit/server/board/entity/Board.java index 5787fa1..1e1a407 100644 --- a/server/src/main/java/com/yogit/server/board/entity/Board.java +++ b/server/src/main/java/com/yogit/server/board/entity/Board.java @@ -162,4 +162,8 @@ public List getBoardImagesUUids(){ .map(boardImage -> boardImage.getImgUUid()) .collect(Collectors.toList()); } + + public void addCurrentMember(){ + this.currentMember+=1; + } } diff --git a/server/src/main/java/com/yogit/server/board/exception/BoardExceptionList.java b/server/src/main/java/com/yogit/server/board/exception/BoardExceptionList.java index c530a2d..660e2de 100644 --- a/server/src/main/java/com/yogit/server/board/exception/BoardExceptionList.java +++ b/server/src/main/java/com/yogit/server/board/exception/BoardExceptionList.java @@ -11,7 +11,9 @@ public enum BoardExceptionList { NOT_FOUND_BOARD("B0001", NOT_FOUND,"존재하지 않는 Board아이디입니다."), - NOT_HOST_OF_BOARD("B0002",HttpStatus.BAD_REQUEST, "요청한 유저가 호스트가 아닙니다."); + NOT_HOST_OF_BOARD("B0002",HttpStatus.BAD_REQUEST, "요청한 유저가 호스트가 아닙니다."), + MAX_BOARD_USER("B003", HttpStatus.BAD_REQUEST, "보드 인원이 다 찼습니다."), + DUPLICATED_BOARD_USER("B004", HttpStatus.BAD_REQUEST, "이미 보드에 참여했습니다."); private final String CODE; private final HttpStatus HTTPSTATUS; diff --git a/server/src/main/java/com/yogit/server/board/exception/DuplicatedBoardUserException.java b/server/src/main/java/com/yogit/server/board/exception/DuplicatedBoardUserException.java new file mode 100644 index 0000000..42e491c --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/DuplicatedBoardUserException.java @@ -0,0 +1,7 @@ +package com.yogit.server.board.exception; + +public class DuplicatedBoardUserException extends BoardException{ + public DuplicatedBoardUserException(){ + super(BoardExceptionList.DUPLICATED_BOARD_USER.getCODE(), BoardExceptionList.DUPLICATED_BOARD_USER.getHTTPSTATUS(), BoardExceptionList.DUPLICATED_BOARD_USER.getMESSAGE()); + } +} diff --git a/server/src/main/java/com/yogit/server/board/exception/MaxBoardUserException.java b/server/src/main/java/com/yogit/server/board/exception/MaxBoardUserException.java new file mode 100644 index 0000000..556a3c3 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/exception/MaxBoardUserException.java @@ -0,0 +1,8 @@ +package com.yogit.server.board.exception; + + +public class MaxBoardUserException extends BoardException { + public MaxBoardUserException(){ + super(BoardExceptionList.MAX_BOARD_USER.getCODE(), BoardExceptionList.MAX_BOARD_USER.getHTTPSTATUS(), BoardExceptionList.MAX_BOARD_USER.getMESSAGE()); + } +} diff --git a/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java b/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java index 69e25a5..bcba13e 100644 --- a/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/BoardRepository.java @@ -6,9 +6,11 @@ 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 BoardRepository extends JpaRepository { @Query("select b from Board b where b.id = :boardId and b.status = 'ACTIVE'") diff --git a/server/src/main/java/com/yogit/server/board/repository/BoardUserRepository.java b/server/src/main/java/com/yogit/server/board/repository/BoardUserRepository.java new file mode 100644 index 0000000..d8076da --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/repository/BoardUserRepository.java @@ -0,0 +1,17 @@ +package com.yogit.server.board.repository; + +import com.yogit.server.board.entity.BoardUser; +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 BoardUserRepository extends JpaRepository { + + @Query("select bu from BoardUser bu where bu.user.id = :userId and bu.board.id = :boardId and bu.status = 'ACTIVE'") + Optional findByUserIdAndBoardId(@Param("userId") Long userId, @Param("boardId") Long boardId); + +} diff --git a/server/src/main/java/com/yogit/server/board/repository/CategoryRepository.java b/server/src/main/java/com/yogit/server/board/repository/CategoryRepository.java index 1a3ce7e..2c960d0 100644 --- a/server/src/main/java/com/yogit/server/board/repository/CategoryRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/CategoryRepository.java @@ -3,9 +3,11 @@ import com.yogit.server.board.entity.Category; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; +import org.springframework.stereotype.Repository; import java.util.List; +@Repository public interface CategoryRepository extends JpaRepository { @Query("select c from Category c where c.status = 'ACTIVE'") diff --git a/server/src/main/java/com/yogit/server/board/repository/ClipBoardRepository.java b/server/src/main/java/com/yogit/server/board/repository/ClipBoardRepository.java index de620ba..12cca16 100644 --- a/server/src/main/java/com/yogit/server/board/repository/ClipBoardRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/ClipBoardRepository.java @@ -5,10 +5,12 @@ 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.List; import java.util.Optional; +@Repository public interface ClipBoardRepository extends JpaRepository { @Query("select cb from ClipBoard cb where cb.id = :clipBoardId and cb.status = 'ACTIVE'") diff --git a/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java b/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java index 06da19b..225ce9d 100644 --- a/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java +++ b/server/src/main/java/com/yogit/server/board/repository/CommentRepository.java @@ -4,10 +4,12 @@ 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.List; import java.util.Optional; +@Repository public interface CommentRepository extends JpaRepository { @Query("select cm from Comment cm where cm.clipBoard.id = :clipBoardId and cm.status = 'ACTIVE'") diff --git a/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserService.java b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserService.java new file mode 100644 index 0000000..bbe6314 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserService.java @@ -0,0 +1,10 @@ +package com.yogit.server.board.service.boarduser; + +import com.yogit.server.board.dto.request.boarduser.CreateBoardUserReq; +import com.yogit.server.board.dto.response.boarduser.BoardUserRes; +import com.yogit.server.global.dto.ApplicationResponse; + +public interface BoardUserService { + + ApplicationResponse joinBoardUser(CreateBoardUserReq createBoardUserReq); +} diff --git a/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java new file mode 100644 index 0000000..11ad8f5 --- /dev/null +++ b/server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java @@ -0,0 +1,58 @@ +package com.yogit.server.board.service.boarduser; + +import com.yogit.server.board.dto.request.boarduser.CreateBoardUserReq; +import com.yogit.server.board.dto.response.boarduser.BoardUserRes; +import com.yogit.server.board.entity.Board; +import com.yogit.server.board.entity.BoardUser; +import com.yogit.server.board.exception.DuplicatedBoardUserException; +import com.yogit.server.board.exception.MaxBoardUserException; +import com.yogit.server.board.exception.NotFoundBoardException; +import com.yogit.server.board.repository.BoardRepository; +import com.yogit.server.board.repository.BoardUserRepository; +import com.yogit.server.global.dto.ApplicationResponse; +import com.yogit.server.user.entity.User; +import com.yogit.server.user.exception.NotFoundUserException; +import com.yogit.server.user.repository.UserRepository; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@Transactional(readOnly = true) +@RequiredArgsConstructor +public class BoardUserServiceImpl implements BoardUserService{ + + private final BoardUserRepository boardUserRepository; + private final UserRepository userRepository; + private final BoardRepository boardRepository; + + @Transactional(readOnly = false) + @Override + public ApplicationResponse joinBoardUser(CreateBoardUserReq dto) { + + User user = userRepository.findById(dto.getUserId()) + .orElseThrow(() -> new NotFoundUserException()); + + Board board = boardRepository.findBoardById(dto.getBoardId()) + .orElseThrow(() -> new NotFoundBoardException()); + + // Validation: 유저가 멤버에 이미 가입되어 있는지 검증 + if(boardUserRepository.findByUserIdAndBoardId(dto.getUserId(), dto.getBoardId()).isPresent()){ + throw new DuplicatedBoardUserException(); + } + + // Validation: 보드 인원 다 차면 신청 불가능 검증 + if(board.getCurrentMember() >= board.getTotalMember()){ + throw new MaxBoardUserException(); + } + + BoardUser boardUser = new BoardUser(user, board); + BoardUser savedBoardUser = boardUserRepository.save(boardUser); + + board.addCurrentMember();// 보드 현재 인원 +1 + board.addBoardUser(boardUser); // 보드에 멤버 추가 + + BoardUserRes res = BoardUserRes.toDto(boardUser,user, board); + return ApplicationResponse.create("보드에 유저가 조인되었습니다.", res); + } +}