Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] 한줄평 관련 API 수정, 독서 상태 REST Docs 추가 #116

Merged
merged 3 commits into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 12 additions & 0 deletions src/docs/asciidoc/api/userlibrary/userlibrary.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,15 @@ include::{snippets}/user-library/delete/path-parameters.adoc[]
===== Response Body
include::{snippets}/user-library/delete/response-body.adoc[]
include::{snippets}/user-library/delete/response-fields.adoc[]

=== 독서 상태 페이지 조회

==== HTTP Request
include::{snippets}/user-library/get-status/http-request.adoc[]
include::{snippets}/user-library/get-status/query-parameters.adoc[]

==== HTTP Response
include::{snippets}/user-library/get-status/http-response.adoc[]
===== Response Body
include::{snippets}/user-library/get-status/response-body.adoc[]
include::{snippets}/user-library/get-status/response-fields.adoc[]
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,17 @@ public RatingCreateResponse creatingRating(Long userId, RatingCreateServiceReque

public RatingGetOneResponse getRating(Long userId, String isbn) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));
.orElse(null);

Book book = bookRepository.findById(isbn)
.orElseThrow(() -> new BusinessException(ErrorCode.BOOK_NOT_FOUND));

Rating rating = ratingRepository.findRatingByUserAndBook(user, book)
.orElseThrow(() -> new BusinessException(RATING_NOT_FOUND));
.orElse(null);

if (rating == null || user == null) {
return RatingGetOneResponse.of(null, null, book.getIsbn());
}

