-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from YogitTeam/appleLogin1
#65 feat: Board에 유저멤버 추가 API 구현, Validation(중복 유저, 보드 맥스 인원) 검증, Exce…
- Loading branch information
Showing
14 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
41 changes: 41 additions & 0 deletions
41
server/src/main/java/com/yogit/server/board/controller/BoardUserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BoardUserRes> joinBoardUser(@RequestBody @Validated CreateBoardUserReq dto){ | ||
return boardUserService.joinBoardUser(dto); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
server/src/main/java/com/yogit/server/board/dto/request/boarduser/CreateBoardUserReq.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
42 changes: 42 additions & 0 deletions
42
server/src/main/java/com/yogit/server/board/dto/response/boarduser/BoardUserRes.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
server/src/main/java/com/yogit/server/board/exception/DuplicatedBoardUserException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
server/src/main/java/com/yogit/server/board/exception/MaxBoardUserException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
server/src/main/java/com/yogit/server/board/repository/BoardUserRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.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<BoardUser, Long> { | ||
|
||
@Query("select bu from BoardUser bu where bu.user.id = :userId and bu.board.id = :boardId and bu.status = 'ACTIVE'") | ||
Optional<BoardUser> findByUserIdAndBoardId(@Param("userId") Long userId, @Param("boardId") Long boardId); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BoardUserRes> joinBoardUser(CreateBoardUserReq createBoardUserReq); | ||
} |
58 changes: 58 additions & 0 deletions
58
server/src/main/java/com/yogit/server/board/service/boarduser/BoardUserServiceImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<BoardUserRes> 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); | ||
} | ||
} |