Skip to content

Commit

Permalink
Merge pull request #60 from KNU-HAEDAL-Website/feat-get-board-api-iss…
Browse files Browse the repository at this point in the history
…ue-59

Feat: 단일 게시판 조회 API
  • Loading branch information
tfer2442 authored Jul 13, 2024
2 parents 897c88b + 56adfce commit 940f76d
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public CorsConfiguration getCorsConfiguration(HttpServletRequest request) {
.requestMatchers("/admin/**").hasAnyRole("WEB_MASTER", "ADMIN")
.requestMatchers("/boards/generate-presigned-url").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers(HttpMethod.POST, "/activities/{activityId}/boards").hasAnyRole("WEB_MASTER", "ADMIN", "TEAM_LEADER")
.requestMatchers("/activities/{activityId}/boards", "/login", "/", "/join/**", "/reissue", "/swagger-ui/**", "/v3/api-docs/**", "/users/**","/semesters/**").permitAll()
.requestMatchers("/activities/{activityId}/boards","/activities/{activityId}/boards/{boardId}","/login", "/", "/join/**", "/reissue", "/swagger-ui/**", "/v3/api-docs/**", "/users/**","/semesters/**").permitAll()
.anyRequest().authenticated());

//JWTFilter 등록
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/com/haedal/haedalweb/constants/ErrorCode.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public enum ErrorCode implements ResponseCode{
NOT_FOUND_SEMESTER_ID(HttpStatus.NOT_FOUND, "014", "해당 학기를 찾을 수 없습니다."),
NOT_FOUND_ACTIVITY_ID(HttpStatus.NOT_FOUND, "015", "해당 활동을 찾을 수 없습니다."),
EXIST_ACTIVITY(HttpStatus.CONFLICT, "016", "해당 학기에 활동이 존재하는 경우 삭제할 수 없습니다."),
EXIST_BOARD(HttpStatus.CONFLICT, "017", "해당 활동에 게시판이 존재하는 경우 삭제할 수 없습니다.");
EXIST_BOARD(HttpStatus.CONFLICT, "017", "해당 활동에 게시판이 존재하는 경우 삭제할 수 없습니다."),
NOT_FOUND_BOARD_ID(HttpStatus.NOT_FOUND, "018", "해당 게시판을 찾을 수 없습니다.");

private final HttpStatus httpStatus;
private final String code;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/haedal/haedalweb/controller/BoardController.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,16 @@ public ResponseEntity<Page<BoardDTO>> getBoards(@PathVariable Long activityId,

return ResponseEntity.ok(boardDTOs);
}

@Operation(summary = "게시판 단일 조회")
@Parameters({
@Parameter(name = "activityId", description = "게시판 조회할 활동 ID"),
@Parameter(name = "boardId", description = "해당 게시판 ID")
})
@GetMapping("/activities/{activityId}/boards/{boardId}")
public ResponseEntity<BoardDTO> getBoard(@PathVariable Long activityId, @PathVariable Long boardId) {
BoardDTO boardDTO = boardService.getBoardDTO(activityId, boardId);

return ResponseEntity.ok(boardDTO);
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/haedal/haedalweb/dto/response/BoardDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
@Builder
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class BoardDTO {
private Long activityId;

private Long boardId;

private String boardName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface BoardRepository extends JpaRepository<Board, Long> {
boolean existsByActivityId(Long activityId);

//@Query("select distinct b from Board b join fetch b.participants p join fetch p.user where b.activity.id = :activityId")
Page<Board> findBoardsByActivityId(Long activityId, Pageable pageable);

Optional<Board> findByActivityIdAndId(Long activityId, Long boardId);
}
13 changes: 11 additions & 2 deletions src/main/java/com/haedal/haedalweb/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ public void createBoard(Long activityId, CreateBoardDTO createBoardDTO) {
public Page<BoardDTO> getBoardDTOs(Long activityId, Pageable pageable) {
Page<Board> boardPage = boardRepository.findBoardsByActivityId(activityId, pageable);

return boardPage.map(this::convertToBoardDTO);
return boardPage.map(board -> convertToBoardDTO(board, activityId));
}

@Transactional(readOnly = true)
public BoardDTO getBoardDTO(Long activityId, Long boardId) {
Board board = boardRepository.findByActivityIdAndId(activityId, boardId)
.orElseThrow(() -> new BusinessException(ErrorCode.NOT_FOUND_BOARD_ID));

return convertToBoardDTO(board, activityId);
}

private void addParticipantsToBoard(Board board, List<User> participants) {
Expand All @@ -84,8 +92,9 @@ private void validateParticipants(List<User> users, List<String> userIds) {
});
}

private BoardDTO convertToBoardDTO(Board board) {
private BoardDTO convertToBoardDTO(Board board, Long activityId) {
return BoardDTO.builder()
.activityId(activityId)
.boardId(board.getId())
.boardName(board.getName())
.boardIntro(board.getIntro())
Expand Down

0 comments on commit 940f76d

Please sign in to comment.