Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/docs/asciidoc/board.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ operation::board/delete[snippets="http-request,http-response,request-fields"]
=== 게시글 조회
operation::board/get[snippets="http-request,http-response,query-parameters"]

=== 내 게시글 조회
operation::my-board/get[snippets="http-request,http-response,query-parameters"]

=== 게시글 수정
operation::board/patch[snippets="http-request,http-response,request-fields"]
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.data.domain.Sort;
Expand Down Expand Up @@ -49,8 +50,12 @@ public BoardDto delete(
@GetMapping
public Slice<BoardDto> read(
@PageableDefault(sort = "createdAt", direction = Sort.Direction.DESC) Pageable pageable,
@RequestParam(required = false) Optional<Long> ancestorId
@RequestParam(required = false) Optional<Long> ancestorId,
@RequestHeader(HttpHeaders.AUTHORIZATION) Optional<String> token
) {
if (ancestorId.isEmpty() && token.isPresent()) {
return boardService.readMine(pageable, jwtBuilder.decryptJwt(token.get()));
}
return boardService.readDefault(pageable, ancestorId);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface BoardRepository extends JpaRepository<Board, Long> {

Slice<Board> findAllBy(Pageable pageable);

Slice<Board> findAllByUser_Id(Long userId, Pageable pageable);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface BoardTreePathRepository extends JpaRepository<BoardTreePath, Lo

Slice<BoardTreePath> findAllByAncestor_Id(Long boardId, Pageable pageable);

Slice<BoardTreePath> findAllByAncestor_IdAndAncestor_User_Id(Long boardId, Long userId, Pageable pageable);

}
7 changes: 7 additions & 0 deletions src/main/java/com/bigant/gaeme/service/BoardService.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,12 @@ public Slice<BoardDto> readDefault(Pageable pageable, Optional<Long> ancestorId)
return recentBoard(pageable);
}

public Slice<BoardDto> readMine(Pageable pageable, Long requestId) {
Slice<Board> boards = boardRepository.findAllByUser_Id(requestId, pageable);

return new SliceImpl<>(boards.stream().map(board -> modelMapper.map(board, BoardDto.class)).toList(), pageable, boards.hasNext());
}

private Slice<BoardDto> readDescendant(Pageable pageable, Long ancestorId) {
Pageable newPageable = PageRequest.of(pageable.getPageNumber(), pageable.getPageSize());
Slice<BoardTreePath> boardTreePaths = boardTreePathRepository.findAllByAncestor_Id(ancestorId, newPageable);
Expand All @@ -115,4 +121,5 @@ public BoardDto update(Long requesterId, BoardUpdateRequestDto dto) {
board.update(dto);
return modelMapper.map(board, BoardDto.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ private RestDocumentationResultHandler getBoardGetResultHandler() {
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
queryParameters(parameterWithName("pageNum").description("조회할 페이지 번호"),
parameterWithName("ancestorId").description("댓글 조회시 부모 보드 아이디"))
parameterWithName("ancestorId").optional().description("댓글 조회시 부모 보드 아이디"))
);
}

Expand Down Expand Up @@ -274,4 +274,48 @@ RestDocumentationResultHandler getBoardPatchResultHandler() {
);
}

@Test
void getMyBoard() throws Exception {
//given
User testUser = TestFixture.getTestUser();
BDDMockito.given(boardService.readMine(BDDMockito.any(), BDDMockito.any())).willReturn(
new SliceImpl<>(
List.of(
BoardDto.builder()
.user(modelMapper.map(testUser, UserDto.class))
.id(2L)
.content("cccccc")
.likeCnt(0L)
.createdAt(LocalDateTime.now())
.updatedAt(LocalDateTime.now())
.isDeleted(false)
.pictureUrls(List.of())
.build()
),
PageRequest.of(1, 10, Sort.Direction.DESC, "createdAt"),
false
)
);

//when
mvc.perform(get("/v1/board")
.param("pageNum", "1")
.header(HttpHeaders.AUTHORIZATION, "token"))
.andDo(print())
.andExpect(status().isOk())
.andDo(getMyBoardGetResultHandler());

//then
BDDMockito.then(boardService).should().readMine(BDDMockito.any(), BDDMockito.any());
}

private RestDocumentationResultHandler getMyBoardGetResultHandler() {
return document("my-board/get",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
queryParameters(parameterWithName("pageNum").description("조회할 페이지 번호"),
parameterWithName("ancestorId").optional().description("댓글 조회시 부모 보드 아이디"))
);
}

}
60 changes: 55 additions & 5 deletions src/test/java/com/bigant/gaeme/service/BoardServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.bigant.gaeme.repository.entity.Board;
import com.bigant.gaeme.repository.entity.BoardTreePath;
import com.bigant.gaeme.repository.entity.User;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Assertions;
Expand All @@ -18,10 +17,6 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.data.auditing.AuditingHandler;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
Expand Down Expand Up @@ -317,4 +312,59 @@ public BoardServiceTest(BoardRepository boardRepository, BoardTreePathRepository
.build());
}

@Test
void 본인_게시글_조회_성공() {
//given
User mine = TestFixture.getTestUser();
User another = TestFixture.getTestUser();
another.setName("tester2");
Board testBoard = TestFixture.getTestBoard(another);
Pageable pageable = PageRequest.of(0, 10, Sort.Direction.DESC, "createdAt");
userRepository.save(mine);
userRepository.save(another);
boardRepository.save(Board.builder()
.user(mine)
.content("aaaaa")
.likeCnt(0L)
.build());
boardRepository.save(Board.builder()
.user(mine)
.content("bbbbb")
.likeCnt(0L)
.build());
boardRepository.save(Board.builder()
.user(another)
.content("cccccc")
.likeCnt(0L)
.build());
boardRepository.save(testBoard);

//when
Slice<BoardDto> result = boardService.readMine(pageable, mine.getId());

//then
Assertions.assertEquals(List.of(
BoardDto.builder()
.user(modelMapper.map(mine, UserDto.class))
.id(result.getContent().get(0).getId())
.createdAt(result.getContent().get(0).getCreatedAt())
.updatedAt(result.getContent().get(0).getUpdatedAt())
.content("bbbbb")
.likeCnt(0L)
.pictureUrls(List.of())
.isDeleted(false)
.build(),
BoardDto.builder()
.user(modelMapper.map(mine, UserDto.class))
.id(result.getContent().get(1).getId())
.createdAt(result.getContent().get(1).getCreatedAt())
.updatedAt(result.getContent().get(1).getUpdatedAt())
.content("aaaaa")
.likeCnt(0L)
.pictureUrls(List.of())
.isDeleted(false)
.build()
), result.getContent());
}

}