From c92f3aeb4ed11bdb9120fa925640ae6a43af26f8 Mon Sep 17 00:00:00 2001 From: JeongHoon Lee Date: Sun, 21 Jul 2024 20:19:11 +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=9E=91=EC=84=B1=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 --- .../recruit/controller/RecruitController.java | 10 ++- .../controller/port/RecruitService.java | 4 +- .../kkijuk/server/recruit/domain/Recruit.java | 16 ++-- ...cruitCreateDto.java => RecruitCreate.java} | 2 +- .../recruit/service/RecruitServiceImpl.java | 6 +- .../review/controller/ReviewController.java | 41 ++++++++++ .../review/controller/port/ReviewService.java | 9 +++ .../controller/response/ReviewIdResponse.java | 17 +++++ .../kkijuk/server/review/domain/Review.java | 26 +++++++ .../server/review/domain/ReviewCreate.java | 28 +++++++ .../review/infrastructure/ReviewEntity.java | 44 +++++++++++ .../infrastructure/ReviewJpaRepository.java | 6 ++ .../infrastructure/ReviewRepositoryImpl.java | 31 ++++++++ .../review/service/ReviewServiceImpl.java | 30 ++++++++ .../review/service/port/ReviewRepository.java | 13 ++++ .../recruit/service/RecruitServiceTest.java | 15 ++-- .../review/mock/FakeReviewRepository.java | 48 ++++++++++++ .../review/service/ReviewServiceTest.java | 74 +++++++++++++++++++ 18 files changed, 396 insertions(+), 24 deletions(-) rename src/main/java/umc/kkijuk/server/recruit/domain/{RecruitCreateDto.java => RecruitCreate.java} (98%) create mode 100644 src/main/java/umc/kkijuk/server/review/controller/ReviewController.java create mode 100644 src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java create mode 100644 src/main/java/umc/kkijuk/server/review/controller/response/ReviewIdResponse.java create mode 100644 src/main/java/umc/kkijuk/server/review/domain/Review.java create mode 100644 src/main/java/umc/kkijuk/server/review/domain/ReviewCreate.java create mode 100644 src/main/java/umc/kkijuk/server/review/infrastructure/ReviewEntity.java create mode 100644 src/main/java/umc/kkijuk/server/review/infrastructure/ReviewJpaRepository.java create mode 100644 src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java create mode 100644 src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java create mode 100644 src/main/java/umc/kkijuk/server/review/service/port/ReviewRepository.java create mode 100644 src/test/java/umc/kkijuk/server/review/mock/FakeReviewRepository.java create mode 100644 src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java diff --git a/src/main/java/umc/kkijuk/server/recruit/controller/RecruitController.java b/src/main/java/umc/kkijuk/server/recruit/controller/RecruitController.java index d7c8412f..5693ce69 100644 --- a/src/main/java/umc/kkijuk/server/recruit/controller/RecruitController.java +++ b/src/main/java/umc/kkijuk/server/recruit/controller/RecruitController.java @@ -33,9 +33,9 @@ public class RecruitController { summary = "지원 공고 생성", description = "주어진 정보를 바탕으로 지원 공고 데이터를 생성합니다.") @PostMapping - public ResponseEntity create(@RequestBody @Valid RecruitCreateDto recruitCreateDto) { + public ResponseEntity create(@RequestBody @Valid RecruitCreate recruitCreate) { LoginUser loginUser = LoginUser.get(); - Recruit recruit = recruitService.create(recruitCreateDto); + Recruit recruit = recruitService.create(recruitCreate); return ResponseEntity .status(HttpStatus.CREATED) .body(RecruitIdResponse.from(recruit)); @@ -120,4 +120,10 @@ public ResponseEntity findAllRecruitListByEnd .ok() .body(RecruitListByEndTimeAfterResponse.from(recruits)); } + + // 멤버 추가시 추가 +// @GetMapping("/list/valid") +// public ResponseEntity findValidRecruit() { +// +// } } diff --git a/src/main/java/umc/kkijuk/server/recruit/controller/port/RecruitService.java b/src/main/java/umc/kkijuk/server/recruit/controller/port/RecruitService.java index 4a9f9c5f..8526d14e 100644 --- a/src/main/java/umc/kkijuk/server/recruit/controller/port/RecruitService.java +++ b/src/main/java/umc/kkijuk/server/recruit/controller/port/RecruitService.java @@ -1,7 +1,7 @@ package umc.kkijuk.server.recruit.controller.port; import umc.kkijuk.server.recruit.domain.Recruit; -import umc.kkijuk.server.recruit.domain.RecruitCreateDto; +import umc.kkijuk.server.recruit.domain.RecruitCreate; import umc.kkijuk.server.recruit.domain.RecruitStatusUpdate; import umc.kkijuk.server.recruit.domain.RecruitUpdate; @@ -10,7 +10,7 @@ import java.util.List; public interface RecruitService { - Recruit create(RecruitCreateDto recruitCreateDto); + Recruit create(RecruitCreate recruitCreate); Recruit update(Long recruitId, RecruitUpdate recruitUpdate); diff --git a/src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java b/src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java index 1628b45f..3d2de6d6 100644 --- a/src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java +++ b/src/main/java/umc/kkijuk/server/recruit/domain/Recruit.java @@ -22,15 +22,15 @@ public class Recruit { private final boolean active; private final LocalDateTime disabledTime; - public static Recruit from(RecruitCreateDto recruitCreateDto) { + public static Recruit from(RecruitCreate recruitCreate) { return Recruit.builder() - .title(recruitCreateDto.getTitle()) - .status(recruitCreateDto.getStatus()) - .startTime(recruitCreateDto.getStartTime()) - .endTime(recruitCreateDto.getEndTime()) - .applyDate(recruitCreateDto.getApplyDate()) - .tags(recruitCreateDto.getTags() != null ? recruitCreateDto.getTags() : new ArrayList<>()) - .link(recruitCreateDto.getLink()) + .title(recruitCreate.getTitle()) + .status(recruitCreate.getStatus()) + .startTime(recruitCreate.getStartTime()) + .endTime(recruitCreate.getEndTime()) + .applyDate(recruitCreate.getApplyDate()) + .tags(recruitCreate.getTags() != null ? recruitCreate.getTags() : new ArrayList<>()) + .link(recruitCreate.getLink()) .active(true) .build(); } diff --git a/src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreateDto.java b/src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreate.java similarity index 98% rename from src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreateDto.java rename to src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreate.java index b770f9ed..f09c45dd 100644 --- a/src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreateDto.java +++ b/src/main/java/umc/kkijuk/server/recruit/domain/RecruitCreate.java @@ -14,7 +14,7 @@ @Builder @AllArgsConstructor @NoArgsConstructor(access = AccessLevel.PROTECTED) -public class RecruitCreateDto { +public class RecruitCreate { @NotBlank(message = "공고 제목은 필수 입력 항목입니다.") @Schema(description = "공고 제목", example = "[00] 공고제목", type = "string") private String title; diff --git a/src/main/java/umc/kkijuk/server/recruit/service/RecruitServiceImpl.java b/src/main/java/umc/kkijuk/server/recruit/service/RecruitServiceImpl.java index ce488705..8b41832e 100644 --- a/src/main/java/umc/kkijuk/server/recruit/service/RecruitServiceImpl.java +++ b/src/main/java/umc/kkijuk/server/recruit/service/RecruitServiceImpl.java @@ -7,7 +7,7 @@ import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; import umc.kkijuk.server.recruit.controller.port.RecruitService; import umc.kkijuk.server.recruit.domain.Recruit; -import umc.kkijuk.server.recruit.domain.RecruitCreateDto; +import umc.kkijuk.server.recruit.domain.RecruitCreate; import umc.kkijuk.server.recruit.domain.RecruitStatusUpdate; import umc.kkijuk.server.recruit.domain.RecruitUpdate; import umc.kkijuk.server.recruit.service.port.RecruitRepository; @@ -31,8 +31,8 @@ public Recruit getById(long id) { @Override @Transactional - public Recruit create(RecruitCreateDto recruitCreateDto) { - Recruit recruit = Recruit.from(recruitCreateDto); + public Recruit create(RecruitCreate recruitCreate) { + Recruit recruit = Recruit.from(recruitCreate); return recruitRepository.save(recruit); } diff --git a/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java b/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java new file mode 100644 index 00000000..2c46e877 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/controller/ReviewController.java @@ -0,0 +1,41 @@ +package umc.kkijuk.server.review.controller; + +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; +import umc.kkijuk.server.recruit.controller.port.RecruitService; +import umc.kkijuk.server.recruit.domain.Recruit; +import umc.kkijuk.server.review.controller.port.ReviewService; +import umc.kkijuk.server.review.controller.response.ReviewIdResponse; +import umc.kkijuk.server.review.domain.Review; +import umc.kkijuk.server.review.domain.ReviewCreate; + +@Tag(name = "review", description = "모집 공고 후기 API") +@RestController +@RequiredArgsConstructor +@RequestMapping("/recruit/{recruitId}") +public class ReviewController { + private final ReviewService reviewService; + private final RecruitService recruitService; + + @Operation( + summary = "지원 공고 후기 추가", + description = "주어진 지원 공고에 후기를 생성합니다") + @Parameter(name = "recruitId", description = "지원 공고 ID", example = "1") + @PostMapping("/review") + public ResponseEntity create( + @PathVariable Long recruitId, + @RequestBody @Valid ReviewCreate reviewCreate + ) { + Recruit recruit = recruitService.getById(recruitId); + Review review = reviewService.create(recruit, reviewCreate); + + 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 new file mode 100644 index 00000000..ac808038 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/controller/port/ReviewService.java @@ -0,0 +1,9 @@ +package umc.kkijuk.server.review.controller.port; + +import umc.kkijuk.server.recruit.domain.Recruit; +import umc.kkijuk.server.review.domain.Review; +import umc.kkijuk.server.review.domain.ReviewCreate; + +public interface ReviewService { + Review create(Recruit recruit, ReviewCreate reviewCreate); +} diff --git a/src/main/java/umc/kkijuk/server/review/controller/response/ReviewIdResponse.java b/src/main/java/umc/kkijuk/server/review/controller/response/ReviewIdResponse.java new file mode 100644 index 00000000..17baed60 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/controller/response/ReviewIdResponse.java @@ -0,0 +1,17 @@ +package umc.kkijuk.server.review.controller.response; + +import lombok.Builder; +import lombok.Getter; +import umc.kkijuk.server.review.domain.Review; + +@Getter +@Builder +public class ReviewIdResponse { + private Long id; + + public static ReviewIdResponse from(Review review) { + return ReviewIdResponse.builder() + .id(review.getId()) + .build(); + } +} diff --git a/src/main/java/umc/kkijuk/server/review/domain/Review.java b/src/main/java/umc/kkijuk/server/review/domain/Review.java new file mode 100644 index 00000000..b8fc38e1 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/domain/Review.java @@ -0,0 +1,26 @@ +package umc.kkijuk.server.review.domain; + +import lombok.Builder; +import lombok.Getter; +import umc.kkijuk.server.recruit.domain.Recruit; + +import java.time.LocalDate; + +@Getter +@Builder +public class Review { + private final Long id; + private final Long recruitId; + private final String title; + private final String content; + private final LocalDate date; + + public static Review from(Recruit recruit, ReviewCreate reviewCreate) { + return Review.builder() + .recruitId(recruit.getId()) + .title(reviewCreate.getTitle()) + .content(reviewCreate.getContent()) + .date(reviewCreate.getDate()) + .build(); + } +} diff --git a/src/main/java/umc/kkijuk/server/review/domain/ReviewCreate.java b/src/main/java/umc/kkijuk/server/review/domain/ReviewCreate.java new file mode 100644 index 00000000..dd8baaf7 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/domain/ReviewCreate.java @@ -0,0 +1,28 @@ +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 ReviewCreate { + + @NotBlank(message = "공고 후기 제목은 필수 입력 항목입니다.") + @Schema(description = "공고 후기 제목", example = "코딩 테스트", type = "string") + private String title; + + @Schema(description = "공고 후기 내용", example = "Dijkstra 알고리즘을 활용하는 문제", type = "string") + private String content; + + @NotNull(message = "날짜는 필수 입력 항목입니다.") + @Schema(description = "날짜", example = "2024-07-21", 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/ReviewEntity.java b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewEntity.java new file mode 100644 index 00000000..e407256f --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewEntity.java @@ -0,0 +1,44 @@ +package umc.kkijuk.server.review.infrastructure; + +import jakarta.persistence.*; +import umc.kkijuk.server.review.domain.Review; + +import java.time.LocalDate; + +@Entity +@Table(name = "review") +public class ReviewEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @Column(nullable = false) + private Long recruitId; + @Column(nullable = false) + private String title; + private String content; + @Column(nullable = false) + private LocalDate date; + + + public static ReviewEntity from(Review review) { + ReviewEntity reviewEntity = new ReviewEntity(); + reviewEntity.id = review.getId(); + reviewEntity.recruitId = review.getRecruitId(); + reviewEntity.title = review.getTitle(); + reviewEntity.content = review.getContent(); + reviewEntity.date = review.getDate(); + return reviewEntity; + } + + public Review toModel() { + return Review.builder() + .id(id) + .recruitId(recruitId) + .title(title) + .content(content) + .date(date) + .build(); + } +} diff --git a/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewJpaRepository.java b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewJpaRepository.java new file mode 100644 index 00000000..94227100 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewJpaRepository.java @@ -0,0 +1,6 @@ +package umc.kkijuk.server.review.infrastructure; + +import org.springframework.data.jpa.repository.JpaRepository; + +public interface ReviewJpaRepository extends JpaRepository { +} diff --git a/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java new file mode 100644 index 00000000..8b9b40cb --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/infrastructure/ReviewRepositoryImpl.java @@ -0,0 +1,31 @@ +package umc.kkijuk.server.review.infrastructure; + +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Repository; +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; +import umc.kkijuk.server.review.domain.Review; +import umc.kkijuk.server.review.service.port.ReviewRepository; + +import java.util.Optional; + +@Repository +@RequiredArgsConstructor +public class ReviewRepositoryImpl implements ReviewRepository { + private final ReviewJpaRepository reviewJpaRepository; + + @Override + public Review save(Review review) { + return reviewJpaRepository.save(ReviewEntity.from(review)).toModel(); + } + + @Override + public Optional findById(Long id) { + return reviewJpaRepository.findById(id).map(ReviewEntity::toModel); + } + + @Override + public Review getById(Long id) { + return findById(id) + .orElseThrow(() -> new ResourceNotFoundException("recruit", 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 new file mode 100644 index 00000000..850e1b62 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/service/ReviewServiceImpl.java @@ -0,0 +1,30 @@ +package umc.kkijuk.server.review.service; + +import lombok.Builder; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; +import umc.kkijuk.server.recruit.domain.Recruit; +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.service.port.ReviewRepository; + +@Service +@Builder +@RequiredArgsConstructor +public class ReviewServiceImpl implements ReviewService { + private final ReviewRepository reviewRepository; + + public Review getById(Long id) { + return reviewRepository.getById(id); + } + + @Override + @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); + } +} diff --git a/src/main/java/umc/kkijuk/server/review/service/port/ReviewRepository.java b/src/main/java/umc/kkijuk/server/review/service/port/ReviewRepository.java new file mode 100644 index 00000000..ac30f9c6 --- /dev/null +++ b/src/main/java/umc/kkijuk/server/review/service/port/ReviewRepository.java @@ -0,0 +1,13 @@ +package umc.kkijuk.server.review.service.port; + +import umc.kkijuk.server.review.domain.Review; + +import java.util.Optional; + +public interface ReviewRepository { + Review save(Review review); + + Optional findById(Long id); + + Review getById(Long id); +} diff --git a/src/test/java/umc/kkijuk/server/recruit/service/RecruitServiceTest.java b/src/test/java/umc/kkijuk/server/recruit/service/RecruitServiceTest.java index bfcf4253..572a6f35 100644 --- a/src/test/java/umc/kkijuk/server/recruit/service/RecruitServiceTest.java +++ b/src/test/java/umc/kkijuk/server/recruit/service/RecruitServiceTest.java @@ -1,6 +1,5 @@ package umc.kkijuk.server.recruit.service; -import org.assertj.core.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; @@ -45,7 +44,7 @@ void init() { @Test void create_새로운_recruit_만들기() { //given - RecruitCreateDto recruitCreateDto = RecruitCreateDto.builder() + RecruitCreate recruitCreate = RecruitCreate.builder() .title("dto-title") .status(RecruitStatus.PLANNED) .startTime(LocalDateTime.of(2024, 7, 19, 2, 30)) @@ -55,7 +54,7 @@ void init() { .link("https://www.dto-title.com") .build(); //when - Recruit result = recruitService.create(recruitCreateDto); + Recruit result = recruitService.create(recruitCreate); //then assertAll( @@ -73,14 +72,14 @@ void init() { @Test void create_새로운_recruit_만들기_nullable() { //given - RecruitCreateDto recruitCreateDto = RecruitCreateDto.builder() + RecruitCreate recruitCreate = RecruitCreate.builder() .title("dto-title") .status(RecruitStatus.PLANNED) .startTime(LocalDateTime.of(2024, 7, 19, 2, 30)) .endTime(LocalDateTime.of(2024, 7, 30, 2, 30)) .build(); //when - Recruit result = recruitService.create(recruitCreateDto); + Recruit result = recruitService.create(recruitCreate); //then assertAll( @@ -218,7 +217,7 @@ void init() { LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30); int times = 10; for (int i = 0; i < times; i++){ - recruitService.create(RecruitCreateDto.builder() + recruitService.create(RecruitCreate.builder() .endTime(dateTime) .build()); } @@ -236,7 +235,7 @@ void init() { LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30); int times = 10; for (int i = 0; i < times; i++){ - Recruit recruit = recruitService.create(RecruitCreateDto.builder() + Recruit recruit = recruitService.create(RecruitCreate.builder() .endTime(dateTime) .build()); recruitService.disable(recruit.getId()); @@ -255,7 +254,7 @@ void init() { LocalDateTime dateTime = LocalDateTime.of(2024, 7, 30, 2, 30); int times = 10; for (int i = 0; i < times; i++){ - Recruit recruit = recruitService.create(RecruitCreateDto.builder() + Recruit recruit = recruitService.create(RecruitCreate.builder() .endTime(dateTime) .build()); if (i % 2 == 0) diff --git a/src/test/java/umc/kkijuk/server/review/mock/FakeReviewRepository.java b/src/test/java/umc/kkijuk/server/review/mock/FakeReviewRepository.java new file mode 100644 index 00000000..9f4479a5 --- /dev/null +++ b/src/test/java/umc/kkijuk/server/review/mock/FakeReviewRepository.java @@ -0,0 +1,48 @@ +package umc.kkijuk.server.review.mock; + +import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException; +import umc.kkijuk.server.review.domain.Review; +import umc.kkijuk.server.review.service.port.ReviewRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.concurrent.atomic.AtomicLong; + +public class FakeReviewRepository implements ReviewRepository { + private final AtomicLong authGeneratedId = new AtomicLong(0); + private final List data = new ArrayList<>(); + + @Override + public Review save(Review review) { + if (review.getId() == null || review.getId() == 0) { + Review newReview = Review.builder() + .id(authGeneratedId.incrementAndGet()) + .recruitId(review.getRecruitId()) + .title(review.getTitle()) + .content(review.getContent()) + .date(review.getDate()) + .build(); + data.add(newReview); + return newReview; + } else { + data.removeIf(item -> Objects.equals(item.getId(), review.getId())); + data.add(review); + return review; + } + } + + @Override + public Optional findById(Long id) { + return data.stream() + .filter(item -> item.getId().equals(id)) + .findAny(); + } + + @Override + public Review getById(Long id) { + return findById(id) + .orElseThrow(() -> new ResourceNotFoundException("review", id)); + } +} diff --git a/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java b/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java new file mode 100644 index 00000000..8227171b --- /dev/null +++ b/src/test/java/umc/kkijuk/server/review/service/ReviewServiceTest.java @@ -0,0 +1,74 @@ +package umc.kkijuk.server.review.service; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import umc.kkijuk.server.recruit.domain.Recruit; +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.mock.FakeReviewRepository; +import umc.kkijuk.server.review.service.port.ReviewRepository; + +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class ReviewServiceTest { + + private ReviewService reviewService; + + @BeforeEach + void Init() { + ReviewRepository reviewRepository = new FakeReviewRepository(); + reviewService = ReviewServiceImpl.builder() + .reviewRepository(reviewRepository) + .build(); + } + + @Test + void crate_새로운_review_만들기() { + //given + ReviewCreate reviewCreate = ReviewCreate.builder() + .title("new-title") + .content("new-content") + .date(LocalDate.of(2024, 7, 21)) + .build(); + + Recruit recruit = Recruit.builder().id(3L).build(); + + //when + Review result = reviewService.create(recruit, reviewCreate); + + //then + assertAll( + () -> assertThat(result.getRecruitId()).isEqualTo(recruit.getId()), + () -> assertThat(result.getTitle()).isEqualTo(reviewCreate.getTitle()), + () -> assertThat(result.getContent()).isEqualTo(reviewCreate.getContent()), + () -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 21)) + ); + } + + @Test + void crate_새로운_review_만들기_nullable() { + //given + ReviewCreate reviewCreate = ReviewCreate.builder() + .title("new-title") + .date(LocalDate.of(2024, 7, 21)) + .build(); + + Recruit recruit = Recruit.builder().id(3L).build(); + + //when + Review result = reviewService.create(recruit, reviewCreate); + + //then + assertAll( + () -> assertThat(result.getRecruitId()).isEqualTo(recruit.getId()), + () -> assertThat(result.getTitle()).isEqualTo(reviewCreate.getTitle()), + () -> assertThat(result.getContent()).isNull(), + () -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 21)) + ); + } +} \ No newline at end of file