Skip to content

Commit

Permalink
Feat: 지원 공고 후기 수정 API 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
tape4 committed Jul 21, 2024
1 parent c92f3ae commit b3cacda
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -38,4 +39,23 @@ public ResponseEntity<ReviewIdResponse> 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<ReviewIdResponse> 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
10 changes: 10 additions & 0 deletions src/main/java/umc/kkijuk/server/review/domain/Review.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
27 changes: 27 additions & 0 deletions src/main/java/umc/kkijuk/server/review/domain/ReviewUpdate.java
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public Optional<Review> findById(Long id) {
@Override
public Review getById(Long id) {
return findById(id)
.orElseThrow(() -> new ResourceNotFoundException("recruit", id));
.orElseThrow(() -> new ResourceNotFoundException("review", id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -51,7 +61,7 @@ void Init() {
}

@Test
void crate_새로운_review_만들기_nullable() {
void create_새로운_review_만들기_nullable() {
//given
ReviewCreate reviewCreate = ReviewCreate.builder()
.title("new-title")
Expand All @@ -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))
);
}
}

0 comments on commit b3cacda

Please sign in to comment.