Skip to content

Commit

Permalink
Merge pull request #113 from 2023-Team-Joon-CheckIt/BE/fix/#112
Browse files Browse the repository at this point in the history
[fix/#112] 결과 메시지 수정 + pr 메시지 기반 리팩토링
  • Loading branch information
gmlrude authored May 6, 2024
2 parents ec21af5 + 8120a63 commit b2f4f74
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.List;

import static com.techeer.checkIt.global.result.ResultCode.GET_LIKE_BOOK_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.GET_NEW_BOOK_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.UPDATE_BOOK_LIKE_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.GET_ONE_BOOK_SUCCESS;
Expand Down Expand Up @@ -72,6 +73,6 @@ public ResponseEntity<ResultResponse> updateLikeById(
@GetMapping("/like")
public ResponseEntity<ResultResponse> getLikeBooksList() {
Page<BookSearchLikeRes> books = bookService.sortedBooksByLike();
return ResponseEntity.ok(ResultResponse.of(GET_NEW_BOOK_SUCCESS, books));
return ResponseEntity.ok(ResultResponse.of(GET_LIKE_BOOK_SUCCESS, books));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.techeer.checkIt.domain.book.dto.Response;

import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import lombok.AccessLevel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;

@Getter
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
public class BookReadingRes {
private Long id;
private String title;
private String author;
private String publisher;
private String coverImageUrl;
private int height;
private int width;
private int pages;
private int likes;
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.techeer.checkIt.domain.book.mapper;

import com.techeer.checkIt.domain.book.dto.Response.BookReadingRes;
import com.techeer.checkIt.domain.book.dto.Response.BookRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchLikeRes;
import com.techeer.checkIt.domain.book.dto.Response.BookSearchRes;
Expand Down Expand Up @@ -56,16 +57,27 @@ public List<BookSearchRes> toSearchDtoList(List<BookDocument> books){
.map(this::toBookSearchDto)
.collect(Collectors.toList());
}
// toPageDtoList = toBookSearchResDtoPage
public Page<BookSearchRes> toPageDtoList(Page<BookDocument> books) {
public Page<BookSearchRes> toBookSearchResDtoPage(Page<BookDocument> books) {
return new PageImpl<>(books.stream()
.map(this::toBookSearchDto)
.collect(Collectors.toList()));
}
// toPageDtoList2 = BookSearchLikeResDtoPage
public Page<BookSearchLikeRes> toPageDtoList2(Page<Book> books) {
public Page<BookSearchLikeRes> BookSearchLikeResDtoPage(Page<Book> books) {
return new PageImpl<>(books.stream()
.map(this::toBookSearchLikeDto)
.collect(Collectors.toList()));
}
public BookReadingRes toDtoByBook(Book book) {
return BookReadingRes.builder()
.id(book.getId())
.title(book.getTitle())
.author(book.getAuthor())
.publisher(book.getPublisher())
.coverImageUrl(book.getCoverImageUrl())
.height(book.getHeight())
.width(book.getWidth())
.pages(book.getPages())
.likes(book.getLikeCount())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ public List<BookSearchRes> findBookByTitle(String title) {
public Page<BookSearchRes> sortedBooksByTime() {
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Order.desc("createdAt")));
Page<BookDocument> newBooks = bookSearchRepository.findAll(pageRequest);
return bookMapper.toPageDtoList(newBooks);
return bookMapper.toBookSearchResDtoPage(newBooks);
}

public Page<BookSearchLikeRes> sortedBooksByLike() {
PageRequest pageRequest = PageRequest.of(0, 10, Sort.by(Sort.Order.desc("likeCount")));
Page<Book> newBooks = bookJpaRepository.findAll(pageRequest);
return bookMapper.toPageDtoList2(newBooks);
return bookMapper.BookSearchLikeResDtoPage(newBooks);
}

// id별 조회할 때
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.techeer.checkIt.domain.reading.dto.response;

import com.techeer.checkIt.domain.book.dto.Response.BookReadingRes;
import com.techeer.checkIt.domain.book.dto.Response.BookRes;
import com.techeer.checkIt.domain.reading.entity.ReadingStatus;
import lombok.AccessLevel;
Expand All @@ -13,6 +14,6 @@
@Builder
@AllArgsConstructor(access = AccessLevel.PRIVATE)
public class ReadingRes {
private List<BookRes> bookInfos;
private List<BookReadingRes> bookInfos;
private ReadingStatus status;
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package com.techeer.checkIt.domain.reading.mapper;

import com.techeer.checkIt.domain.book.dto.Response.BookReadingRes;
import com.techeer.checkIt.domain.book.entity.Book;
import com.techeer.checkIt.domain.book.dto.Response.BookRes;

import com.techeer.checkIt.domain.book.mapper.BookMapper;
import com.techeer.checkIt.domain.reading.dto.response.ReadingRes;
import com.techeer.checkIt.domain.reading.dto.response.UpdateLastPageAndPercentageRes;
import com.techeer.checkIt.domain.reading.dto.response.UpdateReadingAndReadingVolumeRes;
import com.techeer.checkIt.domain.reading.entity.Reading;
import com.techeer.checkIt.domain.reading.entity.ReadingStatus;
import com.techeer.checkIt.domain.readingVolume.entity.ReadingVolume;
import com.techeer.checkIt.domain.user.entity.User;
import lombok.AccessLevel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component
@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
public class ReadingMapper {

private final BookMapper bookMapper;

public Reading toEntity(User user, Book book, int lastPage, ReadingStatus status) {
return Reading.builder()
.user(user)
Expand All @@ -26,8 +31,8 @@ public Reading toEntity(User user, Book book, int lastPage, ReadingStatus status
.status(status)
.build();
}
public BookRes toDto(Reading reading) {
return BookRes.builder()
public BookReadingRes toDto(Reading reading) {
return BookReadingRes.builder()
.id(reading.getBook().getId())
.title(reading.getBook().getTitle())
.author(reading.getBook().getAuthor())
Expand All @@ -36,25 +41,12 @@ public BookRes toDto(Reading reading) {
.height(reading.getBook().getHeight())
.width(reading.getBook().getWidth())
.pages(reading.getBook().getPages())
.build();
}

// TODO: int likes, boolean likeStatus 을 안 받기 때문에 값 안 가져옴
public BookRes toDtoByBook(Book book) {
return BookRes.builder()
.id(book.getId())
.title(book.getTitle())
.author(book.getAuthor())
.publisher(book.getPublisher())
.coverImageUrl(book.getCoverImageUrl())
.height(book.getHeight())
.width(book.getWidth())
.pages(book.getPages())
.likes(reading.getBook().getLikeCount())
.build();
}

public ReadingRes toReadingList(List<Reading> readings, ReadingStatus status) {
List<BookRes> bookInfos = readings.stream().map(this::toDto).collect(Collectors.toList());
List<BookReadingRes> bookInfos = readings.stream().map(this::toDto).collect(Collectors.toList());

return ReadingRes.builder()
.bookInfos(bookInfos)
Expand All @@ -63,7 +55,7 @@ public ReadingRes toReadingList(List<Reading> readings, ReadingStatus status) {
}

public ReadingRes toReadingListByBook(List<Book> books, ReadingStatus status) {
List<BookRes> bookInfos = books.stream().map(this::toDtoByBook).collect(Collectors.toList());
List<BookReadingRes> bookInfos = books.stream().map(bookMapper::toDtoByBook).collect(Collectors.toList());

return ReadingRes.builder()
.bookInfos(bookInfos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.techeer.checkIt.domain.user.entity.User;
import com.techeer.checkIt.domain.user.entity.UserDetail;
import com.techeer.checkIt.domain.user.service.UserService;
import com.techeer.checkIt.global.result.ResultCode;
import com.techeer.checkIt.global.result.ResultResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
Expand All @@ -26,6 +25,11 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static com.techeer.checkIt.global.result.ResultCode.GET_REVIEW_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.REVIEW_CREATE_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.REVIEW_DELETE_SUCCESS;
import static com.techeer.checkIt.global.result.ResultCode.REVIEW_UPDATE_SUCCESS;

@Api(tags = "리뷰 API")
@RestController
@RequiredArgsConstructor(access = AccessLevel.PRIVATE)
Expand All @@ -45,7 +49,7 @@ public ResponseEntity<ResultResponse> createReview(
Book book = bookService.findById(createReviewReq.getBookId());
reviewService.createReview(user, book, createReviewReq);
ReviewRes reviewRes = reviewService.findReviewByUserNameIdBookId(user, createReviewReq.getBookId());
return ResponseEntity.ok(ResultResponse.of(ResultCode.REVIEW_CREATE_SUCCESS, reviewRes));
return ResponseEntity.ok(ResultResponse.of(REVIEW_CREATE_SUCCESS, reviewRes));
}

@ApiOperation(value = "리뷰 조회 API")
Expand All @@ -56,7 +60,7 @@ public ResponseEntity<ResultResponse> getReviewByUserNameBookId(
){
User user = userService.findUserByUsername(userDetail.getUsername());
ReviewRes reviewRes = reviewService.findReviewByUserNameIdBookId(user, bookId);
return ResponseEntity.ok(ResultResponse.of(ResultCode.REVIEW_CREATE_SUCCESS, reviewRes));
return ResponseEntity.ok(ResultResponse.of(GET_REVIEW_SUCCESS, reviewRes));
}

@ApiOperation(value = "리뷰 삭제 API")
Expand All @@ -67,7 +71,7 @@ public ResponseEntity<ResultResponse> deleteReviewByUserNameBookId(
){
User user = userService.findUserByUsername(userDetail.getUsername());
reviewService.deleteReview(user, bookId);
return ResponseEntity.ok(ResultResponse.of(ResultCode.REVIEW_DELETE_SUCCESS));
return ResponseEntity.ok(ResultResponse.of(REVIEW_DELETE_SUCCESS));
}

@ApiOperation(value = "리뷰 내용 변경 API")
Expand All @@ -79,7 +83,7 @@ public ResponseEntity<ResultResponse> updateReview(
User user = userService.findUserByUsername(userDetail.getUsername());
reviewService.updateReview(user, createReviewReq);
ReviewRes reviewRes = reviewService.findReviewByUserNameIdBookId(user, createReviewReq.getBookId());
return ResponseEntity.ok(ResultResponse.of(ResultCode.REVIEW_DELETE_SUCCESS, reviewRes));
return ResponseEntity.ok(ResultResponse.of(REVIEW_UPDATE_SUCCESS, reviewRes));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public enum ResultCode {
GET_NEW_BOOK_SUCCESS("B001","신규 도서 조회 성공"),
UPDATE_BOOK_LIKE_SUCCESS("B002","좋아요 갱신 성공"),
GET_ONE_BOOK_SUCCESS("B003","책 조회 성공"),
GET_LIKE_BOOK_SUCCESS("B004","인기있는 도서 조회 성공"),


// Reading
Expand Down

0 comments on commit b2f4f74

Please sign in to comment.