Skip to content

Commit

Permalink
#65 feat: Board 유저멤버 삭제 (보드 모임 탈퇴)
Browse files Browse the repository at this point in the history
  • Loading branch information
shinhn committed Nov 29, 2022
1 parent 595818a commit 5c3c569
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@
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;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
Expand All @@ -38,4 +35,20 @@ public class BoardUserController {
public ApplicationResponse<BoardUserRes> joinBoardUser(@RequestBody @Validated CreateBoardUserReq dto){
return boardUserService.joinBoardUser(dto);
}

/**
* 보드 멤버 제거
* @author peanut
*/
@ApiOperation(value = "보드 멤버 제거", notes = "보드 모임 탈퇴 요청")
@ApiResponses({
@ApiResponse(code= 201, message = "요청에 성공하였습니다."),
@ApiResponse(code= 404, message = "존재하지 않는 유저입니다."),
@ApiResponse(code= 404, message = "존재하지 않는 Board아이디입니다."),
@ApiResponse(code = 4000 , message = "서버 오류입니다.")
})
@PatchMapping
public ApplicationResponse<Void> delBoardUser(@RequestBody @Validated CreateBoardUserReq dto){
return boardUserService.delBoardUser(dto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
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 DelBoardUserReq {
@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
Expand Up @@ -13,7 +13,8 @@ public enum BoardExceptionList {
NOT_FOUND_BOARD("B0001", NOT_FOUND,"존재하지 않는 Board아이디입니다."),
NOT_HOST_OF_BOARD("B0002",HttpStatus.BAD_REQUEST, "요청한 유저가 호스트가 아닙니다."),
MAX_BOARD_USER("B003", HttpStatus.BAD_REQUEST, "보드 인원이 다 찼습니다."),
DUPLICATED_BOARD_USER("B004", HttpStatus.BAD_REQUEST, "이미 보드에 참여했습니다.");
DUPLICATED_BOARD_USER("B004", HttpStatus.BAD_REQUEST, "이미 보드에 참여했습니다."),
NOT_FOUND_USER_BOARD("B005", NOT_FOUND, "보드에 가입된 인원이 아닙니다.");

private final String CODE;
private final HttpStatus HTTPSTATUS;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.yogit.server.board.exception;

import static com.yogit.server.board.exception.BoardExceptionList.NOT_FOUND_USER_BOARD;

public class NotFoundUserBoard extends BoardException {
public NotFoundUserBoard(){
super(NOT_FOUND_USER_BOARD.getCODE(), NOT_FOUND_USER_BOARD.getHTTPSTATUS(), NOT_FOUND_USER_BOARD.getMESSAGE());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@
public interface BoardUserService {

ApplicationResponse<BoardUserRes> joinBoardUser(CreateBoardUserReq createBoardUserReq);

ApplicationResponse<Void> delBoardUser(CreateBoardUserReq createBoardUserReq);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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.exception.NotFoundUserBoard;
import com.yogit.server.board.repository.BoardRepository;
import com.yogit.server.board.repository.BoardUserRepository;
import com.yogit.server.global.dto.ApplicationResponse;
Expand All @@ -17,6 +18,8 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

@Service
@Transactional(readOnly = true)
@RequiredArgsConstructor
Expand Down Expand Up @@ -55,4 +58,23 @@ public ApplicationResponse<BoardUserRes> joinBoardUser(CreateBoardUserReq dto) {
BoardUserRes res = BoardUserRes.toDto(boardUser,user, board);
return ApplicationResponse.create("보드에 유저가 조인되었습니다.", res);
}

@Transactional
@Override
public ApplicationResponse<Void> delBoardUser(CreateBoardUserReq createBoardUserReq){

User user = userRepository.findById(createBoardUserReq.getUserId())
.orElseThrow(() -> new NotFoundUserException());

Board board = boardRepository.findBoardById(createBoardUserReq.getBoardId())
.orElseThrow(() -> new NotFoundBoardException());

Optional<BoardUser> boardUser = boardUserRepository.findByUserIdAndBoardId(createBoardUserReq.getUserId(), createBoardUserReq.getBoardId());

if(!boardUser.isPresent()) throw new NotFoundUserBoard();

boardUserRepository.deleteById(boardUser.get().getId());

return ApplicationResponse.ok();
}
}

0 comments on commit 5c3c569

Please sign in to comment.