Skip to content

Commit

Permalink
✏️ [FIX] 레큐북 즐겨찾기 여부까지 넘겨주는 API 추가 #76
Browse files Browse the repository at this point in the history
  • Loading branch information
ddongseop committed Mar 13, 2024
1 parent e2912fd commit 0684dff
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.dto.request.BookCreateRequestDto;
import org.sopt.lequuServer.domain.book.dto.response.BookCreateResponseDto;
import org.sopt.lequuServer.domain.book.dto.response.BookDetailFavoriteResponseDto;
import org.sopt.lequuServer.domain.book.dto.response.BookDetailResponseDto;
import org.sopt.lequuServer.domain.book.facade.BookFacade;
import org.sopt.lequuServer.global.auth.jwt.JwtProvider;
Expand Down Expand Up @@ -37,4 +38,9 @@ public ResponseEntity<?> deleteBook(@PathVariable Long bookId) {
public ResponseEntity<ApiResponse<BookDetailResponseDto>> getBookDetail(@PathVariable String bookUuid) {
return ResponseEntity.ok(ApiResponse.success(SuccessType.GET_BOOK_DETAIL_SUCCESS, bookFacade.getBookDetail(bookUuid)));
}

@GetMapping("/favorite/{bookUuid}")
public ResponseEntity<ApiResponse<BookDetailFavoriteResponseDto>> getBookDetailFavorite(Principal principal, @PathVariable String bookUuid) {
return ResponseEntity.ok(ApiResponse.success(SuccessType.GET_BOOK_DETAIL_SUCCESS, bookFacade.getBookDetailFavorite(JwtProvider.getUserFromPrincial(principal), bookUuid)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package org.sopt.lequuServer.domain.book.dto.response;

import io.swagger.v3.oas.annotations.media.Schema;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.member.model.Member;
import org.sopt.lequuServer.domain.note.dto.response.NoteDetailResponseDto;
import org.sopt.lequuServer.domain.note.model.Note;
import org.sopt.lequuServer.domain.sticker.dto.response.PostedStickerDetailResponseDto;
import org.sopt.lequuServer.domain.sticker.model.PostedSticker;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

import static java.util.Comparator.comparing;

public record BookDetailFavoriteResponseDto(

@Schema(description = "레큐북 고유 id", example = "1")
Long bookId,

@Schema(description = "레큐북 즐겨찾기 등록 여부", example = "true")
Boolean isFavorite,

@Schema(description = "최애 사진", example = "https://dzfv99wxq6tx0.cloudfront.net/books/favorite_image/b4006561-382b-479e-ae1d-e841922e883f.jpg")
String favoriteImage,

@Schema(description = "최애 이름", example = "LeoJ")
String favoriteName,

@Schema(description = "레큐북 제목", example = "1번째 레큐북")
String title,

@Schema(description = "레큐북 소개", example = "레큐북의 내용입니다.")
String description,

@Schema(description = "레큐북 생성 일시", example = "2024.01.11")
String bookDate,

@Schema(description = "레큐북 작성자 닉네임", example = "예딘")
String bookNickname,

@Schema(description = "레큐북 배경색", example = "#F5F5F5")
String bookBackgroundColor,

@Schema(description = "레큐 노트 개수", example = "100")
int noteNum,

List<NoteDetailResponseDto> noteList,

List<PostedStickerDetailResponseDto> postedStickerList
) {
public static BookDetailFavoriteResponseDto of(Member member, Book book) {
String bookDate = formatLocalDate(book);

List<Note> sortedNotes = book.getNotes().stream()
.sorted(comparing(Note::getCreatedAt).reversed())
.toList();

// 레큐노트 리스트 가공
int renderTypeCounter = 1;
List<NoteDetailResponseDto> noteList = new ArrayList<>();
for (Note note : sortedNotes) {
noteList.add(NoteDetailResponseDto.of(note, renderTypeCounter));
renderTypeCounter = (renderTypeCounter % 6 == 0) ? 1 : renderTypeCounter + 1;
}

// 부착된 스티커 리스트 가공
List<PostedSticker> postedStickers = book.getPostedStickers();
List<PostedStickerDetailResponseDto> postedStickerList = new ArrayList<>();
for (PostedSticker postedSticker : postedStickers) {
postedStickerList.add(PostedStickerDetailResponseDto.of(postedSticker));
}

Boolean isFavorite = member.getFavorites().stream()
.anyMatch(favorite -> favorite.getBook().equals(book));

return new BookDetailFavoriteResponseDto(book.getId(), isFavorite,
book.getFavoriteImage(), book.getFavoriteName(),
book.getTitle(), book.getDescription(), bookDate, book.getMember().getNickname(),
book.getBackgroundColor(), book.getNotes().size(), noteList, postedStickerList
);
}

private static String formatLocalDate(Book book) {
LocalDateTime createdAt = book.getCreatedAt();
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy.MM.dd");
return createdAt.format(formatter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ public record BookDetailResponseDto(
@Schema(description = "레큐북 고유 id", example = "1")
Long bookId,

@Schema(description = "레큐북 즐겨찾기 등록 여부", example = "true")
Boolean isFavorite,

@Schema(description = "최애 사진", example = "https://dzfv99wxq6tx0.cloudfront.net/books/favorite_image/b4006561-382b-479e-ae1d-e841922e883f.jpg")
String favoriteImage,

Expand Down Expand Up @@ -72,10 +69,7 @@ public static BookDetailResponseDto of(Book book) {
postedStickerList.add(PostedStickerDetailResponseDto.of(postedSticker));
}

Boolean isFavorite = book.getMember().getFavorites().stream()
.anyMatch(favorite -> favorite.getBook().equals(book));

return new BookDetailResponseDto(book.getId(), isFavorite,
return new BookDetailResponseDto(book.getId(),
book.getFavoriteImage(), book.getFavoriteName(),
book.getTitle(), book.getDescription(), bookDate, book.getMember().getNickname(),
book.getBackgroundColor(), book.getNotes().size(), noteList, postedStickerList
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.RequiredArgsConstructor;
import org.sopt.lequuServer.domain.book.dto.request.BookCreateRequestDto;
import org.sopt.lequuServer.domain.book.dto.response.BookCreateResponseDto;
import org.sopt.lequuServer.domain.book.dto.response.BookDetailFavoriteResponseDto;
import org.sopt.lequuServer.domain.book.dto.response.BookDetailResponseDto;
import org.sopt.lequuServer.domain.book.model.Book;
import org.sopt.lequuServer.domain.book.repository.BookRepository;
Expand Down Expand Up @@ -58,14 +59,14 @@ public BookCreateResponseDto createBook(BookCreateRequestDto request, Long membe
String imageUrl = s3Service.getCloudFrontURL(BOOK_FAVORITE_IMAGE_FOLDER_NAME.getValue() + request.favoriteImage());

Book book = Book.builder()
.uuid(bookUuid)
.favoriteName(badWordFilterService.badWordChange(memberId, request.favoriteName()))
.favoriteImage(imageUrl)
.title(badWordFilterService.badWordChange(memberId, request.title()))
.description(badWordFilterService.badWordChange(memberId, request.description()))
.backgroundColor(request.backgroundColor())
.member(member)
.build();
.uuid(bookUuid)
.favoriteName(badWordFilterService.badWordChange(memberId, request.favoriteName()))
.favoriteImage(imageUrl)
.title(badWordFilterService.badWordChange(memberId, request.title()))
.description(badWordFilterService.badWordChange(memberId, request.description()))
.backgroundColor(request.backgroundColor())
.member(member)
.build();

return bookService.createBook(book, member);
}
Expand Down Expand Up @@ -102,4 +103,11 @@ public BookDetailResponseDto getBookDetail(String bookUuid) {
return BookDetailResponseDto.of(book);
}

public BookDetailFavoriteResponseDto getBookDetailFavorite(Long userId, String bookUuid) {
Book book = bookRepository.findByUuidOrThrow(bookUuid);
Member member = memberRepository.findByIdOrThrow(userId);

return BookDetailFavoriteResponseDto.of(member, book);
}

}

0 comments on commit 0684dff

Please sign in to comment.