From a5116eedecbbdd54cd52be93ae94218bc6ed8b4b Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Wed, 27 Nov 2024 16:13:50 +0900 Subject: [PATCH 01/29] =?UTF-8?q?feat(ReviewRepository,=20ReviewRepository?= =?UTF-8?q?Test):=20=EB=A0=88=ED=8F=AC=EC=A7=80=ED=86=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/repository/ReviewRepository.java | 11 +++++ .../repository/ReviewRepositoryTest.java | 44 +++++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java create mode 100644 src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java diff --git a/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java b/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java new file mode 100644 index 0000000..01a031c --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java @@ -0,0 +1,11 @@ +package com.bookmile.backend.domain.review.repository; + +import com.bookmile.backend.domain.review.entity.Review; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ReviewRepository extends JpaRepository { + + List findAllByBookId(Long bookId); + +} diff --git a/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java b/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java new file mode 100644 index 0000000..7ad484a --- /dev/null +++ b/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java @@ -0,0 +1,44 @@ +package com.bookmile.backend.domain.review.repository; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.bookmile.backend.domain.book.entity.Book; +import com.bookmile.backend.domain.review.entity.Review; +import com.bookmile.backend.domain.user.entity.User; +import java.util.List; +import org.junit.jupiter.api.Test; +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.autoconfigure.orm.jpa.TestEntityManager; + +@DataJpaTest +@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) +class ReviewRepositoryTest { + + @Autowired + private ReviewRepository reviewRepository; + + @Autowired + private TestEntityManager testEntityManager; + + @Test + void 책_리뷰_조회_올바른_갯수_반환_테스트() { + //Given + User user1 = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book1 = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + + testEntityManager.persist(user1); + testEntityManager.persist(book1); + + Review review1 = new Review(user1, book1, 4.5, "굳이에요 굳"); + reviewRepository.save(review1); + + //When + List reviews = reviewRepository.findAllByBookId(book1.getId()); + + //Then + assertEquals(1, reviews.size()); + + } +} \ No newline at end of file From f88b1d600d4c9603b04d6e5f0992bda72fa1da17 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Wed, 27 Nov 2024 16:29:42 +0900 Subject: [PATCH 02/29] =?UTF-8?q?feat(ReviewRepository,=20ReviewRepository?= =?UTF-8?q?Test):=20=EB=A0=88=ED=8F=AC=EC=A7=80=ED=86=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=B6=94=EA=B0=80=20=EC=9E=91=EC=84=B1=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 사용자 ID로 리뷰 리스트 조회 레포지토리 기능 구현 및 테스트 --- .../review/repository/ReviewRepository.java | 2 ++ .../repository/ReviewRepositoryTest.java | 25 ++++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java b/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java index 01a031c..928ae41 100644 --- a/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java +++ b/src/main/java/com/bookmile/backend/domain/review/repository/ReviewRepository.java @@ -8,4 +8,6 @@ public interface ReviewRepository extends JpaRepository { List findAllByBookId(Long bookId); + List findAllByUserId(Long userId); + } diff --git a/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java b/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java index 7ad484a..ea0b5e3 100644 --- a/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/repository/ReviewRepositoryTest.java @@ -23,7 +23,7 @@ class ReviewRepositoryTest { private TestEntityManager testEntityManager; @Test - void 책_리뷰_조회_올바른_갯수_반환_테스트() { + void 책_리뷰_책_ID로_조회_올바른_갯수_반환_테스트() { //Given User user1 = new User("김진용", "kje@naver.com", "1234", "urlurl"); Book book1 = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); @@ -39,6 +39,29 @@ class ReviewRepositoryTest { //Then assertEquals(1, reviews.size()); + } + + + @Test + void 책_리뷰_사용자_ID로_조회_올바른_갯수_반환_테스트() { + //Given + User user1 = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book1 = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + Book book2 = new Book("김진용의 인생2", 456, "image", "김진용", "책설명", "링크url", 5.0); + + testEntityManager.persist(user1); + testEntityManager.persist(book1); + testEntityManager.persist(book2); + + Review review1 = new Review(user1, book1, 4.5, "굳이에요 굳"); + Review review2 = new Review(user1, book2, 4.5, "이번에도 굳이에요 굳"); + reviewRepository.save(review1); + reviewRepository.save(review2); + + //When + List reviews = reviewRepository.findAllByUserId(user1.getId()); + //Then + assertEquals(2, reviews.size()); } } \ No newline at end of file From c7cf47143dcf9230097c7269967c50cc32994efc Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Wed, 27 Nov 2024 18:31:49 +0900 Subject: [PATCH 03/29] =?UTF-8?q?feat(ReviewService,=20ReviewListResponse,?= =?UTF-8?q?=20ReviewServiceTest):=20=EB=A6=AC=EB=B7=B0=20=EB=A6=AC?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20?= =?UTF-8?q?=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 서비스 계층까지 구현 --- .../domain/review/dto/ReviewListResponse.java | 23 ++++++++++ .../domain/review/service/ReviewService.java | 31 +++++++++++++ .../review/service/ReviewServiceTest.java | 46 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java create mode 100644 src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java create mode 100644 src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java diff --git a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java new file mode 100644 index 0000000..bb1be5a --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java @@ -0,0 +1,23 @@ +package com.bookmile.backend.domain.review.dto; + +import com.bookmile.backend.domain.review.entity.Review; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ReviewListResponse { + private Long userId; + private String name; + private double rating; + private String text; + + public static ReviewListResponse createReview(Review review) { + return new ReviewListResponse( + review.getId(), + review.getUser().getName(), + review.getRating(), + review.getText() + ); + } +} diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java new file mode 100644 index 0000000..aa2ce22 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -0,0 +1,31 @@ +package com.bookmile.backend.domain.review.service; + +import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.repository.ReviewRepository; +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ReviewService { + private final ReviewRepository reviewRepository; +// private final BookRepository bookRepository; + + + public List viewReviewList(Long bookId) { + // 책 조회 및 예외처리 +// validateBook(bookId); + + // 리뷰 리스트 조회 및 엔티티 -> DTO 변환 + return reviewRepository.findAllByBookId(bookId).stream() + .map(ReviewListResponse::createReview) + .collect(Collectors.toList()); + } + +// private void validateBook(Long bookId){ +// bookRepository.findById(bookId) +// .orElseThrow(() -> new IllegalArgumentException("없는 소원입니다.")); +// } +} \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java new file mode 100644 index 0000000..1eb8c8f --- /dev/null +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -0,0 +1,46 @@ +package com.bookmile.backend.domain.review.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +import com.bookmile.backend.domain.book.entity.Book; +import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.entity.Review; +import com.bookmile.backend.domain.review.repository.ReviewRepository; +import com.bookmile.backend.domain.user.entity.User; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class ReviewServiceTest { + + @Mock + private ReviewRepository reviewRepository; + + @InjectMocks + private ReviewService reviewService; + + @Test + void Book_Id로_리뷰_리스트_가져오기() { + //Given + Long bookId = 1L; + User user1 = new User("김진용", "kje@naver.com", "1234", "urlurl"); + User user2 = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + + Review review1 = new Review(user1, book, 4.5, "굳이에요 굳"); + Review review2 = new Review(user2, book, 4.5, "굳이에요 굳"); + + when(reviewRepository.findAllByBookId(bookId)).thenReturn(List.of(review1, review2)); + + //When + List reviews = reviewService.viewReviewList(bookId); + + //Then + assertEquals(2, reviews.size()); + } +} \ No newline at end of file From 42e2189253d72c733e89fb49ce1e51adc0ef7981 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Wed, 27 Nov 2024 18:57:41 +0900 Subject: [PATCH 04/29] =?UTF-8?q?feat(ReviewController):=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=EC=A1=B0=ED=9A=8C=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EB=AA=A8=EB=91=90=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java new file mode 100644 index 0000000..287be74 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -0,0 +1,24 @@ +package com.bookmile.backend.domain.review.controller; + +import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.service.ReviewService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/reviews") +@RequiredArgsConstructor +public class ReviewController { + private final ReviewService reviewService; + + @GetMapping + public ResponseEntity> viewReviewList(@RequestParam Long bookId) { + List reviews = reviewService.viewReviewList(bookId); + return ResponseEntity.ok(reviews); + } +} From e86f5341612f5332502614d1c600bd40304d703b Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Wed, 27 Nov 2024 23:41:48 +0900 Subject: [PATCH 05/29] =?UTF-8?q?feat(ReviewService,=20ReviewRequest,=20Re?= =?UTF-8?q?viewListResponse,=20Review,=20ReviewController):=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 이어서 Mock 기반 테스트 코드 작성 --- .../review/controller/ReviewController.java | 10 +++++++ .../domain/review/dto/ReviewListResponse.java | 2 +- .../domain/review/dto/ReviewRequest.java | 11 +++++++ .../backend/domain/review/entity/Review.java | 10 +++++++ .../domain/review/service/ReviewService.java | 29 ++++++++++++++----- 5 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java index 287be74..7f13b6e 100644 --- a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -1,11 +1,14 @@ package com.bookmile.backend.domain.review.controller; import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.dto.ReviewRequest; import com.bookmile.backend.domain.review.service.ReviewService; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -21,4 +24,11 @@ public ResponseEntity> viewReviewList(@RequestParam Lon List reviews = reviewService.viewReviewList(bookId); return ResponseEntity.ok(reviews); } + + @PostMapping + public ResponseEntity createReview(@RequestParam Long bookId, @RequestParam Long userId, + @RequestBody ReviewRequest reviewRequest) { + Long createReview = reviewService.createReview(bookId, userId, reviewRequest); + return ResponseEntity.ok(createReview); + } } diff --git a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java index bb1be5a..37abe05 100644 --- a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java @@ -9,7 +9,7 @@ public class ReviewListResponse { private Long userId; private String name; - private double rating; + private Double rating; private String text; public static ReviewListResponse createReview(Review review) { diff --git a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java new file mode 100644 index 0000000..e6a7076 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java @@ -0,0 +1,11 @@ +package com.bookmile.backend.domain.review.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ReviewRequest { + private Double rating; + private String text; +} diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index 4b94281..3a4361a 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -1,6 +1,7 @@ package com.bookmile.backend.domain.review.entity; import com.bookmile.backend.domain.book.entity.Book; +import com.bookmile.backend.domain.review.dto.ReviewRequest; import com.bookmile.backend.domain.user.entity.User; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; @@ -50,4 +51,13 @@ public Review(User user, Book book, Double rating, String text) { this.rating = rating; this.text = text; } + + public static Review from(User user, Book book, ReviewRequest reviewRequest) { + return new Review( + user, + book, + reviewRequest.getRating(), + reviewRequest.getText() + ); + } } diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java index aa2ce22..ab114a8 100644 --- a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -1,7 +1,11 @@ package com.bookmile.backend.domain.review.service; +import com.bookmile.backend.domain.book.entity.Book; import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.dto.ReviewRequest; +import com.bookmile.backend.domain.review.entity.Review; import com.bookmile.backend.domain.review.repository.ReviewRepository; +import com.bookmile.backend.domain.user.entity.User; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -13,19 +17,28 @@ public class ReviewService { private final ReviewRepository reviewRepository; // private final BookRepository bookRepository; - public List viewReviewList(Long bookId) { - // 책 조회 및 예외처리 -// validateBook(bookId); - // 리뷰 리스트 조회 및 엔티티 -> DTO 변환 +// Book book = bookRepository.findById(bookId) +// .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); + return reviewRepository.findAllByBookId(bookId).stream() .map(ReviewListResponse::createReview) .collect(Collectors.toList()); } -// private void validateBook(Long bookId){ -// bookRepository.findById(bookId) -// .orElseThrow(() -> new IllegalArgumentException("없는 소원입니다.")); -// } + public Long createReview(Long bookId, Long userId, ReviewRequest reviewRequest) { + + Book book = bookRepository.findById(bookId) + .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); + + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("없는 사용자입니다.")); + + Review review = Review.from(user, book, reviewRequest); + + reviewRepository.save(review); + + return review.getId(); + } } \ No newline at end of file From 37dfa888e776d36e5033f425085656f822b3ff68 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 14:59:02 +0900 Subject: [PATCH 06/29] =?UTF-8?q?test(BookRepository,=20UserRepository,=20?= =?UTF-8?q?ReviewService,=20ReviewServiceTest):=20=EB=A6=AC=EB=B7=B0=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 추가적으로 테스트 하는데 있어서 어려움이 있어 임시로 BookRepository, UserRepository를 생성해주었습니다. - 생성 해줌에 따라 기존에 있던 리뷰 조회 기능 테스트 코드 로직 또한 바뀌었습니다. --- .../domain/review/service/BookRepository.java | 7 ++ .../domain/review/service/ReviewService.java | 7 +- .../domain/review/service/UserRepository.java | 7 ++ .../review/service/ReviewServiceTest.java | 68 ++++++++++++++----- 4 files changed, 68 insertions(+), 21 deletions(-) create mode 100644 src/main/java/com/bookmile/backend/domain/review/service/BookRepository.java create mode 100644 src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java diff --git a/src/main/java/com/bookmile/backend/domain/review/service/BookRepository.java b/src/main/java/com/bookmile/backend/domain/review/service/BookRepository.java new file mode 100644 index 0000000..a7417c8 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/service/BookRepository.java @@ -0,0 +1,7 @@ +package com.bookmile.backend.domain.review.service; + +import com.bookmile.backend.domain.book.entity.Book; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BookRepository extends JpaRepository { +} diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java index ab114a8..2183565 100644 --- a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -15,12 +15,13 @@ @RequiredArgsConstructor public class ReviewService { private final ReviewRepository reviewRepository; -// private final BookRepository bookRepository; + private final BookRepository bookRepository; + private final UserRepository userRepository; public List viewReviewList(Long bookId) { -// Book book = bookRepository.findById(bookId) -// .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); + Book book = bookRepository.findById(bookId) + .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); return reviewRepository.findAllByBookId(bookId).stream() .map(ReviewListResponse::createReview) diff --git a/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java b/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java new file mode 100644 index 0000000..dd267d8 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java @@ -0,0 +1,7 @@ +package com.bookmile.backend.domain.review.service; + +import com.bookmile.backend.domain.user.entity.User; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface UserRepository extends JpaRepository { +} diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java index 1eb8c8f..d71971e 100644 --- a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -1,46 +1,78 @@ package com.bookmile.backend.domain.review.service; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; import com.bookmile.backend.domain.book.entity.Book; import com.bookmile.backend.domain.review.dto.ReviewListResponse; +import com.bookmile.backend.domain.review.dto.ReviewRequest; import com.bookmile.backend.domain.review.entity.Review; import com.bookmile.backend.domain.review.repository.ReviewRepository; import com.bookmile.backend.domain.user.entity.User; +import jakarta.transaction.Transactional; import java.util.List; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; -@ExtendWith(MockitoExtension.class) +@SpringBootTest class ReviewServiceTest { - @Mock + @Autowired + private ReviewService reviewService; + + @Autowired private ReviewRepository reviewRepository; - @InjectMocks - private ReviewService reviewService; + @Autowired + private BookRepository bookRepository; + + @Autowired + private UserRepository userRepository; @Test + @Transactional void Book_Id로_리뷰_리스트_가져오기() { - //Given - Long bookId = 1L; + // Given User user1 = new User("김진용", "kje@naver.com", "1234", "urlurl"); - User user2 = new User("김진용", "kje@naver.com", "1234", "urlurl"); + User user2 = new User("진용짱", "jin@naver.com", "5678", "urlurl2"); Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + userRepository.save(user1); + userRepository.save(user2); + bookRepository.save(book); + Review review1 = new Review(user1, book, 4.5, "굳이에요 굳"); - Review review2 = new Review(user2, book, 4.5, "굳이에요 굳"); + Review review2 = new Review(user2, book, 4.0, "좋아요!"); - when(reviewRepository.findAllByBookId(bookId)).thenReturn(List.of(review1, review2)); + reviewRepository.save(review1); + reviewRepository.save(review2); - //When - List reviews = reviewService.viewReviewList(bookId); + // When + List reviews = reviewService.viewReviewList(book.getId()); - //Then + // Then assertEquals(2, reviews.size()); + assertEquals("굳이에요 굳", reviews.get(0).getText()); + assertEquals("좋아요!", reviews.get(1).getText()); + } + + @Test + void 리뷰_생성하기() { + // Given + User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + + userRepository.save(user); + bookRepository.save(book); + + ReviewRequest reviewRequest = new ReviewRequest(4.5, "굳굳"); + + // When + Long reviewId = reviewService.createReview(book.getId(), user.getId(), reviewRequest); + + // Then + Review review = reviewRepository.findById(reviewId).orElseThrow(); + assertEquals(4.5, review.getRating()); + assertEquals("굳굳", review.getText()); } -} \ No newline at end of file +} From d9863ed1b89c5cf0bdc56ee304b7362981b3cb86 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 18:58:55 +0900 Subject: [PATCH 07/29] =?UTF-8?q?feat(ReviewService,=20Review,=20ReviewSer?= =?UTF-8?q?viceTest):=20=EB=A6=AC=EB=B7=B0=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/review/entity/Review.java | 5 ++++ .../domain/review/service/ReviewService.java | 12 ++++++++-- .../review/service/ReviewServiceTest.java | 24 +++++++++++++++++++ 3 files changed, 39 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index 3a4361a..2337982 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -60,4 +60,9 @@ public static Review from(User user, Book book, ReviewRequest reviewRequest) { reviewRequest.getText() ); } + + public void update(ReviewRequest reviewRequest) { + this.rating = reviewRequest.getRating(); + this.text = reviewRequest.getText(); + } } diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java index 2183565..91b53f9 100644 --- a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -19,7 +19,6 @@ public class ReviewService { private final UserRepository userRepository; public List viewReviewList(Long bookId) { - Book book = bookRepository.findById(bookId) .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); @@ -29,7 +28,6 @@ public List viewReviewList(Long bookId) { } public Long createReview(Long bookId, Long userId, ReviewRequest reviewRequest) { - Book book = bookRepository.findById(bookId) .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); @@ -42,4 +40,14 @@ public Long createReview(Long bookId, Long userId, ReviewRequest reviewRequest) return review.getId(); } + + public Long updateReview(Long reviewId, ReviewRequest reviewRequest) { + Review target = reviewRepository.findById(reviewId) + .orElseThrow(() -> new IllegalArgumentException("없는 리뷰입니다.")); + + target.update(reviewRequest); + reviewRepository.save(target); + + return target.getId(); + } } \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java index d71971e..c8ca6b2 100644 --- a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -75,4 +75,28 @@ class ReviewServiceTest { assertEquals(4.5, review.getRating()); assertEquals("굳굳", review.getText()); } + + @Test + void 리뷰_수정하기() { + // Given + User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + + userRepository.save(user); + bookRepository.save(book); + + Review review = new Review(user, book, 4.5, "굳이에요 굳"); + + reviewRepository.save(review); + + ReviewRequest reviewRequest = new ReviewRequest(1.0, "다시 생각해보니 별로네요."); + + // When + Long reviewId = reviewService.updateReview(review.getId(), reviewRequest); + + // Then + Review updateReview = reviewRepository.findById(reviewId).orElseThrow(); + assertEquals(1.0, updateReview.getRating()); + assertEquals("다시 생각해보니 별로네요.", updateReview.getText()); + } } From 8a09ec72d762cf8646bf48e2c797a4eab19f3cb3 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 19:06:54 +0900 Subject: [PATCH 08/29] =?UTF-8?q?refactor(ReviewController):=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/review/controller/ReviewController.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java index 7f13b6e..377ab45 100644 --- a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -7,7 +7,9 @@ import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; @@ -31,4 +33,10 @@ public ResponseEntity createReview(@RequestParam Long bookId, @RequestPara Long createReview = reviewService.createReview(bookId, userId, reviewRequest); return ResponseEntity.ok(createReview); } + + @PutMapping + public ResponseEntity updateReview(@PathVariable Long reviewId, @RequestBody ReviewRequest reviewRequest) { + Long updateReview = reviewService.updateReview(reviewId, reviewRequest); + return ResponseEntity.ok(updateReview); + } } From 44f8e50a06f2e1adcf04fb0fc2e409ace6297a0a Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 19:11:39 +0900 Subject: [PATCH 09/29] =?UTF-8?q?refactor(ReviewController):=20=EB=A6=AC?= =?UTF-8?q?=EB=B7=B0=20=EC=88=98=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20Mapping=20=EA=B2=BD=EB=A1=9C=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/review/controller/ReviewController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java index 377ab45..ea87b72 100644 --- a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -34,7 +34,7 @@ public ResponseEntity createReview(@RequestParam Long bookId, @RequestPara return ResponseEntity.ok(createReview); } - @PutMapping + @PutMapping("/{reviewId}") public ResponseEntity updateReview(@PathVariable Long reviewId, @RequestBody ReviewRequest reviewRequest) { Long updateReview = reviewService.updateReview(reviewId, reviewRequest); return ResponseEntity.ok(updateReview); From 4f1e777d9388baf09172708e100b2a9ea81214f8 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 19:30:44 +0900 Subject: [PATCH 10/29] =?UTF-8?q?feat(ReviewService,=20Review,=20ReviewCon?= =?UTF-8?q?troller,=20ReviewServiceTest):=20=EB=A6=AC=EB=B7=B0=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B0=8F?= =?UTF-8?q?=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 약간의 변수명 변경을 곁들인.. --- .../review/controller/ReviewController.java | 7 +++++++ .../backend/domain/review/entity/Review.java | 4 ++++ .../domain/review/service/ReviewService.java | 19 +++++++++++++---- .../review/service/ReviewServiceTest.java | 21 +++++++++++++++++++ 4 files changed, 47 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java index ea87b72..94cba64 100644 --- a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -6,6 +6,7 @@ import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -39,4 +40,10 @@ public ResponseEntity updateReview(@PathVariable Long reviewId, @RequestBo Long updateReview = reviewService.updateReview(reviewId, reviewRequest); return ResponseEntity.ok(updateReview); } + + @DeleteMapping("/{reviewId}") + public ResponseEntity deleteReview(@PathVariable Long reviewId) { + Long deleteReview = reviewService.deleteReview(reviewId); + return ResponseEntity.ok(deleteReview); + } } diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index 2337982..822315d 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -65,4 +65,8 @@ public void update(ReviewRequest reviewRequest) { this.rating = reviewRequest.getRating(); this.text = reviewRequest.getText(); } + + public void delete(Review review) { + this.isDeleted = Boolean.TRUE; + } } diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java index 91b53f9..af45f8c 100644 --- a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -42,12 +42,23 @@ public Long createReview(Long bookId, Long userId, ReviewRequest reviewRequest) } public Long updateReview(Long reviewId, ReviewRequest reviewRequest) { - Review target = reviewRepository.findById(reviewId) + Review review = reviewRepository.findById(reviewId) .orElseThrow(() -> new IllegalArgumentException("없는 리뷰입니다.")); - target.update(reviewRequest); - reviewRepository.save(target); + review.update(reviewRequest); + reviewRepository.save(review); + + return review.getId(); + } + + public Long deleteReview(Long reviewId) { + Review review = reviewRepository.findById(reviewId) + .orElseThrow(() -> new IllegalArgumentException("없는 리뷰입니다.")); + + review.delete(review); - return target.getId(); + reviewRepository.save(review); + + return review.getId(); } } \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java index c8ca6b2..073e414 100644 --- a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -99,4 +99,25 @@ class ReviewServiceTest { assertEquals(1.0, updateReview.getRating()); assertEquals("다시 생각해보니 별로네요.", updateReview.getText()); } + + @Test + void 리뷰_삭제하기() { + // Given + User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + + userRepository.save(user); + bookRepository.save(book); + + Review review = new Review(user, book, 4.5, "굳이에요 굳"); + + reviewRepository.save(review); + + // When + Long reviewId = reviewService.deleteReview(review.getId()); + + // Then + Review deleteReview = reviewRepository.findById(reviewId).orElseThrow(); + assertEquals(Boolean.TRUE, deleteReview.getIsDeleted()); + } } From a17d9a3d780535370e5f544636a86792aec8dd36 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 29 Nov 2024 21:11:43 +0900 Subject: [PATCH 11/29] =?UTF-8?q?feat(RecordRepository):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EB=A0=88=ED=8F=AC=EC=A7=80=ED=86=A0=EB=A6=AC=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/record/repository/RecordRepository.java | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java diff --git a/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java new file mode 100644 index 0000000..db914d0 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java @@ -0,0 +1,10 @@ +package com.bookmile.backend.domain.record.repository; + +import com.bookmile.backend.domain.record.entity.Record; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface RecordRepository extends JpaRepository { + List findAllByRecordId(Long recordId); + +} From 3a1b46495d567ffc0f0d791b9a07b47c4ef0ddda Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Sat, 30 Nov 2024 00:25:17 +0900 Subject: [PATCH 12/29] =?UTF-8?q?feat(Record,=20RecordRepository,=20UserGr?= =?UTF-8?q?oup,=20GroupRepository,=20RecordListResponse,=20RecordService,?= =?UTF-8?q?=20RecordServiceTest,=20UserGroupRepository):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 구현 중 기존 엔티티 필드 추가가 필요해서 추가해주었고, 테스트를 위해 필요한 레포지토리를 생성했습니다. --- .../domain/record/dto/RecordListResponse.java | 28 ++++++++ .../backend/domain/record/entity/Record.java | 10 ++- .../record/repository/RecordRepository.java | 5 +- .../record/service/GroupRepository.java | 7 ++ .../domain/record/service/RecordService.java | 34 +++++++++ .../record/service/UserGroupRepository.java | 12 ++++ .../domain/userGroup/entity/UserGroup.java | 3 +- .../record/service/RecordServiceTest.java | 69 +++++++++++++++++++ 8 files changed, 163 insertions(+), 5 deletions(-) create mode 100644 src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java create mode 100644 src/main/java/com/bookmile/backend/domain/record/service/GroupRepository.java create mode 100644 src/main/java/com/bookmile/backend/domain/record/service/RecordService.java create mode 100644 src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java create mode 100644 src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java new file mode 100644 index 0000000..660c9a7 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java @@ -0,0 +1,28 @@ +package com.bookmile.backend.domain.record.dto; + +import com.bookmile.backend.domain.image.entity.Image; +import com.bookmile.backend.domain.record.entity.Record; +import java.time.LocalDateTime; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class RecordListResponse { + private Long recordId; + private String text; + private Integer current_page; + private LocalDateTime createdAt; + private List images; + + public static RecordListResponse createRecord(Record record) { + return new RecordListResponse( + record.getId(), + record.getText(), + record.getCurrentPage(), + record.getCreatedAt(), + record.getImage() + ); + } +} \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 940e3bd..06d5e26 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -1,13 +1,16 @@ package com.bookmile.backend.domain.record.entity; import com.bookmile.backend.domain.image.entity.Image; +import com.bookmile.backend.domain.userGroup.entity.UserGroup; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import jakarta.persistence.OneToMany; import java.util.ArrayList; import java.util.List; @@ -27,6 +30,10 @@ public class Record extends BaseEntity { @Column(name = "record_id") private Long id; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "usergroup_id", nullable = false) + private UserGroup userGroup; + @OneToMany @JoinColumn(name = "record_id") private List image = new ArrayList<>(); @@ -37,7 +44,8 @@ public class Record extends BaseEntity { @Column(nullable = false) private Integer currentPage; - public Record(String text, Integer currentPage) { + public Record(UserGroup userGroup, String text, Integer currentPage) { + this.userGroup = userGroup; this.text = text; this.currentPage = currentPage; } diff --git a/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java index db914d0..70c85c9 100644 --- a/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java +++ b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java @@ -3,8 +3,9 @@ import com.bookmile.backend.domain.record.entity.Record; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; public interface RecordRepository extends JpaRepository { - List findAllByRecordId(Long recordId); - + @Query(value = "SELECT * FROM record WHERE usergroup_id = :userGroupId", nativeQuery = true) + List findAllByUserGroupId(Long userGroupId); } diff --git a/src/main/java/com/bookmile/backend/domain/record/service/GroupRepository.java b/src/main/java/com/bookmile/backend/domain/record/service/GroupRepository.java new file mode 100644 index 0000000..c2e33af --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/service/GroupRepository.java @@ -0,0 +1,7 @@ +package com.bookmile.backend.domain.record.service; + +import com.bookmile.backend.domain.group.entity.Group; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface GroupRepository extends JpaRepository { +} \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java new file mode 100644 index 0000000..23f3ace --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -0,0 +1,34 @@ +package com.bookmile.backend.domain.record.service; + +import com.bookmile.backend.domain.group.entity.Group; +import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.repository.RecordRepository; +import com.bookmile.backend.domain.review.service.UserRepository; +import com.bookmile.backend.domain.user.entity.User; +import java.util.List; +import java.util.stream.Collectors; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class RecordService { + private final UserRepository userRepository; + private final GroupRepository groupRepository; + private final UserGroupRepository userGroupRepository; + private final RecordRepository recordRepository; + + public List viewRecordList(Long groupId, Long userId) { + Group group = groupRepository.findById(groupId) + .orElseThrow(() -> new IllegalArgumentException("없는 그룹입니다.")); + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("없는 사용자입니다.")); + + Long userGroupId = userGroupRepository.findUserGroupIdByGroupIdAndUserId(groupId, userId) + .orElseThrow(() -> new IllegalArgumentException("해당 그룹에 속하지 않은 사용자입니다.")); + + return recordRepository.findAllByUserGroupId(userGroupId).stream() + .map(RecordListResponse::createRecord) + .collect(Collectors.toList()); + } +} \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java b/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java new file mode 100644 index 0000000..d3fe3c8 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java @@ -0,0 +1,12 @@ +package com.bookmile.backend.domain.record.service; + +import com.bookmile.backend.domain.userGroup.entity.UserGroup; +import java.util.Optional; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; + +public interface UserGroupRepository extends JpaRepository { + + @Query(value = "SELECT usergroup_id FROM user_group WHERE group_id = :groupId AND user_id = :userId", nativeQuery = true) + Optional findUserGroupIdByGroupIdAndUserId(Long groupId, Long userId); +} \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java b/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java index 84d8707..f2f4dbd 100644 --- a/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java +++ b/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java @@ -42,8 +42,7 @@ public class UserGroup extends BaseEntity { @JoinColumn(name = "group_id", nullable = false) private Group group; - @OneToMany - @JoinColumn(name = "usergroup_id") + @OneToMany(mappedBy = "userGroup") private List record = new ArrayList<>(); @Column(nullable = false) diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java new file mode 100644 index 0000000..247a5ac --- /dev/null +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -0,0 +1,69 @@ +package com.bookmile.backend.domain.record.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.bookmile.backend.domain.book.entity.Book; +import com.bookmile.backend.domain.group.entity.Group; +import com.bookmile.backend.domain.group.entity.Role; +import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.entity.Record; +import com.bookmile.backend.domain.record.repository.RecordRepository; +import com.bookmile.backend.domain.review.service.BookRepository; +import com.bookmile.backend.domain.review.service.UserRepository; +import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.userGroup.entity.UserGroup; +import java.util.List; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +class RecordServiceTest { + + @Autowired + private BookRepository bookRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private GroupRepository groupRepository; + + @Autowired + private UserGroupRepository userGroupRepository; + + @Autowired + private RecordRepository recordRepository; + + @Autowired + private RecordService recordService; + + @Test + void 사용자의_그룹에_해당하는_기록_불러오기() { + //Given + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + bookRepository.save(book); + + User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + userRepository.save(user); + + Group group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); + groupRepository.save(group); + + UserGroup userGroup = new UserGroup(user, group, Role.MASTER); + userGroupRepository.save(userGroup); + + Record record1 = new Record(userGroup, "첫 번째 기록", 50); + Record record2 = new Record(userGroup, "두 번째 기록", 1000); + recordRepository.save(record1); + recordRepository.save(record2); + + //When + List records = recordService.viewRecordList(group.getId(), user.getId()); + + //Then + assertEquals(2, records.size()); + assertEquals("첫 번째 기록", records.get(0).getText()); + assertEquals("두 번째 기록", records.get(1).getText()); + } +} \ No newline at end of file From d245dea7c1a948138be3fa7208dc7b2de4862130 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Sat, 30 Nov 2024 00:30:47 +0900 Subject: [PATCH 13/29] =?UTF-8?q?feat(RecordController):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=EA=B8=B0=EB=8A=A5=20=EC=BB=A8?= =?UTF-8?q?=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EA=B3=84=EC=B8=B5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../record/controller/RecordController.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java diff --git a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java new file mode 100644 index 0000000..735b022 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java @@ -0,0 +1,25 @@ +package com.bookmile.backend.domain.record.controller; + +import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.service.RecordService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/records") +@RequiredArgsConstructor +public class RecordController { + private RecordService recordService; + + @GetMapping + public ResponseEntity> viewRecordList(@RequestParam Long groupId, + @RequestParam Long userId) { + List records = recordService.viewRecordList(groupId, userId); + return ResponseEntity.ok(records); + } +} From 43b22b6a0aeda73cb31e65ec9767dfd5e4b16765 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Sun, 1 Dec 2024 11:07:05 +0900 Subject: [PATCH 14/29] =?UTF-8?q?refacotr(RecordRepository):=20=ED=95=84?= =?UTF-8?q?=EC=9A=94=EC=97=86=EB=8A=94=20=EB=84=A4=EC=9D=B4=ED=8B=B0?= =?UTF-8?q?=EB=B8=8C=20=EC=BF=BC=EB=A6=AC=20=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/record/repository/RecordRepository.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java index 70c85c9..2a95805 100644 --- a/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java +++ b/src/main/java/com/bookmile/backend/domain/record/repository/RecordRepository.java @@ -3,9 +3,7 @@ import com.bookmile.backend.domain.record.entity.Record; import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Query; public interface RecordRepository extends JpaRepository { - @Query(value = "SELECT * FROM record WHERE usergroup_id = :userGroupId", nativeQuery = true) List findAllByUserGroupId(Long userGroupId); } From 39a964d7aa9b407baf9fd2390b16ceef824fd4a1 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Sun, 1 Dec 2024 19:49:50 +0900 Subject: [PATCH 15/29] =?UTF-8?q?feat(Record,=20RecordService,=20RecordSer?= =?UTF-8?q?viceTest,=20RequestRecord,=20UserGroupRepository):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=83=9D=EC=84=B1=20=EA=B8=B0=EB=8A=A5=20=EA=B5=AC?= =?UTF-8?q?=ED=98=84=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 이미지 부분은 이 후 고민 해보기로 하였습니다. --- .../domain/record/dto/RequestRecord.java | 11 ++++++++ .../backend/domain/record/entity/Record.java | 9 +++++++ .../domain/record/service/RecordService.java | 20 +++++++++++++++ .../record/service/UserGroupRepository.java | 2 ++ .../record/service/RecordServiceTest.java | 25 +++++++++++++++++++ 5 files changed, 67 insertions(+) create mode 100644 src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java new file mode 100644 index 0000000..dccaa57 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java @@ -0,0 +1,11 @@ +package com.bookmile.backend.domain.record.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class RequestRecord { + private String text; + private Integer currentPage; +} diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 06d5e26..21c22bb 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -1,6 +1,7 @@ package com.bookmile.backend.domain.record.entity; import com.bookmile.backend.domain.image.entity.Image; +import com.bookmile.backend.domain.record.dto.RequestRecord; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; @@ -49,4 +50,12 @@ public Record(UserGroup userGroup, String text, Integer currentPage) { this.text = text; this.currentPage = currentPage; } + + public static Record From(UserGroup userGroup, RequestRecord requestRecord) { + return new Record( + userGroup, + requestRecord.getText(), + requestRecord.getCurrentPage() + ); + } } diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index 23f3ace..3e1d86c 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -2,9 +2,12 @@ import com.bookmile.backend.domain.group.entity.Group; import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.dto.RequestRecord; +import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; import com.bookmile.backend.domain.review.service.UserRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.userGroup.entity.UserGroup; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -31,4 +34,21 @@ public List viewRecordList(Long groupId, Long userId) { .map(RecordListResponse::createRecord) .collect(Collectors.toList()); } + + public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) { + Group group = groupRepository.findById(groupId) + .orElseThrow(() -> new IllegalArgumentException("없는 그룹입니다.")); + User user = userRepository.findById(userId) + .orElseThrow(() -> new IllegalArgumentException("없는 사용자입니다.")); + + Long userGroupId = userGroupRepository.findUserGroupIdByGroupIdAndUserId(groupId, userId) + .orElseThrow(() -> new IllegalArgumentException("해당 그룹에 속하지 않은 사용자입니다.")); + + UserGroup userGroup = userGroupRepository.findUserGroupById(userGroupId); + + Record record = Record.From(userGroup, requestRecord); + recordRepository.save(record); + + return record.getId(); + } } \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java b/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java index d3fe3c8..8c71859 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/UserGroupRepository.java @@ -9,4 +9,6 @@ public interface UserGroupRepository extends JpaRepository { @Query(value = "SELECT usergroup_id FROM user_group WHERE group_id = :groupId AND user_id = :userId", nativeQuery = true) Optional findUserGroupIdByGroupIdAndUserId(Long groupId, Long userId); + + UserGroup findUserGroupById(Long userGroupId); } \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 247a5ac..5ec765f 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -6,6 +6,7 @@ import com.bookmile.backend.domain.group.entity.Group; import com.bookmile.backend.domain.group.entity.Role; import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.dto.RequestRecord; import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; import com.bookmile.backend.domain.review.service.BookRepository; @@ -66,4 +67,28 @@ class RecordServiceTest { assertEquals("첫 번째 기록", records.get(0).getText()); assertEquals("두 번째 기록", records.get(1).getText()); } + + @Test + void 사용자의_그룹에서의_기록_생성() { + //Given + Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + bookRepository.save(book); + + User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + userRepository.save(user); + + Group group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); + groupRepository.save(group); + + UserGroup userGroup = new UserGroup(user, group, Role.MASTER); + userGroupRepository.save(userGroup); + + //When + Long recordId1 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); +// Long recordId2 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록2", 389)); + Record record = recordRepository.findById(recordId1).orElseThrow(); + + assertEquals("나의 기록1", record.getText()); + assertEquals(193, record.getCurrentPage()); + } } \ No newline at end of file From 584ea1cce65fbc64f8edddb9f8d68cfbc11fff51 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 11:15:37 +0900 Subject: [PATCH 16/29] =?UTF-8?q?feat(RecordService,=20RecordServiceTest,?= =?UTF-8?q?=20Record):=20=EA=B8=B0=EB=A1=9D=20=EC=88=98=EC=A0=95=20?= =?UTF-8?q?=EA=B8=B0=EB=8A=A5=20=EA=B5=AC=ED=98=84=20=EB=B0=8F=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 테스트 코드가 너무 어지러워 리팩토링 했습니다 --- .../backend/domain/record/entity/Record.java | 5 ++ .../domain/record/service/RecordService.java | 11 ++++ .../record/service/RecordServiceTest.java | 53 +++++++++++-------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 21c22bb..4b1013f 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -58,4 +58,9 @@ public static Record From(UserGroup userGroup, RequestRecord requestRecord) { requestRecord.getCurrentPage() ); } + + public void update(RequestRecord requestRecord) { + this.text = requestRecord.getText(); + this.currentPage = requestRecord.getCurrentPage(); + } } diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index 3e1d86c..e07ce45 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -51,4 +51,15 @@ public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) return record.getId(); } + + public Long updateRecord(Long recordId, RequestRecord requestRecord) { + Record record = recordRepository.findById(recordId) + .orElseThrow(() -> new IllegalArgumentException("없는 기록입니다.")); + + record.update(requestRecord); + + recordRepository.save(record); + + return record.getId(); + } } \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 5ec765f..626f121 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -14,6 +14,7 @@ import com.bookmile.backend.domain.user.entity.User; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import java.util.List; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; @@ -39,21 +40,29 @@ class RecordServiceTest { @Autowired private RecordService recordService; - @Test - void 사용자의_그룹에_해당하는_기록_불러오기() { - //Given - Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + private Book book; + private User user; + private Group group; + private UserGroup userGroup; + + @BeforeEach + void setUp() { + book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); bookRepository.save(book); - User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + user = new User("김진용", "kje@naver.com", "1234", "urlurl"); userRepository.save(user); - Group group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); + group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); groupRepository.save(group); - UserGroup userGroup = new UserGroup(user, group, Role.MASTER); + userGroup = new UserGroup(user, group, Role.MASTER); userGroupRepository.save(userGroup); + } + @Test + void 사용자의_그룹에_해당하는_기록_불러오기() { + //Given Record record1 = new Record(userGroup, "첫 번째 기록", 50); Record record2 = new Record(userGroup, "두 번째 기록", 1000); recordRepository.save(record1); @@ -70,25 +79,27 @@ class RecordServiceTest { @Test void 사용자의_그룹에서의_기록_생성() { - //Given - Book book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); - bookRepository.save(book); - - User user = new User("김진용", "kje@naver.com", "1234", "urlurl"); - userRepository.save(user); - - Group group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); - groupRepository.save(group); - - UserGroup userGroup = new UserGroup(user, group, Role.MASTER); - userGroupRepository.save(userGroup); - //When Long recordId1 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); -// Long recordId2 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록2", 389)); Record record = recordRepository.findById(recordId1).orElseThrow(); + //Then assertEquals("나의 기록1", record.getText()); assertEquals(193, record.getCurrentPage()); } + + @Test + void 기록_수정() { + //When + // 일단 생성 + Long recordId = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); + Record record1 = recordRepository.findById(recordId).orElseThrow(); + // 그리고 수정 + Long updateRecord = recordService.updateRecord(recordId, new RequestRecord("나의 수정한 기록1", 193)); + Record record2 = recordRepository.findById(updateRecord).orElseThrow(); + //Then + assertEquals(record1.getId(), record2.getId()); + assertEquals("나의 기록1", record1.getText()); // 수정 전 + assertEquals("나의 수정한 기록1", record2.getText()); // 수정 후 + } } \ No newline at end of file From 57029f4777aebdcad35e438dec292bc8bbd3ccb1 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 11:26:06 +0900 Subject: [PATCH 17/29] =?UTF-8?q?feat(RecordController):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EC=83=9D=EC=84=B1&=EC=88=98=EC=A0=95=20=EA=B8=B0?= =?UTF-8?q?=EB=8A=A5=20=EC=BB=A8=ED=8A=B8=EB=A1=A4=EB=9F=AC=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../record/controller/RecordController.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java index 735b022..197b983 100644 --- a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java +++ b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java @@ -1,11 +1,16 @@ package com.bookmile.backend.domain.record.controller; import com.bookmile.backend.domain.record.dto.RecordListResponse; +import com.bookmile.backend.domain.record.dto.RequestRecord; import com.bookmile.backend.domain.record.service.RecordService; import java.util.List; import lombok.RequiredArgsConstructor; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @@ -22,4 +27,17 @@ public ResponseEntity> viewRecordList(@RequestParam Lon List records = recordService.viewRecordList(groupId, userId); return ResponseEntity.ok(records); } + + @PostMapping + public ResponseEntity createRecord(@RequestParam Long groupId, + @RequestParam Long userId, @RequestBody RequestRecord requestRecord) { + Long recordId = recordService.createRecord(groupId, userId, requestRecord); + return ResponseEntity.ok(recordId); + } + + @PutMapping("/{recordId}") + public ResponseEntity updateRecord(@PathVariable Long recordId, @RequestBody RequestRecord requestRecord) { + Long updateRecord = recordService.updateRecord(recordId, requestRecord); + return ResponseEntity.ok(updateRecord); + } } From 55cbe3f7d2eb45a75effaafd4decc1eac31ce4b6 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 12:55:11 +0900 Subject: [PATCH 18/29] =?UTF-8?q?test(RecordServiceTest):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0=20=ED=85=8C?= =?UTF-8?q?=EC=8A=A4=ED=8A=B8=20=EB=A1=9C=EC=A7=81=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기록 생성 기능 구현에 따라 테스트 코드 로직 변경 --- .../record/service/RecordServiceTest.java | 35 +++++++++++-------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 626f121..57f7bbd 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -60,13 +60,29 @@ void setUp() { userGroupRepository.save(userGroup); } + @Test + void 사용자의_그룹에서의_기록_생성() { + //When + Long recordId1 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); + Record record = recordRepository.findById(recordId1).orElseThrow(); + + //Then + assertEquals("나의 기록1", record.getText()); + assertEquals(193, record.getCurrentPage()); + } + @Test void 사용자의_그룹에_해당하는_기록_불러오기() { //Given - Record record1 = new Record(userGroup, "첫 번째 기록", 50); - Record record2 = new Record(userGroup, "두 번째 기록", 1000); - recordRepository.save(record1); - recordRepository.save(record2); + recordService.createRecord( + group.getId(), + user.getId(), + new RequestRecord("첫 번째 기록", 193)); + + recordService.createRecord( + group.getId(), + user.getId(), + new RequestRecord("두 번째 기록", 193)); //When List records = recordService.viewRecordList(group.getId(), user.getId()); @@ -77,17 +93,6 @@ void setUp() { assertEquals("두 번째 기록", records.get(1).getText()); } - @Test - void 사용자의_그룹에서의_기록_생성() { - //When - Long recordId1 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); - Record record = recordRepository.findById(recordId1).orElseThrow(); - - //Then - assertEquals("나의 기록1", record.getText()); - assertEquals(193, record.getCurrentPage()); - } - @Test void 기록_수정() { //When From 852d5777d2ba80b7aade6c7b1c18eaa2a787bf4b Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 13:59:15 +0900 Subject: [PATCH 19/29] =?UTF-8?q?feat(Record,=20RecordListResponse,=20Reco?= =?UTF-8?q?rdServiceTest,=20RequestRecord):=20=EA=B8=B0=EB=A1=9D=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1=20&=20=EC=A1=B0=ED=9A=8C=20=EC=8B=9C,=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EB=B6=80=EB=B6=84=20=EB=A7=88?= =?UTF-8?q?=EC=A0=80=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/record/dto/RecordListResponse.java | 7 ++++-- .../domain/record/dto/RequestRecord.java | 2 ++ .../backend/domain/record/entity/Record.java | 14 +++++++++--- .../record/service/RecordServiceTest.java | 22 +++++++++++++------ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java index 660c9a7..9025899 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java @@ -4,6 +4,7 @@ import com.bookmile.backend.domain.record.entity.Record; import java.time.LocalDateTime; import java.util.List; +import java.util.stream.Collectors; import lombok.AllArgsConstructor; import lombok.Getter; @@ -14,7 +15,7 @@ public class RecordListResponse { private String text; private Integer current_page; private LocalDateTime createdAt; - private List images; + private List images; public static RecordListResponse createRecord(Record record) { return new RecordListResponse( @@ -22,7 +23,9 @@ public static RecordListResponse createRecord(Record record) { record.getText(), record.getCurrentPage(), record.getCreatedAt(), - record.getImage() + record.getImages().stream() + .map(Image::getImageUrl) + .collect(Collectors.toList()) ); } } \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java index dccaa57..78105ff 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java @@ -1,5 +1,6 @@ package com.bookmile.backend.domain.record.dto; +import java.util.List; import lombok.AllArgsConstructor; import lombok.Getter; @@ -8,4 +9,5 @@ public class RequestRecord { private String text; private Integer currentPage; + private List imageUrls; } diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 4b1013f..86592e6 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -37,7 +37,7 @@ public class Record extends BaseEntity { @OneToMany @JoinColumn(name = "record_id") - private List image = new ArrayList<>(); + private List images = new ArrayList<>(); @Column private String text; @@ -45,17 +45,25 @@ public class Record extends BaseEntity { @Column(nullable = false) private Integer currentPage; - public Record(UserGroup userGroup, String text, Integer currentPage) { + public Record(UserGroup userGroup, String text, Integer currentPage, List images) { this.userGroup = userGroup; this.text = text; this.currentPage = currentPage; + this.images = images; } public static Record From(UserGroup userGroup, RequestRecord requestRecord) { + List imageList = new ArrayList<>(); + for (String imageUrl : requestRecord.getImageUrls()) { + Image image = new Image(imageUrl); + imageList.add(image); + } + return new Record( userGroup, requestRecord.getText(), - requestRecord.getCurrentPage() + requestRecord.getCurrentPage(), + imageList ); } diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 57f7bbd..dd48c6d 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -13,6 +13,7 @@ import com.bookmile.backend.domain.review.service.UserRepository; import com.bookmile.backend.domain.user.entity.User; import com.bookmile.backend.domain.userGroup.entity.UserGroup; +import jakarta.transaction.Transactional; import java.util.List; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -63,7 +64,8 @@ void setUp() { @Test void 사용자의_그룹에서의_기록_생성() { //When - Long recordId1 = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); + Long recordId1 = recordService.createRecord(group.getId(), user.getId(), + new RequestRecord("나의 기록1", 193, List.of("url1", "url2", "url3"))); Record record = recordRepository.findById(recordId1).orElseThrow(); //Then @@ -71,36 +73,42 @@ void setUp() { assertEquals(193, record.getCurrentPage()); } + @Transactional @Test void 사용자의_그룹에_해당하는_기록_불러오기() { //Given recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("첫 번째 기록", 193)); + new RequestRecord("기록1", 193, List.of())); recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("두 번째 기록", 193)); + new RequestRecord("기록2", 234, List.of("url1", "url2", "url3"))); //When List records = recordService.viewRecordList(group.getId(), user.getId()); //Then assertEquals(2, records.size()); - assertEquals("첫 번째 기록", records.get(0).getText()); - assertEquals("두 번째 기록", records.get(1).getText()); + assertEquals("기록1", records.get(0).getText()); + assertEquals("기록2", records.get(1).getText()); + assertEquals(193, records.get(0).getCurrent_page()); + assertEquals(234, records.get(1).getCurrent_page()); + assertEquals(0, records.get(0).getImages().size()); + assertEquals(3, records.get(1).getImages().size()); } @Test void 기록_수정() { //When // 일단 생성 - Long recordId = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193)); + Long recordId = recordService.createRecord(group.getId(), user.getId(), + new RequestRecord("나의 기록1", 193, List.of())); Record record1 = recordRepository.findById(recordId).orElseThrow(); // 그리고 수정 - Long updateRecord = recordService.updateRecord(recordId, new RequestRecord("나의 수정한 기록1", 193)); + Long updateRecord = recordService.updateRecord(recordId, new RequestRecord("나의 수정한 기록1", 193, List.of())); Record record2 = recordRepository.findById(updateRecord).orElseThrow(); //Then assertEquals(record1.getId(), record2.getId()); From 54cbc7c703430aef060070fd92b0fd5d4db0afd2 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 14:15:11 +0900 Subject: [PATCH 20/29] =?UTF-8?q?feat(Record,=20RecordServiceTest):=20?= =?UTF-8?q?=EA=B8=B0=EB=A1=9D=20=EC=88=98=EC=A0=95=20=EB=B6=80=EB=B6=84=20?= =?UTF-8?q?=EC=9D=B4=EB=AF=B8=EC=A7=80=20=EC=88=98=EC=A0=95=20=EB=A1=9C?= =?UTF-8?q?=EC=A7=81=20=EC=B6=94=EA=B0=80=20=EB=B0=8F=20=ED=85=8C=EC=8A=A4?= =?UTF-8?q?=ED=8A=B8=20=EC=BD=94=EB=93=9C=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/record/entity/Record.java | 6 ++++++ .../domain/record/service/RecordServiceTest.java | 15 ++++++++++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 86592e6..81453a3 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -68,7 +68,13 @@ public static Record From(UserGroup userGroup, RequestRecord requestRecord) { } public void update(RequestRecord requestRecord) { + List imageList = new ArrayList<>(); + for (String imageUrl : requestRecord.getImageUrls()) { + Image image = new Image(imageUrl); + imageList.add(image); + } this.text = requestRecord.getText(); this.currentPage = requestRecord.getCurrentPage(); + this.images = imageList; } } diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index dd48c6d..3f28de0 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -100,6 +100,7 @@ void setUp() { assertEquals(3, records.get(1).getImages().size()); } + @Transactional @Test void 기록_수정() { //When @@ -107,12 +108,20 @@ void setUp() { Long recordId = recordService.createRecord(group.getId(), user.getId(), new RequestRecord("나의 기록1", 193, List.of())); Record record1 = recordRepository.findById(recordId).orElseThrow(); + + // 수정 전 + assertEquals("나의 기록1", record1.getText()); + assertEquals(0, record1.getImages().size()); + // 그리고 수정 - Long updateRecord = recordService.updateRecord(recordId, new RequestRecord("나의 수정한 기록1", 193, List.of())); + Long updateRecord = recordService.updateRecord(recordId, + new RequestRecord("나의 수정한 기록1", 193, List.of("url1", "url2", "url3"))); Record record2 = recordRepository.findById(updateRecord).orElseThrow(); + //Then assertEquals(record1.getId(), record2.getId()); - assertEquals("나의 기록1", record1.getText()); // 수정 전 - assertEquals("나의 수정한 기록1", record2.getText()); // 수정 후 + // 수정 후 + assertEquals("나의 수정한 기록1", record2.getText()); + assertEquals(3, record2.getImages().size()); } } \ No newline at end of file From 16597501e91e00f315696ae593d0c8fdeabd6ad8 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Mon, 2 Dec 2024 15:28:12 +0900 Subject: [PATCH 21/29] =?UTF-8?q?refactor(RecordService):=20=EA=B8=B0?= =?UTF-8?q?=EB=A1=9D=20=EB=B6=88=EB=9F=AC=EC=98=A4=EA=B8=B0,=20=EC=88=98?= =?UTF-8?q?=EC=A0=95=20=EA=B8=B0=EB=8A=A5=20=EB=A6=AC=ED=8C=A9=ED=86=A0?= =?UTF-8?q?=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기록 리스트 반환 시, 코드 간결하게 수정하였습니다 - 기록 수정 시, 필요 없는 save 호출 제거를 해주었습니다. 이유) JPA 엔티티 상태 변경을 할 경우, 트랜잭션 종료시 dirty checking을 통해 자동으로 변경 사항이 저장되기 때문 --- .../backend/domain/record/service/RecordService.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index e07ce45..4120e20 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -9,7 +9,6 @@ import com.bookmile.backend.domain.user.entity.User; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import java.util.List; -import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -32,7 +31,7 @@ public List viewRecordList(Long groupId, Long userId) { return recordRepository.findAllByUserGroupId(userGroupId).stream() .map(RecordListResponse::createRecord) - .collect(Collectors.toList()); + .toList(); } public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) { @@ -58,8 +57,6 @@ public Long updateRecord(Long recordId, RequestRecord requestRecord) { record.update(requestRecord); - recordRepository.save(record); - return record.getId(); } } \ No newline at end of file From 47bc3dff64ebb9843a3ba3a94fcfaefd1d82cdb6 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Thu, 5 Dec 2024 17:44:26 +0900 Subject: [PATCH 22/29] =?UTF-8?q?refactor(Record,=20RecordService):=20?= =?UTF-8?q?=EB=A9=94=EC=84=9C=EB=93=9C=20=EB=AA=85=20=EC=86=8C=EB=AC=B8?= =?UTF-8?q?=EC=9E=90=EB=A1=9C=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 자연스럽게 대문자로 적고 넘어갔었던... ~ --- .../java/com/bookmile/backend/domain/record/entity/Record.java | 2 +- .../bookmile/backend/domain/record/service/RecordService.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 81453a3..8845e45 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -52,7 +52,7 @@ public Record(UserGroup userGroup, String text, Integer currentPage, List this.images = images; } - public static Record From(UserGroup userGroup, RequestRecord requestRecord) { + public static Record from(UserGroup userGroup, RequestRecord requestRecord) { List imageList = new ArrayList<>(); for (String imageUrl : requestRecord.getImageUrls()) { Image image = new Image(imageUrl); diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index 4120e20..5494b9d 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -45,7 +45,7 @@ public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) UserGroup userGroup = userGroupRepository.findUserGroupById(userGroupId); - Record record = Record.From(userGroup, requestRecord); + Record record = Record.from(userGroup, requestRecord); recordRepository.save(record); return record.getId(); From 27d3badf6d0985c546be27ec96bdade1202124df Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Thu, 5 Dec 2024 22:15:46 +0900 Subject: [PATCH 23/29] =?UTF-8?q?refactor(Image,=20Record,=20RecordControl?= =?UTF-8?q?ler,=20RecordListResponse,=20RecordService,=20RequestRecord,=20?= =?UTF-8?q?ImageController,=20ImageListResponse,=20ImageRepository,=20Imag?= =?UTF-8?q?eSaveRequest,=20ImageService,=20RequestUpdateRecord,=20RecordSe?= =?UTF-8?q?rviceTest):=20Record-Image=20=EA=B0=84=20=EB=A1=9C=EC=A7=81=20?= =?UTF-8?q?=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 연관 관계 다시 명시 해주었습니다. - 이에 따라 Image 자체에서 조회, 생성, 삭제 기능 구현 하였습니다. - 이 후 테스트 코드 작성과 일부 코드 수정이 있을 예정입니다. --- .../image/controller/ImageController.java | 45 ++++++++++++++++ .../image/dto/req/ImageSaveRequest.java | 11 ++++ .../image/dto/res/ImageListResponse.java | 17 +++++++ .../backend/domain/image/entity/Image.java | 18 ++++++- .../image/repository/ImageRepository.java | 11 ++++ .../domain/image/service/ImageService.java | 51 +++++++++++++++++++ .../record/controller/RecordController.java | 6 ++- .../domain/record/dto/RecordListResponse.java | 9 +--- .../domain/record/dto/RequestRecord.java | 2 - .../record/dto/RequestUpdateRecord.java | 12 +++++ .../backend/domain/record/entity/Record.java | 32 +++--------- .../domain/record/service/RecordService.java | 5 +- .../record/service/RecordServiceTest.java | 15 +++--- 13 files changed, 184 insertions(+), 50 deletions(-) create mode 100644 src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java create mode 100644 src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java create mode 100644 src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java create mode 100644 src/main/java/com/bookmile/backend/domain/image/repository/ImageRepository.java create mode 100644 src/main/java/com/bookmile/backend/domain/image/service/ImageService.java create mode 100644 src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java diff --git a/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java b/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java new file mode 100644 index 0000000..aefc755 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java @@ -0,0 +1,45 @@ +package com.bookmile.backend.domain.image.controller; + +import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; +import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.service.ImageService; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1/images") +@RequiredArgsConstructor +public class ImageController { + + private ImageService imageService; + + @GetMapping("{recordId}") + public ResponseEntity> viewImages(@PathVariable Long recordId) { + List images = imageService.viewImages(recordId); + + return ResponseEntity.ok(images); + } + + @PostMapping + public ResponseEntity saveImages(@RequestParam Long recordId, @RequestBody ImageSaveRequest imageSaveRequest) { + imageService.saveImages(recordId, imageSaveRequest); + + return ResponseEntity.ok("생성 완료!"); + } + + @DeleteMapping("{imageId}") + public ResponseEntity deleteImage(@PathVariable Long imageId) { + imageService.deleteImage(imageId); + + return ResponseEntity.ok("삭제 완료!"); + } +} diff --git a/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java b/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java new file mode 100644 index 0000000..46bdbd8 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java @@ -0,0 +1,11 @@ +package com.bookmile.backend.domain.image.dto.req; + +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ImageSaveRequest { + private List imageUrls; +} diff --git a/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java b/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java new file mode 100644 index 0000000..515d8cb --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java @@ -0,0 +1,17 @@ +package com.bookmile.backend.domain.image.dto.res; + +import com.bookmile.backend.domain.image.entity.Image; +import lombok.AllArgsConstructor; +import lombok.Getter; + +@Getter +@AllArgsConstructor +public class ImageListResponse { + private String imageUrls; + + public static ImageListResponse createImage(Image image) { + return new ImageListResponse( + image.getImageUrl() + ); + } +} diff --git a/src/main/java/com/bookmile/backend/domain/image/entity/Image.java b/src/main/java/com/bookmile/backend/domain/image/entity/Image.java index 36b416f..77cb08a 100644 --- a/src/main/java/com/bookmile/backend/domain/image/entity/Image.java +++ b/src/main/java/com/bookmile/backend/domain/image/entity/Image.java @@ -1,11 +1,15 @@ package com.bookmile.backend.domain.image.entity; +import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; import jakarta.persistence.GeneratedValue; import jakarta.persistence.GenerationType; import jakarta.persistence.Id; +import jakarta.persistence.JoinColumn; +import jakarta.persistence.ManyToOne; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; @@ -22,10 +26,22 @@ public class Image extends BaseEntity { @Column(name = "image_id") private Long id; + @ManyToOne(fetch = FetchType.LAZY) + @JoinColumn(name = "record_id", nullable = false) + private Record record; + @Column(nullable = false) private String imageUrl; - public Image(String imageUrl) { + @Column(nullable = false) + private Boolean isDeleted = Boolean.FALSE; + + public Image(Record record, String imageUrl) { + this.record = record; this.imageUrl = imageUrl; } + + public void delete(Image image) { + this.isDeleted = Boolean.TRUE; + } } diff --git a/src/main/java/com/bookmile/backend/domain/image/repository/ImageRepository.java b/src/main/java/com/bookmile/backend/domain/image/repository/ImageRepository.java new file mode 100644 index 0000000..9e6382d --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/image/repository/ImageRepository.java @@ -0,0 +1,11 @@ +package com.bookmile.backend.domain.image.repository; + +import com.bookmile.backend.domain.image.entity.Image; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +@Repository +public interface ImageRepository extends JpaRepository { + List findAllByRecordId(Long recordId); +} diff --git a/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java b/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java new file mode 100644 index 0000000..871c1b2 --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java @@ -0,0 +1,51 @@ +package com.bookmile.backend.domain.image.service; + +import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; +import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.entity.Image; +import com.bookmile.backend.domain.image.repository.ImageRepository; +import com.bookmile.backend.domain.record.entity.Record; +import com.bookmile.backend.domain.record.repository.RecordRepository; +import java.util.List; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class ImageService { + + private final ImageRepository imageRepository; + private final RecordRepository recordRepository; + + public List viewImages(Long recordId) { + recordRepository.findById(recordId) + .orElseThrow(() -> new IllegalArgumentException("없는 recordId 입니다.")); + + return imageRepository.findAllByRecordId(recordId) + .stream() + .map(ImageListResponse::createImage) + .toList(); + } + + public void saveImages(Long recordId, ImageSaveRequest imageSaveRequest) { + Record record = recordRepository.findById(recordId) + .orElseThrow(() -> new IllegalArgumentException("없는 기록 ID 입니다.")); + + List images = imageSaveRequest.getImageUrls().stream() + .map(url -> new Image(record, url)) + .toList(); + + imageRepository.saveAll(images); + } + + public Long deleteImage(Long imageId) { + Image image = imageRepository.findById(imageId) + .orElseThrow(() -> new IllegalArgumentException("없는 이미지 입니다.")); + + image.delete(image); + + imageRepository.save(image); + + return image.getId(); + } +} diff --git a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java index 197b983..d704440 100644 --- a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java +++ b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java @@ -2,6 +2,7 @@ import com.bookmile.backend.domain.record.dto.RecordListResponse; import com.bookmile.backend.domain.record.dto.RequestRecord; +import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; import com.bookmile.backend.domain.record.service.RecordService; import java.util.List; import lombok.RequiredArgsConstructor; @@ -36,8 +37,9 @@ public ResponseEntity createRecord(@RequestParam Long groupId, } @PutMapping("/{recordId}") - public ResponseEntity updateRecord(@PathVariable Long recordId, @RequestBody RequestRecord requestRecord) { - Long updateRecord = recordService.updateRecord(recordId, requestRecord); + public ResponseEntity updateRecord(@PathVariable Long recordId, + @RequestBody RequestUpdateRecord requestUpdateRecord) { + Long updateRecord = recordService.updateRecord(recordId, requestUpdateRecord); return ResponseEntity.ok(updateRecord); } } diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java index 9025899..0fcc818 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java @@ -1,10 +1,7 @@ package com.bookmile.backend.domain.record.dto; -import com.bookmile.backend.domain.image.entity.Image; import com.bookmile.backend.domain.record.entity.Record; import java.time.LocalDateTime; -import java.util.List; -import java.util.stream.Collectors; import lombok.AllArgsConstructor; import lombok.Getter; @@ -15,17 +12,13 @@ public class RecordListResponse { private String text; private Integer current_page; private LocalDateTime createdAt; - private List images; public static RecordListResponse createRecord(Record record) { return new RecordListResponse( record.getId(), record.getText(), record.getCurrentPage(), - record.getCreatedAt(), - record.getImages().stream() - .map(Image::getImageUrl) - .collect(Collectors.toList()) + record.getCreatedAt() ); } } \ No newline at end of file diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java index 78105ff..dccaa57 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java @@ -1,6 +1,5 @@ package com.bookmile.backend.domain.record.dto; -import java.util.List; import lombok.AllArgsConstructor; import lombok.Getter; @@ -9,5 +8,4 @@ public class RequestRecord { private String text; private Integer currentPage; - private List imageUrls; } diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java new file mode 100644 index 0000000..284992f --- /dev/null +++ b/src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java @@ -0,0 +1,12 @@ +package com.bookmile.backend.domain.record.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; + + +@Getter +@AllArgsConstructor +public class RequestUpdateRecord { + private String text; + private Integer currentPage; +} diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index 8845e45..b6e2ab3 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.record.entity; -import com.bookmile.backend.domain.image.entity.Image; import com.bookmile.backend.domain.record.dto.RequestRecord; +import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; @@ -12,9 +12,6 @@ import jakarta.persistence.Id; import jakarta.persistence.JoinColumn; import jakarta.persistence.ManyToOne; -import jakarta.persistence.OneToMany; -import java.util.ArrayList; -import java.util.List; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Getter; @@ -35,46 +32,29 @@ public class Record extends BaseEntity { @JoinColumn(name = "usergroup_id", nullable = false) private UserGroup userGroup; - @OneToMany - @JoinColumn(name = "record_id") - private List images = new ArrayList<>(); - @Column private String text; @Column(nullable = false) private Integer currentPage; - public Record(UserGroup userGroup, String text, Integer currentPage, List images) { + public Record(UserGroup userGroup, String text, Integer currentPage) { this.userGroup = userGroup; this.text = text; this.currentPage = currentPage; - this.images = images; } public static Record from(UserGroup userGroup, RequestRecord requestRecord) { - List imageList = new ArrayList<>(); - for (String imageUrl : requestRecord.getImageUrls()) { - Image image = new Image(imageUrl); - imageList.add(image); - } return new Record( userGroup, requestRecord.getText(), - requestRecord.getCurrentPage(), - imageList + requestRecord.getCurrentPage() ); } - public void update(RequestRecord requestRecord) { - List imageList = new ArrayList<>(); - for (String imageUrl : requestRecord.getImageUrls()) { - Image image = new Image(imageUrl); - imageList.add(image); - } - this.text = requestRecord.getText(); - this.currentPage = requestRecord.getCurrentPage(); - this.images = imageList; + public void update(RequestUpdateRecord requestUpdateRecord) { + this.text = requestUpdateRecord.getText(); + this.currentPage = requestUpdateRecord.getCurrentPage(); } } diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index 5494b9d..b18a280 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -3,6 +3,7 @@ import com.bookmile.backend.domain.group.entity.Group; import com.bookmile.backend.domain.record.dto.RecordListResponse; import com.bookmile.backend.domain.record.dto.RequestRecord; +import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; import com.bookmile.backend.domain.review.service.UserRepository; @@ -51,11 +52,11 @@ public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) return record.getId(); } - public Long updateRecord(Long recordId, RequestRecord requestRecord) { + public Long updateRecord(Long recordId, RequestUpdateRecord requestUpdateRecord) { Record record = recordRepository.findById(recordId) .orElseThrow(() -> new IllegalArgumentException("없는 기록입니다.")); - record.update(requestRecord); + record.update(requestUpdateRecord); return record.getId(); } diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 3f28de0..7bfe24e 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -7,6 +7,7 @@ import com.bookmile.backend.domain.group.entity.Role; import com.bookmile.backend.domain.record.dto.RecordListResponse; import com.bookmile.backend.domain.record.dto.RequestRecord; +import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; import com.bookmile.backend.domain.review.service.BookRepository; @@ -65,7 +66,7 @@ void setUp() { void 사용자의_그룹에서의_기록_생성() { //When Long recordId1 = recordService.createRecord(group.getId(), user.getId(), - new RequestRecord("나의 기록1", 193, List.of("url1", "url2", "url3"))); + new RequestRecord("나의 기록1", 193)); Record record = recordRepository.findById(recordId1).orElseThrow(); //Then @@ -80,12 +81,12 @@ void setUp() { recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("기록1", 193, List.of())); + new RequestRecord("기록1", 193)); recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("기록2", 234, List.of("url1", "url2", "url3"))); + new RequestRecord("기록2", 234)); //When List records = recordService.viewRecordList(group.getId(), user.getId()); @@ -96,8 +97,6 @@ void setUp() { assertEquals("기록2", records.get(1).getText()); assertEquals(193, records.get(0).getCurrent_page()); assertEquals(234, records.get(1).getCurrent_page()); - assertEquals(0, records.get(0).getImages().size()); - assertEquals(3, records.get(1).getImages().size()); } @Transactional @@ -106,22 +105,20 @@ void setUp() { //When // 일단 생성 Long recordId = recordService.createRecord(group.getId(), user.getId(), - new RequestRecord("나의 기록1", 193, List.of())); + new RequestRecord("나의 기록1", 193)); Record record1 = recordRepository.findById(recordId).orElseThrow(); // 수정 전 assertEquals("나의 기록1", record1.getText()); - assertEquals(0, record1.getImages().size()); // 그리고 수정 Long updateRecord = recordService.updateRecord(recordId, - new RequestRecord("나의 수정한 기록1", 193, List.of("url1", "url2", "url3"))); + new RequestUpdateRecord("나의 수정한 기록1", 193)); Record record2 = recordRepository.findById(updateRecord).orElseThrow(); //Then assertEquals(record1.getId(), record2.getId()); // 수정 후 assertEquals("나의 수정한 기록1", record2.getText()); - assertEquals(3, record2.getImages().size()); } } \ No newline at end of file From 45bd47d48f006f18be13e8ba2789e29fa8f9343b Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 14:23:51 +0900 Subject: [PATCH 24/29] =?UTF-8?q?test(ImageServiceTest,=20RecordServiceTes?= =?UTF-8?q?t,=20ReviewServiceTest):=20=EC=9D=B4=EB=AF=B8=EC=A7=80=20?= =?UTF-8?q?=EC=83=9D=EC=84=B1,=20=EC=A1=B0=ED=9A=8C,=20=EC=82=AD=EC=A0=9C?= =?UTF-8?q?=20=EA=B8=B0=EB=8A=A5=20=ED=85=8C=EC=8A=A4=ED=8A=B8=20=EC=BD=94?= =?UTF-8?q?=EB=93=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - ImageServiceTest 파일을 포함해 다른 테스트 코드 또한 트랜잭션 어노테이션 적용을 해주었습니다. --- .../image/service/ImageServiceTest.java | 121 ++++++++++++++++++ .../record/service/RecordServiceTest.java | 1 + .../review/service/ReviewServiceTest.java | 1 + 3 files changed, 123 insertions(+) create mode 100644 src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java diff --git a/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java b/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java new file mode 100644 index 0000000..2bccc24 --- /dev/null +++ b/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java @@ -0,0 +1,121 @@ +package com.bookmile.backend.domain.image.service; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import com.bookmile.backend.domain.book.entity.Book; +import com.bookmile.backend.domain.group.entity.Group; +import com.bookmile.backend.domain.group.entity.Role; +import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; +import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.entity.Image; +import com.bookmile.backend.domain.image.repository.ImageRepository; +import com.bookmile.backend.domain.record.entity.Record; +import com.bookmile.backend.domain.record.repository.RecordRepository; +import com.bookmile.backend.domain.record.service.GroupRepository; +import com.bookmile.backend.domain.record.service.RecordService; +import com.bookmile.backend.domain.record.service.UserGroupRepository; +import com.bookmile.backend.domain.review.service.BookRepository; +import com.bookmile.backend.domain.review.service.UserRepository; +import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.userGroup.entity.UserGroup; +import jakarta.transaction.Transactional; +import java.util.List; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +@Transactional +class ImageServiceTest { + @Autowired + private BookRepository bookRepository; + + @Autowired + private UserRepository userRepository; + + @Autowired + private GroupRepository groupRepository; + + @Autowired + private UserGroupRepository userGroupRepository; + + @Autowired + private RecordRepository recordRepository; + + @Autowired + private RecordService recordService; + + @Autowired + private ImageRepository imageRepository; + + @Autowired + private ImageService imageService; + + private Book book; + private User user; + private Group group; + private UserGroup userGroup; + private Record record; + + @BeforeEach + void setUp() { + book = new Book("김진용의 인생", 456, "image", "김진용", "책설명", "링크url", 5.0); + bookRepository.save(book); + + user = new User("김진용", "kje@naver.com", "1234", "urlurl"); + userRepository.save(user); + + group = new Group(book, null, null, "독서 그룹", "독서를 위한 그룹", 1234L, true, false); + groupRepository.save(group); + + userGroup = new UserGroup(user, group, Role.MASTER); + userGroupRepository.save(userGroup); + + record = new Record(userGroup, "재미있네", 159); + recordRepository.save(record); + } + + @Test + void 기록에_이미지_저장() { + //When + imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); + + List images = imageRepository.findAllByRecordId(record.getId()); + + //Then + assertEquals(3, images.size()); + } + + @Test + void 기록_이미지_조회() { + //When + imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); + List imageUrls = imageService.viewImages(record.getId()); + + //Then + assertEquals("url1", imageUrls.get(0).getImageUrls()); + assertEquals("url2", imageUrls.get(1).getImageUrls()); + assertEquals("url3", imageUrls.get(2).getImageUrls()); + } + + @Test + void 기록_이미지_삭제() { + //When + imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); + List images = imageRepository.findAllByRecordId(record.getId()); + + // 삭제 전 + assertEquals(Boolean.FALSE, images.get(0).getIsDeleted()); + assertEquals(Boolean.FALSE, images.get(1).getIsDeleted()); + assertEquals(Boolean.FALSE, images.get(2).getIsDeleted()); + + //Then + imageService.deleteImage(images.get(1).getId()); + + // 삭제 후 + assertEquals(Boolean.FALSE, images.get(0).getIsDeleted()); + assertEquals(Boolean.TRUE, images.get(1).getIsDeleted()); + assertEquals(Boolean.FALSE, images.get(2).getIsDeleted()); + } +} \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 7bfe24e..3ac8ad2 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -22,6 +22,7 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest +@Transactional class RecordServiceTest { @Autowired diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java index 073e414..9d3b751 100644 --- a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -15,6 +15,7 @@ import org.springframework.boot.test.context.SpringBootTest; @SpringBootTest +@Transactional class ReviewServiceTest { @Autowired From b3663008315c6749c398eab86e09c6590cdd08a3 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 17:21:25 +0900 Subject: [PATCH 25/29] =?UTF-8?q?refactor(=EB=84=88=EB=AC=B4=20=EB=A7=8E?= =?UTF-8?q?=EC=9D=8C):=20=EB=A9=94=EC=9D=B8=20=EB=B8=8C=EB=9E=9C=EC=B9=98?= =?UTF-8?q?=EC=97=90=EC=84=9C=20merge=20=ED=9B=84,=20=EC=9E=90=EC=9E=98?= =?UTF-8?q?=ED=95=9C=20=EC=98=A4=EB=A5=98=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 수정 후, 테스트 코드 작동으로 올바르게 merge 된 것을 확인 했습니다. --- .../image/controller/ImageController.java | 12 +++++------ ...eSaveRequest.java => ImageSaveReqDto.java} | 2 +- ...ListResponse.java => ImageListResDto.java} | 6 +++--- .../domain/image/service/ImageService.java | 14 ++++++------- .../record/controller/RecordController.java | 20 +++++++++---------- .../RecordReqDto.java} | 4 ++-- .../UpdateRecordReqDto.java} | 4 ++-- .../RecordListResDto.java} | 8 ++++---- .../backend/domain/record/entity/Record.java | 16 +++++++-------- .../domain/record/service/RecordService.java | 20 +++++++++---------- .../review/controller/ReviewController.java | 16 +++++++-------- .../ReviewReqDto.java} | 4 ++-- .../ReviewListResDto.java} | 10 +++++----- .../backend/domain/review/entity/Review.java | 14 ++++++------- .../domain/review/service/ReviewService.java | 17 ++++++++-------- .../domain/review/service/UserRepository.java | 7 ------- .../image/service/ImageServiceTest.java | 14 ++++++------- .../record/service/RecordServiceTest.java | 20 +++++++++---------- .../review/service/ReviewServiceTest.java | 15 +++++++------- 19 files changed, 109 insertions(+), 114 deletions(-) rename src/main/java/com/bookmile/backend/domain/image/dto/req/{ImageSaveRequest.java => ImageSaveReqDto.java} (86%) rename src/main/java/com/bookmile/backend/domain/image/dto/res/{ImageListResponse.java => ImageListResDto.java} (67%) rename src/main/java/com/bookmile/backend/domain/record/dto/{RequestRecord.java => req/RecordReqDto.java} (64%) rename src/main/java/com/bookmile/backend/domain/record/dto/{RequestUpdateRecord.java => req/UpdateRecordReqDto.java} (63%) rename src/main/java/com/bookmile/backend/domain/record/dto/{RecordListResponse.java => res/RecordListResDto.java} (71%) rename src/main/java/com/bookmile/backend/domain/review/dto/{ReviewRequest.java => req/ReviewReqDto.java} (63%) rename src/main/java/com/bookmile/backend/domain/review/dto/{ReviewListResponse.java => res/ReviewListResDto.java} (61%) delete mode 100644 src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java diff --git a/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java b/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java index aefc755..685a036 100644 --- a/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java +++ b/src/main/java/com/bookmile/backend/domain/image/controller/ImageController.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.image.controller; -import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; -import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.dto.req.ImageSaveReqDto; +import com.bookmile.backend.domain.image.dto.res.ImageListResDto; import com.bookmile.backend.domain.image.service.ImageService; import java.util.List; import lombok.RequiredArgsConstructor; @@ -23,15 +23,15 @@ public class ImageController { private ImageService imageService; @GetMapping("{recordId}") - public ResponseEntity> viewImages(@PathVariable Long recordId) { - List images = imageService.viewImages(recordId); + public ResponseEntity> viewImages(@PathVariable Long recordId) { + List images = imageService.viewImages(recordId); return ResponseEntity.ok(images); } @PostMapping - public ResponseEntity saveImages(@RequestParam Long recordId, @RequestBody ImageSaveRequest imageSaveRequest) { - imageService.saveImages(recordId, imageSaveRequest); + public ResponseEntity saveImages(@RequestParam Long recordId, @RequestBody ImageSaveReqDto imageSaveReqDto) { + imageService.saveImages(recordId, imageSaveReqDto); return ResponseEntity.ok("생성 완료!"); } diff --git a/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java b/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveReqDto.java similarity index 86% rename from src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java rename to src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveReqDto.java index 46bdbd8..67db6c3 100644 --- a/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveRequest.java +++ b/src/main/java/com/bookmile/backend/domain/image/dto/req/ImageSaveReqDto.java @@ -6,6 +6,6 @@ @Getter @AllArgsConstructor -public class ImageSaveRequest { +public class ImageSaveReqDto { private List imageUrls; } diff --git a/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java b/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResDto.java similarity index 67% rename from src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java rename to src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResDto.java index 515d8cb..24f191d 100644 --- a/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/image/dto/res/ImageListResDto.java @@ -6,11 +6,11 @@ @Getter @AllArgsConstructor -public class ImageListResponse { +public class ImageListResDto { private String imageUrls; - public static ImageListResponse createImage(Image image) { - return new ImageListResponse( + public static ImageListResDto createImage(Image image) { + return new ImageListResDto( image.getImageUrl() ); } diff --git a/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java b/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java index 871c1b2..0ac8109 100644 --- a/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java +++ b/src/main/java/com/bookmile/backend/domain/image/service/ImageService.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.image.service; -import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; -import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.dto.req.ImageSaveReqDto; +import com.bookmile.backend.domain.image.dto.res.ImageListResDto; import com.bookmile.backend.domain.image.entity.Image; import com.bookmile.backend.domain.image.repository.ImageRepository; import com.bookmile.backend.domain.record.entity.Record; @@ -16,22 +16,22 @@ public class ImageService { private final ImageRepository imageRepository; private final RecordRepository recordRepository; - - public List viewImages(Long recordId) { + + public List viewImages(Long recordId) { recordRepository.findById(recordId) .orElseThrow(() -> new IllegalArgumentException("없는 recordId 입니다.")); return imageRepository.findAllByRecordId(recordId) .stream() - .map(ImageListResponse::createImage) + .map(ImageListResDto::createImage) .toList(); } - public void saveImages(Long recordId, ImageSaveRequest imageSaveRequest) { + public void saveImages(Long recordId, ImageSaveReqDto imageSaveReqDto) { Record record = recordRepository.findById(recordId) .orElseThrow(() -> new IllegalArgumentException("없는 기록 ID 입니다.")); - List images = imageSaveRequest.getImageUrls().stream() + List images = imageSaveReqDto.getImageUrls().stream() .map(url -> new Image(record, url)) .toList(); diff --git a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java index d704440..c3c21db 100644 --- a/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java +++ b/src/main/java/com/bookmile/backend/domain/record/controller/RecordController.java @@ -1,8 +1,8 @@ package com.bookmile.backend.domain.record.controller; -import com.bookmile.backend.domain.record.dto.RecordListResponse; -import com.bookmile.backend.domain.record.dto.RequestRecord; -import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; +import com.bookmile.backend.domain.record.dto.req.RecordReqDto; +import com.bookmile.backend.domain.record.dto.req.UpdateRecordReqDto; +import com.bookmile.backend.domain.record.dto.res.RecordListResDto; import com.bookmile.backend.domain.record.service.RecordService; import java.util.List; import lombok.RequiredArgsConstructor; @@ -23,23 +23,23 @@ public class RecordController { private RecordService recordService; @GetMapping - public ResponseEntity> viewRecordList(@RequestParam Long groupId, - @RequestParam Long userId) { - List records = recordService.viewRecordList(groupId, userId); + public ResponseEntity> viewRecordList(@RequestParam Long groupId, + @RequestParam Long userId) { + List records = recordService.viewRecordList(groupId, userId); return ResponseEntity.ok(records); } @PostMapping public ResponseEntity createRecord(@RequestParam Long groupId, - @RequestParam Long userId, @RequestBody RequestRecord requestRecord) { - Long recordId = recordService.createRecord(groupId, userId, requestRecord); + @RequestParam Long userId, @RequestBody RecordReqDto recordReqDto) { + Long recordId = recordService.createRecord(groupId, userId, recordReqDto); return ResponseEntity.ok(recordId); } @PutMapping("/{recordId}") public ResponseEntity updateRecord(@PathVariable Long recordId, - @RequestBody RequestUpdateRecord requestUpdateRecord) { - Long updateRecord = recordService.updateRecord(recordId, requestUpdateRecord); + @RequestBody UpdateRecordReqDto updateRecordReqDto) { + Long updateRecord = recordService.updateRecord(recordId, updateRecordReqDto); return ResponseEntity.ok(updateRecord); } } diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/req/RecordReqDto.java similarity index 64% rename from src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java rename to src/main/java/com/bookmile/backend/domain/record/dto/req/RecordReqDto.java index dccaa57..efa4f34 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RequestRecord.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/req/RecordReqDto.java @@ -1,11 +1,11 @@ -package com.bookmile.backend.domain.record.dto; +package com.bookmile.backend.domain.record.dto.req; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor -public class RequestRecord { +public class RecordReqDto { private String text; private Integer currentPage; } diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java b/src/main/java/com/bookmile/backend/domain/record/dto/req/UpdateRecordReqDto.java similarity index 63% rename from src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java rename to src/main/java/com/bookmile/backend/domain/record/dto/req/UpdateRecordReqDto.java index 284992f..c77ade7 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RequestUpdateRecord.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/req/UpdateRecordReqDto.java @@ -1,4 +1,4 @@ -package com.bookmile.backend.domain.record.dto; +package com.bookmile.backend.domain.record.dto.req; import lombok.AllArgsConstructor; import lombok.Getter; @@ -6,7 +6,7 @@ @Getter @AllArgsConstructor -public class RequestUpdateRecord { +public class UpdateRecordReqDto { private String text; private Integer currentPage; } diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java b/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java similarity index 71% rename from src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java rename to src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java index 0fcc818..76d3f58 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/RecordListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java @@ -1,4 +1,4 @@ -package com.bookmile.backend.domain.record.dto; +package com.bookmile.backend.domain.record.dto.res; import com.bookmile.backend.domain.record.entity.Record; import java.time.LocalDateTime; @@ -7,14 +7,14 @@ @Getter @AllArgsConstructor -public class RecordListResponse { +public class RecordListResDto { private Long recordId; private String text; private Integer current_page; private LocalDateTime createdAt; - public static RecordListResponse createRecord(Record record) { - return new RecordListResponse( + public static RecordListResDto createRecord(Record record) { + return new RecordListResDto( record.getId(), record.getText(), record.getCurrentPage(), diff --git a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java index b6e2ab3..7d52d2d 100644 --- a/src/main/java/com/bookmile/backend/domain/record/entity/Record.java +++ b/src/main/java/com/bookmile/backend/domain/record/entity/Record.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.record.entity; -import com.bookmile.backend.domain.record.dto.RequestRecord; -import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; +import com.bookmile.backend.domain.record.dto.req.RecordReqDto; +import com.bookmile.backend.domain.record.dto.req.UpdateRecordReqDto; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; @@ -44,17 +44,17 @@ public Record(UserGroup userGroup, String text, Integer currentPage) { this.currentPage = currentPage; } - public static Record from(UserGroup userGroup, RequestRecord requestRecord) { + public static Record from(UserGroup userGroup, RecordReqDto recordReqDto) { return new Record( userGroup, - requestRecord.getText(), - requestRecord.getCurrentPage() + recordReqDto.getText(), + recordReqDto.getCurrentPage() ); } - public void update(RequestUpdateRecord requestUpdateRecord) { - this.text = requestUpdateRecord.getText(); - this.currentPage = requestUpdateRecord.getCurrentPage(); + public void update(UpdateRecordReqDto updateRecordReqDto) { + this.text = updateRecordReqDto.getText(); + this.currentPage = updateRecordReqDto.getCurrentPage(); } } diff --git a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java index b18a280..5cbcd0d 100644 --- a/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java +++ b/src/main/java/com/bookmile/backend/domain/record/service/RecordService.java @@ -1,13 +1,13 @@ package com.bookmile.backend.domain.record.service; import com.bookmile.backend.domain.group.entity.Group; -import com.bookmile.backend.domain.record.dto.RecordListResponse; -import com.bookmile.backend.domain.record.dto.RequestRecord; -import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; +import com.bookmile.backend.domain.record.dto.req.RecordReqDto; +import com.bookmile.backend.domain.record.dto.req.UpdateRecordReqDto; +import com.bookmile.backend.domain.record.dto.res.RecordListResDto; import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; -import com.bookmile.backend.domain.review.service.UserRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.user.repository.UserRepository; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import java.util.List; import lombok.RequiredArgsConstructor; @@ -21,7 +21,7 @@ public class RecordService { private final UserGroupRepository userGroupRepository; private final RecordRepository recordRepository; - public List viewRecordList(Long groupId, Long userId) { + public List viewRecordList(Long groupId, Long userId) { Group group = groupRepository.findById(groupId) .orElseThrow(() -> new IllegalArgumentException("없는 그룹입니다.")); User user = userRepository.findById(userId) @@ -31,11 +31,11 @@ public List viewRecordList(Long groupId, Long userId) { .orElseThrow(() -> new IllegalArgumentException("해당 그룹에 속하지 않은 사용자입니다.")); return recordRepository.findAllByUserGroupId(userGroupId).stream() - .map(RecordListResponse::createRecord) + .map(RecordListResDto::createRecord) .toList(); } - public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) { + public Long createRecord(Long groupId, Long userId, RecordReqDto recordReqDto) { Group group = groupRepository.findById(groupId) .orElseThrow(() -> new IllegalArgumentException("없는 그룹입니다.")); User user = userRepository.findById(userId) @@ -46,17 +46,17 @@ public Long createRecord(Long groupId, Long userId, RequestRecord requestRecord) UserGroup userGroup = userGroupRepository.findUserGroupById(userGroupId); - Record record = Record.from(userGroup, requestRecord); + Record record = Record.from(userGroup, recordReqDto); recordRepository.save(record); return record.getId(); } - public Long updateRecord(Long recordId, RequestUpdateRecord requestUpdateRecord) { + public Long updateRecord(Long recordId, UpdateRecordReqDto updateRecordReqDto) { Record record = recordRepository.findById(recordId) .orElseThrow(() -> new IllegalArgumentException("없는 기록입니다.")); - record.update(requestUpdateRecord); + record.update(updateRecordReqDto); return record.getId(); } diff --git a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java index 94cba64..8830460 100644 --- a/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java +++ b/src/main/java/com/bookmile/backend/domain/review/controller/ReviewController.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.review.controller; -import com.bookmile.backend.domain.review.dto.ReviewListResponse; -import com.bookmile.backend.domain.review.dto.ReviewRequest; +import com.bookmile.backend.domain.review.dto.req.ReviewReqDto; +import com.bookmile.backend.domain.review.dto.res.ReviewListResDto; import com.bookmile.backend.domain.review.service.ReviewService; import java.util.List; import lombok.RequiredArgsConstructor; @@ -23,21 +23,21 @@ public class ReviewController { private final ReviewService reviewService; @GetMapping - public ResponseEntity> viewReviewList(@RequestParam Long bookId) { - List reviews = reviewService.viewReviewList(bookId); + public ResponseEntity> viewReviewList(@RequestParam Long bookId) { + List reviews = reviewService.viewReviewList(bookId); return ResponseEntity.ok(reviews); } @PostMapping public ResponseEntity createReview(@RequestParam Long bookId, @RequestParam Long userId, - @RequestBody ReviewRequest reviewRequest) { - Long createReview = reviewService.createReview(bookId, userId, reviewRequest); + @RequestBody ReviewReqDto reviewReqDto) { + Long createReview = reviewService.createReview(bookId, userId, reviewReqDto); return ResponseEntity.ok(createReview); } @PutMapping("/{reviewId}") - public ResponseEntity updateReview(@PathVariable Long reviewId, @RequestBody ReviewRequest reviewRequest) { - Long updateReview = reviewService.updateReview(reviewId, reviewRequest); + public ResponseEntity updateReview(@PathVariable Long reviewId, @RequestBody ReviewReqDto reviewReqDto) { + Long updateReview = reviewService.updateReview(reviewId, reviewReqDto); return ResponseEntity.ok(updateReview); } diff --git a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java b/src/main/java/com/bookmile/backend/domain/review/dto/req/ReviewReqDto.java similarity index 63% rename from src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java rename to src/main/java/com/bookmile/backend/domain/review/dto/req/ReviewReqDto.java index e6a7076..7eaf0bc 100644 --- a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewRequest.java +++ b/src/main/java/com/bookmile/backend/domain/review/dto/req/ReviewReqDto.java @@ -1,11 +1,11 @@ -package com.bookmile.backend.domain.review.dto; +package com.bookmile.backend.domain.review.dto.req; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor -public class ReviewRequest { +public class ReviewReqDto { private Double rating; private String text; } diff --git a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java b/src/main/java/com/bookmile/backend/domain/review/dto/res/ReviewListResDto.java similarity index 61% rename from src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java rename to src/main/java/com/bookmile/backend/domain/review/dto/res/ReviewListResDto.java index 37abe05..d05750e 100644 --- a/src/main/java/com/bookmile/backend/domain/review/dto/ReviewListResponse.java +++ b/src/main/java/com/bookmile/backend/domain/review/dto/res/ReviewListResDto.java @@ -1,4 +1,4 @@ -package com.bookmile.backend.domain.review.dto; +package com.bookmile.backend.domain.review.dto.res; import com.bookmile.backend.domain.review.entity.Review; import lombok.AllArgsConstructor; @@ -6,16 +6,16 @@ @Getter @AllArgsConstructor -public class ReviewListResponse { +public class ReviewListResDto { private Long userId; private String name; private Double rating; private String text; - public static ReviewListResponse createReview(Review review) { - return new ReviewListResponse( + public static ReviewListResDto createReview(Review review) { + return new ReviewListResDto( review.getId(), - review.getUser().getName(), + review.getUser().getNickname(), review.getRating(), review.getText() ); diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index 822315d..c5ab6e6 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -1,7 +1,7 @@ package com.bookmile.backend.domain.review.entity; import com.bookmile.backend.domain.book.entity.Book; -import com.bookmile.backend.domain.review.dto.ReviewRequest; +import com.bookmile.backend.domain.review.dto.req.ReviewReqDto; import com.bookmile.backend.domain.user.entity.User; import com.bookmile.backend.global.config.BaseEntity; import jakarta.persistence.Column; @@ -52,18 +52,18 @@ public Review(User user, Book book, Double rating, String text) { this.text = text; } - public static Review from(User user, Book book, ReviewRequest reviewRequest) { + public static Review from(User user, Book book, ReviewReqDto reviewReqDto) { return new Review( user, book, - reviewRequest.getRating(), - reviewRequest.getText() + reviewReqDto.getRating(), + reviewReqDto.getText() ); } - public void update(ReviewRequest reviewRequest) { - this.rating = reviewRequest.getRating(); - this.text = reviewRequest.getText(); + public void update(ReviewReqDto reviewReqDto) { + this.rating = reviewReqDto.getRating(); + this.text = reviewReqDto.getText(); } public void delete(Review review) { diff --git a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java index af45f8c..d8fdbed 100644 --- a/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java +++ b/src/main/java/com/bookmile/backend/domain/review/service/ReviewService.java @@ -1,11 +1,12 @@ package com.bookmile.backend.domain.review.service; import com.bookmile.backend.domain.book.entity.Book; -import com.bookmile.backend.domain.review.dto.ReviewListResponse; -import com.bookmile.backend.domain.review.dto.ReviewRequest; +import com.bookmile.backend.domain.review.dto.req.ReviewReqDto; +import com.bookmile.backend.domain.review.dto.res.ReviewListResDto; import com.bookmile.backend.domain.review.entity.Review; import com.bookmile.backend.domain.review.repository.ReviewRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.user.repository.UserRepository; import java.util.List; import java.util.stream.Collectors; import lombok.RequiredArgsConstructor; @@ -18,34 +19,34 @@ public class ReviewService { private final BookRepository bookRepository; private final UserRepository userRepository; - public List viewReviewList(Long bookId) { + public List viewReviewList(Long bookId) { Book book = bookRepository.findById(bookId) .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); return reviewRepository.findAllByBookId(bookId).stream() - .map(ReviewListResponse::createReview) + .map(ReviewListResDto::createReview) .collect(Collectors.toList()); } - public Long createReview(Long bookId, Long userId, ReviewRequest reviewRequest) { + public Long createReview(Long bookId, Long userId, ReviewReqDto reviewReqDto) { Book book = bookRepository.findById(bookId) .orElseThrow(() -> new IllegalArgumentException("없는 책입니다.")); User user = userRepository.findById(userId) .orElseThrow(() -> new IllegalArgumentException("없는 사용자입니다.")); - Review review = Review.from(user, book, reviewRequest); + Review review = Review.from(user, book, reviewReqDto); reviewRepository.save(review); return review.getId(); } - public Long updateReview(Long reviewId, ReviewRequest reviewRequest) { + public Long updateReview(Long reviewId, ReviewReqDto reviewReqDto) { Review review = reviewRepository.findById(reviewId) .orElseThrow(() -> new IllegalArgumentException("없는 리뷰입니다.")); - review.update(reviewRequest); + review.update(reviewReqDto); reviewRepository.save(review); return review.getId(); diff --git a/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java b/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java deleted file mode 100644 index dd267d8..0000000 --- a/src/main/java/com/bookmile/backend/domain/review/service/UserRepository.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.bookmile.backend.domain.review.service; - -import com.bookmile.backend.domain.user.entity.User; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface UserRepository extends JpaRepository { -} diff --git a/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java b/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java index 2bccc24..c766467 100644 --- a/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/image/service/ImageServiceTest.java @@ -5,8 +5,8 @@ import com.bookmile.backend.domain.book.entity.Book; import com.bookmile.backend.domain.group.entity.Group; import com.bookmile.backend.domain.group.entity.Role; -import com.bookmile.backend.domain.image.dto.req.ImageSaveRequest; -import com.bookmile.backend.domain.image.dto.res.ImageListResponse; +import com.bookmile.backend.domain.image.dto.req.ImageSaveReqDto; +import com.bookmile.backend.domain.image.dto.res.ImageListResDto; import com.bookmile.backend.domain.image.entity.Image; import com.bookmile.backend.domain.image.repository.ImageRepository; import com.bookmile.backend.domain.record.entity.Record; @@ -15,8 +15,8 @@ import com.bookmile.backend.domain.record.service.RecordService; import com.bookmile.backend.domain.record.service.UserGroupRepository; import com.bookmile.backend.domain.review.service.BookRepository; -import com.bookmile.backend.domain.review.service.UserRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.user.repository.UserRepository; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import jakarta.transaction.Transactional; import java.util.List; @@ -79,7 +79,7 @@ record = new Record(userGroup, "재미있네", 159); @Test void 기록에_이미지_저장() { //When - imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); + imageService.saveImages(record.getId(), new ImageSaveReqDto(List.of("url1", "url2", "url3"))); List images = imageRepository.findAllByRecordId(record.getId()); @@ -90,8 +90,8 @@ record = new Record(userGroup, "재미있네", 159); @Test void 기록_이미지_조회() { //When - imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); - List imageUrls = imageService.viewImages(record.getId()); + imageService.saveImages(record.getId(), new ImageSaveReqDto(List.of("url1", "url2", "url3"))); + List imageUrls = imageService.viewImages(record.getId()); //Then assertEquals("url1", imageUrls.get(0).getImageUrls()); @@ -102,7 +102,7 @@ record = new Record(userGroup, "재미있네", 159); @Test void 기록_이미지_삭제() { //When - imageService.saveImages(record.getId(), new ImageSaveRequest(List.of("url1", "url2", "url3"))); + imageService.saveImages(record.getId(), new ImageSaveReqDto(List.of("url1", "url2", "url3"))); List images = imageRepository.findAllByRecordId(record.getId()); // 삭제 전 diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 3ac8ad2..03c5457 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -5,14 +5,14 @@ import com.bookmile.backend.domain.book.entity.Book; import com.bookmile.backend.domain.group.entity.Group; import com.bookmile.backend.domain.group.entity.Role; -import com.bookmile.backend.domain.record.dto.RecordListResponse; -import com.bookmile.backend.domain.record.dto.RequestRecord; -import com.bookmile.backend.domain.record.dto.RequestUpdateRecord; +import com.bookmile.backend.domain.record.dto.req.RecordReqDto; +import com.bookmile.backend.domain.record.dto.req.UpdateRecordReqDto; +import com.bookmile.backend.domain.record.dto.res.RecordListResDto; import com.bookmile.backend.domain.record.entity.Record; import com.bookmile.backend.domain.record.repository.RecordRepository; import com.bookmile.backend.domain.review.service.BookRepository; -import com.bookmile.backend.domain.review.service.UserRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.user.repository.UserRepository; import com.bookmile.backend.domain.userGroup.entity.UserGroup; import jakarta.transaction.Transactional; import java.util.List; @@ -67,7 +67,7 @@ void setUp() { void 사용자의_그룹에서의_기록_생성() { //When Long recordId1 = recordService.createRecord(group.getId(), user.getId(), - new RequestRecord("나의 기록1", 193)); + new RecordReqDto("나의 기록1", 193)); Record record = recordRepository.findById(recordId1).orElseThrow(); //Then @@ -82,15 +82,15 @@ void setUp() { recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("기록1", 193)); + new RecordReqDto("기록1", 193)); recordService.createRecord( group.getId(), user.getId(), - new RequestRecord("기록2", 234)); + new RecordReqDto("기록2", 234)); //When - List records = recordService.viewRecordList(group.getId(), user.getId()); + List records = recordService.viewRecordList(group.getId(), user.getId()); //Then assertEquals(2, records.size()); @@ -106,7 +106,7 @@ void setUp() { //When // 일단 생성 Long recordId = recordService.createRecord(group.getId(), user.getId(), - new RequestRecord("나의 기록1", 193)); + new RecordReqDto("나의 기록1", 193)); Record record1 = recordRepository.findById(recordId).orElseThrow(); // 수정 전 @@ -114,7 +114,7 @@ void setUp() { // 그리고 수정 Long updateRecord = recordService.updateRecord(recordId, - new RequestUpdateRecord("나의 수정한 기록1", 193)); + new UpdateRecordReqDto("나의 수정한 기록1", 193)); Record record2 = recordRepository.findById(updateRecord).orElseThrow(); //Then diff --git a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java index 9d3b751..403ad08 100644 --- a/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/review/service/ReviewServiceTest.java @@ -3,11 +3,12 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import com.bookmile.backend.domain.book.entity.Book; -import com.bookmile.backend.domain.review.dto.ReviewListResponse; -import com.bookmile.backend.domain.review.dto.ReviewRequest; +import com.bookmile.backend.domain.review.dto.req.ReviewReqDto; +import com.bookmile.backend.domain.review.dto.res.ReviewListResDto; import com.bookmile.backend.domain.review.entity.Review; import com.bookmile.backend.domain.review.repository.ReviewRepository; import com.bookmile.backend.domain.user.entity.User; +import com.bookmile.backend.domain.user.repository.UserRepository; import jakarta.transaction.Transactional; import java.util.List; import org.junit.jupiter.api.Test; @@ -49,7 +50,7 @@ class ReviewServiceTest { reviewRepository.save(review2); // When - List reviews = reviewService.viewReviewList(book.getId()); + List reviews = reviewService.viewReviewList(book.getId()); // Then assertEquals(2, reviews.size()); @@ -66,10 +67,10 @@ class ReviewServiceTest { userRepository.save(user); bookRepository.save(book); - ReviewRequest reviewRequest = new ReviewRequest(4.5, "굳굳"); + ReviewReqDto reviewReqDto = new ReviewReqDto(4.5, "굳굳"); // When - Long reviewId = reviewService.createReview(book.getId(), user.getId(), reviewRequest); + Long reviewId = reviewService.createReview(book.getId(), user.getId(), reviewReqDto); // Then Review review = reviewRepository.findById(reviewId).orElseThrow(); @@ -90,10 +91,10 @@ class ReviewServiceTest { reviewRepository.save(review); - ReviewRequest reviewRequest = new ReviewRequest(1.0, "다시 생각해보니 별로네요."); + ReviewReqDto reviewReqDto = new ReviewReqDto(1.0, "다시 생각해보니 별로네요."); // When - Long reviewId = reviewService.updateReview(review.getId(), reviewRequest); + Long reviewId = reviewService.updateReview(review.getId(), reviewReqDto); // Then Review updateReview = reviewRepository.findById(reviewId).orElseThrow(); From 41bb6239ce358ac658d81a1953014f925e9a72b8 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 17:38:03 +0900 Subject: [PATCH 26/29] =?UTF-8?q?refactor(Review):=20Review=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1=EC=9E=90=20=EB=B9=8C=EB=8D=94=ED=8C=A8=ED=84=B4=20?= =?UTF-8?q?=EC=A0=81=EC=9A=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../backend/domain/review/entity/Review.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index c5ab6e6..3f092a0 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -14,6 +14,7 @@ import jakarta.persistence.ManyToOne; import lombok.AccessLevel; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; import lombok.NoArgsConstructor; @@ -45,6 +46,7 @@ public class Review extends BaseEntity { @Column(nullable = false) private Boolean isDeleted = false; + @Builder public Review(User user, Book book, Double rating, String text) { this.user = user; this.book = book; @@ -53,12 +55,18 @@ public Review(User user, Book book, Double rating, String text) { } public static Review from(User user, Book book, ReviewReqDto reviewReqDto) { - return new Review( - user, - book, - reviewReqDto.getRating(), - reviewReqDto.getText() - ); +// return new Review( +// user, +// book, +// reviewReqDto.getRating(), +// reviewReqDto.getText() +// ); + return Review.builder() + .user(user) + .book(book) + .rating(reviewReqDto.getRating()) + .text(reviewReqDto.getText()) + .build(); } public void update(ReviewReqDto reviewReqDto) { From 11b5c43f5265feb06c5cc8ce3329aa2dddf4be71 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 17:38:46 +0900 Subject: [PATCH 27/29] =?UTF-8?q?refactor(Review):=20=EC=A3=BC=EC=84=9D=20?= =?UTF-8?q?=EC=A0=9C=EA=B1=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/bookmile/backend/domain/review/entity/Review.java | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java index 3f092a0..27a9930 100644 --- a/src/main/java/com/bookmile/backend/domain/review/entity/Review.java +++ b/src/main/java/com/bookmile/backend/domain/review/entity/Review.java @@ -55,12 +55,6 @@ public Review(User user, Book book, Double rating, String text) { } public static Review from(User user, Book book, ReviewReqDto reviewReqDto) { -// return new Review( -// user, -// book, -// reviewReqDto.getRating(), -// reviewReqDto.getText() -// ); return Review.builder() .user(user) .book(book) From e79fd22e9e07bf9023525950a53dde02d15e7635 Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 17:47:09 +0900 Subject: [PATCH 28/29] =?UTF-8?q?refactor(RecordListResDto,=20RecordServic?= =?UTF-8?q?eTest):=20=EB=B9=8C=EB=8D=94=20=ED=8C=A8=ED=84=B4=20=EC=A0=81?= =?UTF-8?q?=EC=9A=A9=20=EB=B0=8F=20=EB=B3=80=EC=88=98=EB=AA=85=20=EC=88=98?= =?UTF-8?q?=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../domain/record/dto/res/RecordListResDto.java | 16 +++++++++------- .../domain/record/service/RecordServiceTest.java | 4 ++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java b/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java index 76d3f58..a6eb17a 100644 --- a/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java +++ b/src/main/java/com/bookmile/backend/domain/record/dto/res/RecordListResDto.java @@ -3,22 +3,24 @@ import com.bookmile.backend.domain.record.entity.Record; import java.time.LocalDateTime; import lombok.AllArgsConstructor; +import lombok.Builder; import lombok.Getter; @Getter +@Builder @AllArgsConstructor public class RecordListResDto { private Long recordId; private String text; - private Integer current_page; + private Integer currentPage; private LocalDateTime createdAt; public static RecordListResDto createRecord(Record record) { - return new RecordListResDto( - record.getId(), - record.getText(), - record.getCurrentPage(), - record.getCreatedAt() - ); + return RecordListResDto.builder() + .recordId(record.getId()) + .text(record.getText()) + .currentPage(record.getCurrentPage()) + .createdAt(record.getCreatedAt()) + .build(); } } \ No newline at end of file diff --git a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java index 03c5457..7905676 100644 --- a/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java +++ b/src/test/java/com/bookmile/backend/domain/record/service/RecordServiceTest.java @@ -96,8 +96,8 @@ void setUp() { assertEquals(2, records.size()); assertEquals("기록1", records.get(0).getText()); assertEquals("기록2", records.get(1).getText()); - assertEquals(193, records.get(0).getCurrent_page()); - assertEquals(234, records.get(1).getCurrent_page()); + assertEquals(193, records.get(0).getCurrentPage()); + assertEquals(234, records.get(1).getCurrentPage()); } @Transactional From e44166626a2363479d9ad4aff284751b6c6a3e1f Mon Sep 17 00:00:00 2001 From: kingjinyong Date: Fri, 6 Dec 2024 17:55:54 +0900 Subject: [PATCH 29/29] =?UTF-8?q?refactor(UserGroup):=20=ED=95=84=EC=9A=94?= =?UTF-8?q?=20=EC=97=86=EB=8A=94=20=EB=A9=94=EC=84=9C=EB=93=9C=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../bookmile/backend/domain/userGroup/entity/UserGroup.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java b/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java index f2f4dbd..544d310 100644 --- a/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java +++ b/src/main/java/com/bookmile/backend/domain/userGroup/entity/UserGroup.java @@ -52,10 +52,6 @@ public class UserGroup extends BaseEntity { @Column(nullable = false) private Boolean isDeleted = false; - public void addUser(User user) { - this.user = user; - } - public UserGroup(User user, Group group, Role role) { this.user = user; this.group = group;