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 b3cacda commit 62e6af5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import umc.kkijuk.server.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitNotMatchException;
import umc.kkijuk.server.common.domian.response.ErrorResponse;

@RestControllerAdvice
Expand All @@ -17,4 +18,10 @@ public class ExceptionControllerAdvice {
public ErrorResponse resourceNotFoundException(ResourceNotFoundException exception) {
return new ErrorResponse(exception.getMessage());
}

@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(ReviewRecruitNotMatchException.class)
public ErrorResponse ReviewRecruitMatchException(ReviewRecruitNotMatchException exception) {
return new ErrorResponse(exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package umc.kkijuk.server.common.domian.exception;

public class ReviewRecruitNotMatchException extends RuntimeException{
public ReviewRecruitNotMatchException(long reviewId, long recruitId, long requestedRecruitId) {
super("Review " + reviewId + "의 Recruit ID " + recruitId + "가 요청한 " + requestedRecruitId + "와 일치하지 않습니다.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public ResponseEntity<ReviewIdResponse> create(
description = "주어진 지원 공고 후기를 수정합니다")
@Parameter(name = "recruitId", description = "지원 공고 ID", example = "1")
@Parameter(name = "reviewId", description = "지원 공고 후기 ID", example = "1")
@PostMapping("/review/{reviewId}")
@PutMapping("/review/{reviewId}")
public ResponseEntity<ReviewIdResponse> update(
@PathVariable Long recruitId,
@PathVariable Long reviewId,
Expand All @@ -58,4 +58,22 @@ public ResponseEntity<ReviewIdResponse> update(
.ok()
.body(ReviewIdResponse.from(review));
}

@Operation(
summary = "지원 공고 후기 삭제",
description = "주어진 지원 공고 후기를 삭제합니다")
@Parameter(name = "recruitId", description = "지원 공고 ID", example = "1")
@Parameter(name = "reviewId", description = "지원 공고 후기 ID", example = "1")
@DeleteMapping("/review/{reviewId}")
public ResponseEntity<Void> delete(
@PathVariable Long recruitId,
@PathVariable Long reviewId
) {
Recruit recruit = recruitService.getById(recruitId);
reviewService.delete(recruit, reviewId);

return ResponseEntity
.noContent()
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,6 @@ public interface ReviewService {
Review update(Recruit recruit, Long reviewId, ReviewUpdate reviewUpdate);

Review getById(Long reviewId);

void delete(Recruit recruit, Long reviewId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ public Review getById(Long id) {
return findById(id)
.orElseThrow(() -> new ResourceNotFoundException("review", id));
}

@Override
public void delete(Review review) {
reviewJpaRepository.delete(ReviewEntity.from(review));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitNotMatchException;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.review.controller.port.ReviewService;
import umc.kkijuk.server.review.domain.Review;
Expand Down Expand Up @@ -32,11 +33,21 @@ public Review create(Recruit recruit, ReviewCreate reviewCreate) {
@Transactional
public Review update(Recruit recruit, Long reviewId, ReviewUpdate reviewUpdate) {
Review review = getById(reviewId);
if (!review.getId().equals(recruit.getId())) {
// 잘못된 요청 처리 나중에 작성
if (!review.getRecruitId().equals(recruit.getId())) {
throw new ReviewRecruitNotMatchException(reviewId, review.getRecruitId(), review.getId());
}

review = review.update(reviewUpdate);
return reviewRepository.save(review);
}

@Override
public void delete(Recruit recruit, Long reviewId) {
Review review = getById(reviewId);
if (!review.getRecruitId().equals(recruit.getId())) {
throw new ReviewRecruitNotMatchException(reviewId, review.getRecruitId(), review.getId());
}

reviewRepository.delete(review);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface ReviewRepository {
Optional<Review> findById(Long id);

Review getById(Long id);

void delete(Review review);
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,9 @@ public Review getById(Long id) {
return findById(id)
.orElseThrow(() -> new ResourceNotFoundException("review", id));
}

@Override
public void delete(Review review) {
data.remove(review);
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
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.common.domian.exception.ResourceNotFoundException;
import umc.kkijuk.server.common.domian.exception.ReviewRecruitNotMatchException;
import umc.kkijuk.server.recruit.domain.Recruit;
import umc.kkijuk.server.review.controller.port.ReviewService;
import umc.kkijuk.server.review.domain.Review;
Expand All @@ -14,6 +15,7 @@
import java.time.LocalDate;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.*;

class ReviewServiceTest {
Expand Down Expand Up @@ -128,4 +130,53 @@ void Init() {
() -> assertThat(result.getDate()).isEqualTo(LocalDate.of(2024, 7, 30))
);
}

@Test
void delete_존재하던_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();
Review review = reviewService.create(recruit, reviewCreate);

//when
reviewService.delete(recruit, review.getId());

//then
assertThatThrownBy(
() -> reviewService.getById(review.getId())).isInstanceOf(ResourceNotFoundException.class);
}

@Test
void delete_존재하지않은_review에대한_제거요청() {
//given
Recruit recruit = Recruit.builder().id(3L).build();

//when
//then
assertThatThrownBy(
() -> reviewService.delete(recruit, 3333L)).isInstanceOf(ResourceNotFoundException.class);
}

@Test
void delete_review의_공고ID가_요청한_공고ID와_다른경우() {
//given
ReviewCreate reviewCreate = ReviewCreate.builder()
.title("new-title")
.content("new-content")
.date(LocalDate.of(2024, 7, 21))
.build();
Recruit recruit_1 = Recruit.builder().id(3L).build();
Recruit recruit_2 = Recruit.builder().id(4L).build();
Review review = reviewService.create(recruit_1, reviewCreate);

//when
//then
assertThatThrownBy(
() -> reviewService.delete(recruit_2, review.getId())).isInstanceOf(ReviewRecruitNotMatchException.class);
}
}

0 comments on commit 62e6af5

Please sign in to comment.