Skip to content

Commit

Permalink
Merge pull request #66 from YogitTeam/appleLogin1
Browse files Browse the repository at this point in the history
#65 feat: Board에 유저멤버 추가 API 구현, Validation(중복 유저, 보드 맥스 인원) 검증, Exce…
  • Loading branch information
xhaktmchl authored Nov 24, 2022
2 parents 9ad9769 + 51c14ee commit 3b922be
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 1 deletion.
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);
}
}
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;
}
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;
}
}
4 changes: 4 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 @@ -162,4 +162,8 @@ public List<String> getBoardImagesUUids(){
.map(boardImage -> boardImage.getImgUUid())
.collect(Collectors.toList());
}

public void addCurrentMember(){
this.currentMember+=1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
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());
}
}
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Board, Long> {

@Query("select b from Board b where b.id = :boardId and b.status = 'ACTIVE'")
Expand Down
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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Category, Long> {

@Query("select c from Category c where c.status = 'ACTIVE'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<ClipBoard, Long> {

@Query("select cb from ClipBoard cb where cb.id = :clipBoardId and cb.status = 'ACTIVE'")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Comment, Long> {

@Query("select cm from Comment cm where cm.clipBoard.id = :clipBoardId and cm.status = 'ACTIVE'")
Expand Down
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);
}
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);
}
}

0 comments on commit 3b922be

Please sign in to comment.