From b3cacda452a8f0b40d49f13366576ca5a7dbbf9f Mon Sep 17 00:00:00 2001 From: JeongHoon Lee Date: Sun, 21 Jul 2024 20:45:09 +0900 Subject: [PATCH] =?UTF-8?q?Feat:=20=EC=A7=80=EC=9B=90=20=EA=B3=B5=EA=B3=A0?= =?UTF-8?q?=20=ED=9B=84=EA=B8=B0=20=EC=88=98=EC=A0=95=20API=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../review/controller/ReviewController.java | 20 +++++++ .../review/controller/port/ReviewService.java | 5 ++ .../kkijuk/server/review/domain/Review.java | 10 ++++ .../server/review/domain/ReviewUpdate.java | 27 +++++++++ .../infrastructure/ReviewRepositoryImpl.java | 2 +- .../review/service/ReviewServiceImpl.java | 14 ++++- .../review/service/ReviewServiceTest.java | 59 ++++++++++++++++++- 7 files changed, 134 insertions(+), 3 deletions(-) create mode 100644 src/main/java/umc/kkijuk/server/review/domain/ReviewUpdate.java diff --git a/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java b/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java index 2c46e877..07a8d178 100644 --- a/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java +++ b/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java @@ -13,6 +13,7 @@ import umc.kkijuk.server.review.controller.response.ReviewIdResponse; import umc.kkijuk.server.review.domain.Review; import umc.kkijuk.server.review.domain.ReviewCreate; +import umc.kkijuk.server.review.domain.ReviewUpdate; @Tag(name = "review", description = "모집 공고 후기 API") @RestController @@ -38,4 +39,23 @@ public ResponseEntity create( .ok() .body(ReviewIdResponse.from(review)); } + + @Operation( + summary = "지원 공고 후기 수정", + description = "주어진 지원 공고 후기를 수정합니다") + @Parameter(name = "recruitId", description = "지원 공고 ID", example = "1") + @Parameter(name = "reviewId", description = "지원 공고 후기 ID", example = "1") + @PostMapping("/review/{reviewId}") + public ResponseEntity update( + @PathVariable Long recruitId, + @PathVariable Long reviewId, + @RequestBody @Valid ReviewUpdate reviewUpdate + ) { + Recruit recruit = recruitService.getById(recruitId); + Review review = reviewService.update(recruit, reviewId, reviewUpdate); + + return ResponseEntity + .ok() + .body(ReviewIdResponse.from(review)); + } } diff --git a/src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java b/src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java index ac808038..52bf67f2 100644 --- a/src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java +++ b/src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java @@ -3,7 +3,12 @@ import umc.kkijuk.server.recruit.domain.Recruit; import umc.kkijuk.server.review.domain.Review; import umc.kkijuk.server.review.domain.ReviewCreate; +import umc.kkijuk.server.review.domain.ReviewUpdate; public interface ReviewService { Review create(Recruit recruit, ReviewCreate reviewCreate); + + Review update(Recruit recruit, Long reviewId, ReviewUpdate reviewUpdate); + + Review getById(Long reviewId); } diff --git a/src/main/java/umc/kkijuk/server/review/domain/Review.java b/src/main/java/umc/kkijuk/server/review/domain/Review.java index b8fc38e1..1ed880d7 100644 --- a/src/main/java/umc/kkijuk/server/review/domain/Review.java +++ b/src/main/java/umc/kkijuk/server/review/domain/Review.java @@ -23,4 +23,14 @@ public static Review from(Recruit recruit, ReviewCreate reviewCreate) { .date(reviewCreate.getDate()) .build(); } + + public Review update(ReviewUpdate reviewUpdate) { + return Review.builder() + .id(this.id) + .recruitId(this.recruitId) + .title(reviewUpdate.getTitle()) + .content(reviewUpdate.getContent()) + .date(reviewUpdate.getDate()) + .build(); + } } diff --git a/src/main/java/umc/kkijuk/server/review/domain/ReviewUpdate.java b/src/main/java/umc/kkijuk/server/review/domain/ReviewUpdate.java new file mode 100644 index 00000000..93251a78 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/domain/ReviewUpdate.java @@ -0,0 +1,27 @@ +package umc.kkijuk.server.review.domain; + +import com.fasterxml.jackson.annotation.JsonFormat; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.NotNull; +import lombok.*; + +import java.time.LocalDate; + +@Getter +@Builder +@AllArgsConstructor +@NoArgsConstructor(access = AccessLevel.PROTECTED) +public class ReviewUpdate { + @NotBlank(message = "공고 후기 제목은 필수 입력 항목입니다.") + @Schema(description = "변경될 공고 후기 제목", example = "변경될 제목", type = "string") + private String title; + + @Schema(description = "변경될 공고 후기 내용", example = "변경될 내용", type = "string") + private String content; + + @NotNull(message = "날짜는 필수 입력 항목입니다.") + @Schema(description = "날짜", example = "2024-07-23", pattern = "yyyy-MM-dd") + @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "Asia/Seoul") + private LocalDate date; +} diff --git a/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java index 8b9b40cb..1e2214ea 100644 --- a/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java +++ b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java @@ -26,6 +26,6 @@ public Optional findById(Long id) { @Override public Review getById(Long id) { return findById(id) - .orElseThrow(() -> new ResourceNotFoundException("recruit", id)); + .orElseThrow(() -> new ResourceNotFoundException("review", id)); } } diff --git a/src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java b/src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java index 850e1b62..0bf86798 100644 --- a/src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java @@ -8,6 +8,7 @@ import umc.kkijuk.server.review.controller.port.ReviewService; import umc.kkijuk.server.review.domain.Review; import umc.kkijuk.server.review.domain.ReviewCreate; +import umc.kkijuk.server.review.domain.ReviewUpdate; import umc.kkijuk.server.review.service.port.ReviewRepository; @Service @@ -24,7 +25,18 @@ public Review getById(Long id) { @Transactional //멤버 + recruit 사이의 인가 예외처리 public Review create(Recruit recruit, ReviewCreate reviewCreate) { Review review = Review.from(recruit, reviewCreate); - System.out.println("review.getRecruitId() = " + review.getRecruitId()); + return reviewRepository.save(review); + } + + @Override + @Transactional + public Review update(Recruit recruit, Long reviewId, ReviewUpdate reviewUpdate) { + Review review = getById(reviewId); + if (!review.getId().equals(recruit.getId())) { + // 잘못된 요청 처리 나중에 작성 + } + + review = review.update(reviewUpdate); return reviewRepository.save(review); } } diff --git a/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java b/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java index 8227171b..945dcaa9 100644 --- a/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java +++ b/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java @@ -7,6 +7,7 @@ import umc.kkijuk.server.review.controller.port.ReviewService; import umc.kkijuk.server.review.domain.Review; import umc.kkijuk.server.review.domain.ReviewCreate; +import umc.kkijuk.server.review.domain.ReviewUpdate; import umc.kkijuk.server.review.mock.FakeReviewRepository; import umc.kkijuk.server.review.service.port.ReviewRepository; @@ -25,6 +26,15 @@ void Init() { reviewService = ReviewServiceImpl.builder() .reviewRepository(reviewRepository) .build(); + + Review review = Review.builder() + .recruitId(3L) + .title("test-title") + .content("test-content") + .date(LocalDate.of(2024, 7, 21)) + .build(); + + reviewRepository.save(review); } @Test @@ -51,7 +61,7 @@ void Init() { } @Test - void crate_새로운_review_만들기_nullable() { + void create_새로운_review_만들기_nullable() { //given ReviewCreate reviewCreate = ReviewCreate.builder() .title("new-title") @@ -71,4 +81,51 @@ void Init() { () -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 21)) ); } + + @Test + void update_존재하던_review_수정() { + //given + Recruit recruit = Recruit.builder().id(3L).build(); + Long reviewId = 1L; + ReviewUpdate reviewUpdate = ReviewUpdate.builder() + .title("changed-title") + .content("changed-content") + .date(LocalDate.of(2024, 7, 30)) + .build(); + + //when + Review result = reviewService.update(recruit, reviewId, reviewUpdate); + + //then + assertAll( + () -> assertThat(result.getId()).isNotNull(), + () -> assertThat(result.getRecruitId()).isEqualTo(3L), + () -> assertThat(result.getTitle()).isEqualTo("changed-title"), + () -> assertThat(result.getContent()).isEqualTo("changed-content"), + () -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 30)) + ); + } + + @Test + void update_존재하던_review_수정_content_이미존재했지만_null로변경() { + //given + Recruit recruit = Recruit.builder().id(3L).build(); + Long reviewId = 1L; + ReviewUpdate reviewUpdate = ReviewUpdate.builder() + .title("changed-title") + .date(LocalDate.of(2024, 7, 30)) + .build(); + + //when + Review result = reviewService.update(recruit, reviewId, reviewUpdate); + + //then + assertAll( + () -> assertThat(result.getId()).isNotNull(), + () -> assertThat(result.getRecruitId()).isEqualTo(3L), + () -> assertThat(result.getTitle()).isEqualTo("changed-title"), + () -> assertThat(result.getContent()).isNull(), + () -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 30)) + ); + } } \ No newline at end of file