return RatingGetOneResponse.of(rating.getId(), rating.getRating(), book.getIsbn());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
@NoArgsConstructor
public class UserReadingStatusResponse {

private String isbn;

private String bookImage;

private String bookTitle;
Expand All @@ -17,7 +19,8 @@ public class UserReadingStatusResponse {

@QueryProjection
@Builder
public UserReadingStatusResponse(String bookImage, String bookTitle, Double ratingAvg) {
public UserReadingStatusResponse(String isbn, String bookImage, String bookTitle, Double ratingAvg) {
this.isbn = isbn;
this.bookImage = bookImage;
this.bookTitle = bookTitle;
this.ratingAvg = ratingAvg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public PageResponse<UserReadingStatusResponse> findAllReadingStatusOrderBy(
Long userId, ReadingStatus readingStatus, ReadingStatusOrderType orderType, int size, int offset) {
log.info("--------------start--------------");
List<UserReadingStatusResponse> userReadingStatuses = queryFactory
.select(new QUserReadingStatusResponse(userLibrary.book.imageUrl, userLibrary.book.title, rating1.rating.avg()))
.select(new QUserReadingStatusResponse(
userLibrary.book.isbn, userLibrary.book.imageUrl, userLibrary.book.title, rating1.rating.avg()))
.from(userLibrary)
.join(rating1).on(userLibrary.book.eq(rating1.book))
.where(userLibrary.user.id.eq(userId), userLibrary.status.eq(readingStatus))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void createRatingTwice() {
.hasMessage("이미 별점이 존재합니다.");
}

@DisplayName("유저가 해당 책의 별점을 조회한다.")
@DisplayName("유저가 해당 등록한 별점을 조회한다.")
@Test
void getRating() {
//given
Expand All @@ -102,6 +102,37 @@ void getRating() {
assertThat(result.getIsbn()).isEqualTo(savadRating.getBook().getIsbn());
}

@DisplayName("유저가 해당 등록하지 않은 책의 별점을 조회한다.")
@Test
void getRatingWithEmpty() {
//given
User user = userRepository.save(createUser("1"));
Book book = bookRepository.save(createBook("제목1", "내용1", "1234"));

//when
RatingGetOneResponse result = ratingService.getRating(user.getId(), book.getIsbn());

//then
assertThat(result.getId()).isNull();
assertThat(result.getRating()).isNull();
assertThat(result.getIsbn()).isEqualTo(book.getIsbn());
}

@DisplayName("게스트가 책의 별점을 조회한다.")
@Test
void getRatingWithGuest() {
//given
Book book = bookRepository.save(createBook("제목1", "내용1", "1234"));

//when
RatingGetOneResponse result = ratingService.getRating(-1L, book.getIsbn());

//then
assertThat(result.getId()).isNull();
assertThat(result.getRating()).isNull();
assertThat(result.getIsbn()).isEqualTo(book.getIsbn());
}

@DisplayName("유저가 별점을 수정한다.")
@Test
void updateRating() {
Expand Down
82 changes: 82 additions & 0 deletions src/test/java/com/jisungin/docs/user/UserControllerDocsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
import com.jisungin.application.user.UserService;
import com.jisungin.application.user.request.ReviewContentGetAllServiceRequest;
import com.jisungin.application.user.request.UserRatingGetAllServiceRequest;
import com.jisungin.application.user.request.UserReadingStatusGetAllServiceRequest;
import com.jisungin.application.user.response.UserInfoResponse;
import com.jisungin.application.userlibrary.response.UserReadingStatusResponse;
import com.jisungin.docs.RestDocsSupport;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -192,6 +194,75 @@ void getUserReviews() throws Exception {
));
}

@DisplayName("유저 독서 상태 페이징 조회 API")
@Test
void getReadingStatuses() throws Exception {
List<UserReadingStatusResponse> readingStatusesResponse = createReadingStatusResponse();

PageResponse<UserReadingStatusResponse> response = PageResponse.<UserReadingStatusResponse>builder()
.size(10)
.totalCount(10)
.queryResponse(readingStatusesResponse)
.build();

given(userService.getUserReadingStatuses(anyLong(), any(UserReadingStatusGetAllServiceRequest.class)))
.willReturn(response);

mockMvc.perform(get("/v1/users/statuses")
.param("page", "1")
.param("size", "10")
.param("order", "dictionary")
.param("status", "want")
.contentType(APPLICATION_JSON)
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.code").value("200"))
.andExpect(jsonPath("$.status").value("OK"))
.andExpect(jsonPath("$.message").value("OK"))
.andDo(print())
.andDo(document("user-library/get-status",
preprocessRequest(prettyPrint()),
preprocessResponse(prettyPrint()),
queryParameters(
parameterWithName("page")
.description("페이지 번호"),
parameterWithName("size")
.description("페이지 사이즈"),
parameterWithName("order")
.description(
"정렬 기준 : dictionary(가나다순), rating_avg_desc(평균 별점 높은 순)"),
parameterWithName("status")
.description(
"선택 기준 : want(읽고 싶은), reading(읽는 중), pause(잠시 멈춤)," +
"stop(중단), none(상관없음)")
),
responseFields(
fieldWithPath("code").type(JsonFieldType.NUMBER)
.description("코드"),
fieldWithPath("status").type(JsonFieldType.STRING)
.description("상태"),
fieldWithPath("message").type(JsonFieldType.STRING)
.description("메시지"),
fieldWithPath("data").type(JsonFieldType.OBJECT)
.description("응답 데이터"),
fieldWithPath("data.queryResponse").type(JsonFieldType.ARRAY)
.description("책 목록"),
fieldWithPath("data.queryResponse[].isbn").type(JsonFieldType.STRING)
.description("책 isbn"),
fieldWithPath("data.queryResponse[].bookTitle").type(JsonFieldType.STRING)
.description("책 제목"),
fieldWithPath("data.queryResponse[].bookImage").type(JsonFieldType.STRING)
.description("책 표지"),
fieldWithPath("data.queryResponse[].ratingAvg").type(JsonFieldType.NUMBER)
.description("책 평균 별점"),
fieldWithPath("data.totalCount").type(JsonFieldType.NUMBER)
.description("데이터 총 개수"),
fieldWithPath("data.size").type(JsonFieldType.NUMBER)
.description("해당 페이지 데이터 개수")
)
));
}

@DisplayName("유저 상세 조회 API")
@Test
void getUserInfo() throws Exception {
Expand Down Expand Up @@ -263,4 +334,15 @@ private List<Long> createLikeReviewIds() {
.collect(Collectors.toList());
}

private List<UserReadingStatusResponse> createReadingStatusResponse() {
return IntStream.range(0, 10)
.mapToObj(i -> UserReadingStatusResponse.builder()
.isbn(String.valueOf(i))
.bookTitle("책 제목" + i)
.bookImage("책 표지" + i)
.ratingAvg(i % 5.0 + 1)
.build())
.toList();
}

}
Loading