Skip to content

Commit

Permalink
Merge pull request #23 from selab-hs/feature/board
Browse files Browse the repository at this point in the history
[Feature/board] member 권한 검사 기능 추가
  • Loading branch information
HwangHarim authored Aug 24, 2023
2 parents bb7ee09 + 99c61c0 commit 26c3ff2
Show file tree
Hide file tree
Showing 18 changed files with 305 additions and 54 deletions.
Empty file.
93 changes: 76 additions & 17 deletions src/main/java/com/core/service/board/application/BoardService.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.core.service.board.application;

import static com.core.service.error.dto.ErrorMessage.NON_EXISTENT_BOARD_EXCEPTION;
import static com.core.service.error.dto.ErrorMessage.UNAUTHORIZED_ACCESS_EXCEPTION;

import com.core.service.board.domain.Board;
import com.core.service.auth.domain.UserDetail;
import com.core.service.board.domain.converter.BoardConverter;
import com.core.service.board.dto.Response.ReadAllBoardResponse;
import com.core.service.board.dto.Response.ReadBoardResponse;
import com.core.service.board.dto.request.CreateBoardRequest;
import com.core.service.board.dto.request.UpdateBoardRequest;
import com.core.service.board.infrastructure.BoardRepository;
import com.core.service.error.exception.board.NonExistentBoardException;
import com.core.service.error.exception.member.UnauthorizedAccessException;
import com.core.service.member.domain.vo.RoleType;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -24,43 +27,99 @@ public class BoardService {
private final BoardConverter converter;

@Transactional
public void create(CreateBoardRequest request) {
public void create(CreateBoardRequest request, UserDetail userInfo) {
if (!(userInfo.getRoleType()
.equals(RoleType.LAB_LEADER) ||
userInfo.getRoleType()
.equals(RoleType.LAB_USER))) {
throw new UnauthorizedAccessException(
UNAUTHORIZED_ACCESS_EXCEPTION,
"권한이 없는 접근입니다."
);
}
boardRepository.save(
converter.convertToBoardEntity(request)
converter.convertToBoardEntity(request, userInfo)
);
}

@Transactional
public void update(Long boardId, UpdateBoardRequest request) {
Board board = boardRepository.findById(boardId).orElseThrow(
() -> new NonExistentBoardException(NON_EXISTENT_BOARD_EXCEPTION,"업데이트 단일 게시판 조회 실패")
);
public void update(Long boardId, UpdateBoardRequest request, UserDetail userInfo) {
if (!(boardRepository.existsByIdAndMemberId(
boardId, userInfo.getId()) &&
(userInfo.getRoleType()
.equals(RoleType.LAB_LEADER) ||
userInfo.getRoleType()
.equals(RoleType.LAB_USER)))) {
throw new UnauthorizedAccessException(
UNAUTHORIZED_ACCESS_EXCEPTION,
"권한이 없는 접근입니다."
);
}
var board = boardRepository.findById(boardId)
.orElseThrow(() -> new NonExistentBoardException(
NON_EXISTENT_BOARD_EXCEPTION,
"업데이트 단일 게시판 조회 실패"
)
);
board.update(request);
boardRepository.save(board);
}

@Transactional(readOnly = true)
public ReadBoardResponse get(Long boardId) {
Board board = boardRepository.findById(boardId).orElseThrow(
() -> new NonExistentBoardException(NON_EXISTENT_BOARD_EXCEPTION,"단일 게시판 조회 실패")
);
public ReadBoardResponse get(Long boardId, UserDetail userInfo) {
if (!(userInfo.getRoleType()
.equals(RoleType.LAB_LEADER) ||
userInfo.getRoleType()
.equals(RoleType.LAB_USER))) {
throw new UnauthorizedAccessException(
UNAUTHORIZED_ACCESS_EXCEPTION,
"권한이 없는 접근입니다."
);
}

var board = boardRepository.findById(boardId)
.orElseThrow(
() -> new NonExistentBoardException(
NON_EXISTENT_BOARD_EXCEPTION,
"단일 게시판 조회 실패"
)
);

return converter.convertToReadBoardResponse(board);
}

@Transactional(readOnly = true)
public Page<ReadAllBoardResponse> getAll(Pageable pageable) {
return converter.convertToReadAllBoardResponse(boardRepository.findAll(pageable));
return converter.convertToReadAllBoardResponse(
boardRepository.findAll(pageable)
);
}

@Transactional
public void delete(Long id) {
boardRepository.deleteById(id);
public void delete(Long boardId, UserDetail userInfo) {
if (!(boardRepository.existsByIdAndMemberId(
boardId, userInfo.getId()) &&
(userInfo.getRoleType()
.equals(RoleType.LAB_LEADER) ||
userInfo.getRoleType()
.equals(RoleType.LAB_USER)))) {
throw new UnauthorizedAccessException(
UNAUTHORIZED_ACCESS_EXCEPTION,
"권한이 없는 접근입니다."
);
}
boardRepository.deleteById(boardId);
}

@Transactional
public Long plusView(Long id){
Board board = boardRepository.findById(id).orElseThrow(
() -> new NonExistentBoardException(NON_EXISTENT_BOARD_EXCEPTION, "게시글 조회 단일 게시판 조회 실패"));
public Long plusView(Long boardId) {
var board = boardRepository.findById(boardId)
.orElseThrow(
() -> new NonExistentBoardException(
NON_EXISTENT_BOARD_EXCEPTION,
"게시글 조회 단일 게시판 조회 실패"
)
);
board.updateView();
boardRepository.save(board);

Expand Down
Empty file.
2 changes: 1 addition & 1 deletion src/main/java/com/core/service/board/domain/Board.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class Board extends BaseEntity {
@Column(name = "board_id")
private Long id;

private String memberId;
private Long memberId;

private String title;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.core.service.board.domain.converter;

import com.core.service.auth.domain.UserDetail;
import com.core.service.board.domain.Board;
import com.core.service.board.dto.Response.ReadAllBoardResponse;
import com.core.service.board.dto.Response.ReadBoardResponse;
Expand All @@ -10,10 +11,9 @@
@Component
public class BoardConverter {

public Board convertToBoardEntity(CreateBoardRequest request){
public Board convertToBoardEntity(CreateBoardRequest request, UserDetail userInfo){
return Board.builder()
.id(request.getId())
.memberId(request.getMemberId())
.memberId(userInfo.getId())
.title(request.getTitle())
.content(request.getContent())
.build();
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Builder
public class ReadAllBoardResponse {
private Long id;
private String memberId;
private Long memberId;
private String title;
private String content;
private Long view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
@Getter
public class ReadBoardResponse {
private Long id;
private String memberId;
private Long memberId;
private String title;
private String content;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class CreateBoardRequest {
private Long id;
private String memberId;
private String title;
private String content;
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.core.service.board.dto.request;

import lombok.Getter;
import lombok.Setter;

@Getter
@Setter
public class UpdateBoardRequest {
private String title;
private String content;
Expand Down
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@

public interface BoardRepository extends JpaRepository<Board, Long> {
Page<Board> findAll(Pageable pageable);

boolean existsByIdAndMemberId(Long id, Long memberId);
}
Empty file.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.core.service.board.presentaion;

import com.core.service.auth.domain.UserDetail;
import com.core.service.auth.infrastructure.annotation.AuthMember;
import com.core.service.board.application.BoardService;
import com.core.service.board.dto.Response.ReadAllBoardResponse;
import com.core.service.board.dto.Response.ReadBoardResponse;
Expand Down Expand Up @@ -32,88 +34,119 @@ public class BoardController {
/**
* 게시글을 전체 조회.
*
* @tobo type 별로 검색을 받아 조회 하게 만들 예정
* @param pageable
* @return search all board Response
* @tobo type 별로 검색을 받아 조회 하게 만들 예정
*/
@GetMapping
public ResponseEntity<ResponseDto> getAllBoards(
@PageableDefault(sort ="id", size = 15,direction= Direction.ASC)
Pageable pageable){
Page<ReadAllBoardResponse> boards = boardService.getAll(pageable);
public ResponseEntity<?> getAllBoards(
@PageableDefault(sort = "id", size = 15, direction = Direction.ASC)
Pageable pageable
) {
var boards = boardService.getAll(pageable);

return ResponseDto.toResponseEntity(ResponseMessage.READ_SUCCESS_ALL_BOARD, boards);
return ResponseDto.toResponseEntity(
ResponseMessage.READ_SUCCESS_ALL_BOARD,
boards
);
}

/**
* 게시글 상세 조회.
*
* @tobo 인증 받은 member만 조회 가능하게 변경
* @param id-> 게시판 ID
* @return search Detail board Response
* @tobo 인증 받은 member만 조회 가능하게 변경
*/
@GetMapping("{id}")
public ResponseEntity<ResponseDto> getBoards(@PathVariable("id") Long id){
ReadBoardResponse board = boardService.get(id);
public ResponseEntity<?> getBoards(
@PathVariable("id") Long id,
@AuthMember UserDetail userInfo
) {
var board = boardService.get(id, userInfo);
boardService.plusView(id);

return ResponseDto.toResponseEntity(ResponseMessage.READ_SUCCESS_BOARD, board);
return ResponseDto.toResponseEntity(
ResponseMessage.READ_SUCCESS_BOARD,
board
);
}

/**
* 게시글 생성.
*
* @tobo 이미지도 업로드 가능하게 변경 예정
* @param request 게시글 생성에 필요한 data들
* @return create board
* @tobo 이미지도 업로드 가능하게 변경 예정
*/
@PostMapping
public ResponseEntity<ResponseDto> createBoard(@RequestBody CreateBoardRequest request){
boardService.create(request);
public ResponseEntity<?> createBoard(
@RequestBody CreateBoardRequest request,
@AuthMember UserDetail userInfo
) {
boardService.create(request, userInfo);

return ResponseDto.toResponseEntity(ResponseMessage.CREATE_SUCCESS_BOARD, "게시판 생성 성공");
return ResponseDto.toResponseEntity(
ResponseMessage.CREATE_SUCCESS_BOARD,
"게시판 생성 성공"
);
}

/**
* 게시글 변경.
*
* @tobo 이미지도 변경가능하게 변경, 생성자만 변경 할 수 있게 변경
* @param request 게시글에 변경 사항(title, content)
* @return update board 게시글 변경
* @tobo 이미지도 변경가능하게 변경, 생성자만 변경 할 수 있게 변경
*/
@PatchMapping("{id}")
public ResponseEntity<ResponseDto> updateBoards(
@PathVariable("id") Long id, @RequestBody UpdateBoardRequest request){
boardService.update(id, request);
public ResponseEntity<?> updateBoards(
@PathVariable("id") Long id,
@RequestBody UpdateBoardRequest request,
@AuthMember UserDetail userInfo
) {
boardService.update(id, request, userInfo);

return ResponseDto.toResponseEntity(ResponseMessage.UPDATE_SUCCESS_BOARD, "게시판 수정 성공");
return ResponseDto.toResponseEntity(
ResponseMessage.UPDATE_SUCCESS_BOARD,
"게시판 수정 성공"
);
}

/**
* 게시글 조회수 조회.
*
* @param id 해당 board의 id
* @// TODO: 2023/08/24 Application Publisher 적용하기
* @param id
* @return detail board view up & search
*/
@GetMapping("{id}/view")
public ResponseEntity<ResponseDto> getView(@PathVariable("id") Long id){
Long viewCount = boardService.plusView(id);
public ResponseEntity<?> getView(@PathVariable("id") Long id) {
var viewCount = boardService.plusView(id);

return ResponseDto.toResponseEntity(ResponseMessage.UPDATE_SUCCESS_BOARD, "조회수 조회 성공");
return ResponseDto.toResponseEntity(
ResponseMessage.UPDATE_SUCCESS_BOARD,
"조회수 조회 성공"
);
}

/**
* 게시글 삭제.
*
* @tobo - 게시글과 영속성을 갖일 댓글도 같이 삭제 가능하게 변경할 예정
* - 작성자만 게시글을 삭제 가능하게 변경한 예정
* @param id 삭제 해당 게시글 id
* @return delete board 게시글 삭제
* @tobo - 게시글과 영속성을 갖일 댓글도 같이 삭제 가능하게 변경할 예정 - 작성자만 게시글을 삭제 가능하게 변경한 예정
*/
@DeleteMapping("{id}")
public ResponseEntity<ResponseDto> deleteBoard(@PathVariable("id") Long id){
boardService.delete(id);
public ResponseEntity<?> deleteBoard(
@PathVariable("id") Long id,
@AuthMember UserDetail userInfo
) {
boardService.delete(id, userInfo);

return ResponseDto.toResponseEntity(ResponseMessage.DELETE_SUCCESS_BOARD, "게시판 삭제 성공");
return ResponseDto.toResponseEntity(
ResponseMessage.DELETE_SUCCESS_BOARD,
"게시판 삭제 성공"
);
}
}
1 change: 1 addition & 0 deletions src/main/java/com/core/service/error/dto/ErrorMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public enum ErrorMessage {
INVALID_LOGIN_USER_INFORMATION_EXCEPTION(HttpStatus.NOT_FOUND, "잘못된 로그인 정보입니다"),
NOT_LOGIN_USER_EXCEPTION(HttpStatus.BAD_REQUEST, "로그인 정보가 없습니다"),
NON_EXISTENT_BOARD_EXCEPTION(HttpStatus.BAD_REQUEST,"해당 보드가 존재하지 않습니다."),
UNAUTHORIZED_ACCESS_EXCEPTION(HttpStatus.BAD_REQUEST,"권한이 없는 접근 입니다."),
;

private final HttpStatus status;
Expand Down
Loading

0 comments on commit 26c3ff2

Please sign in to comment.