From 0684dffa876f97b6c28c90b82564616260b11f4d Mon Sep 17 00:00:00 2001 From: ddongseop Date: Wed, 13 Mar 2024 22:14:49 +0900 Subject: [PATCH] =?UTF-8?q?:pencil2:=20[FIX]=20=EB=A0=88=ED=81=90=EB=B6=81?= =?UTF-8?q?=20=EC=A6=90=EA=B2=A8=EC=B0=BE=EA=B8=B0=20=EC=97=AC=EB=B6=80?= =?UTF-8?q?=EA=B9=8C=EC=A7=80=20=EB=84=98=EA=B2=A8=EC=A3=BC=EB=8A=94=20API?= =?UTF-8?q?=20=EC=B6=94=EA=B0=80=20#76?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../book/controller/BookController.java | 6 ++ .../BookDetailFavoriteResponseDto.java | 91 +++++++++++++++++++ .../dto/response/BookDetailResponseDto.java | 8 +- .../domain/book/facade/BookFacade.java | 24 +++-- 4 files changed, 114 insertions(+), 15 deletions(-) create mode 100644 src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailFavoriteResponseDto.java diff --git a/src/main/java/org/sopt/lequuServer/domain/book/controller/BookController.java b/src/main/java/org/sopt/lequuServer/domain/book/controller/BookController.java index 81c4db9..d2fbe81 100644 --- a/src/main/java/org/sopt/lequuServer/domain/book/controller/BookController.java +++ b/src/main/java/org/sopt/lequuServer/domain/book/controller/BookController.java @@ -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; @@ -37,4 +38,9 @@ public ResponseEntity deleteBook(@PathVariable Long bookId) { public ResponseEntity> getBookDetail(@PathVariable String bookUuid) { return ResponseEntity.ok(ApiResponse.success(SuccessType.GET_BOOK_DETAIL_SUCCESS, bookFacade.getBookDetail(bookUuid))); } + + @GetMapping("/favorite/{bookUuid}") + public ResponseEntity> getBookDetailFavorite(Principal principal, @PathVariable String bookUuid) { + return ResponseEntity.ok(ApiResponse.success(SuccessType.GET_BOOK_DETAIL_SUCCESS, bookFacade.getBookDetailFavorite(JwtProvider.getUserFromPrincial(principal), bookUuid))); + } } \ No newline at end of file diff --git a/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailFavoriteResponseDto.java b/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailFavoriteResponseDto.java new file mode 100644 index 0000000..8e0477a --- /dev/null +++ b/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailFavoriteResponseDto.java @@ -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 noteList, + + List postedStickerList +) { + public static BookDetailFavoriteResponseDto of(Member member, Book book) { + String bookDate = formatLocalDate(book); + + List sortedNotes = book.getNotes().stream() + .sorted(comparing(Note::getCreatedAt).reversed()) + .toList(); + + // 레큐노트 리스트 가공 + int renderTypeCounter = 1; + List noteList = new ArrayList<>(); + for (Note note : sortedNotes) { + noteList.add(NoteDetailResponseDto.of(note, renderTypeCounter)); + renderTypeCounter = (renderTypeCounter % 6 == 0) ? 1 : renderTypeCounter + 1; + } + + // 부착된 스티커 리스트 가공 + List postedStickers = book.getPostedStickers(); + List 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); + } +} \ No newline at end of file diff --git a/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailResponseDto.java b/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailResponseDto.java index d1fe183..d5f0640 100644 --- a/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailResponseDto.java +++ b/src/main/java/org/sopt/lequuServer/domain/book/dto/response/BookDetailResponseDto.java @@ -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, @@ -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 diff --git a/src/main/java/org/sopt/lequuServer/domain/book/facade/BookFacade.java b/src/main/java/org/sopt/lequuServer/domain/book/facade/BookFacade.java index 7ce0072..06fe29f 100644 --- a/src/main/java/org/sopt/lequuServer/domain/book/facade/BookFacade.java +++ b/src/main/java/org/sopt/lequuServer/domain/book/facade/BookFacade.java @@ -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; @@ -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); } @@ -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); + } + } \ No newline at end of file