|
| 1 | +package com.somemore.review.service; |
| 2 | + |
| 3 | +import static com.somemore.global.exception.ExceptionMessage.REVIEW_ALREADY_EXISTS; |
| 4 | +import static com.somemore.global.exception.ExceptionMessage.REVIEW_RESTRICTED_TO_ATTENDED; |
| 5 | + |
| 6 | +import com.somemore.global.exception.BadRequestException; |
| 7 | +import com.somemore.review.domain.Review; |
| 8 | +import com.somemore.review.dto.request.ReviewCreateRequestDto; |
| 9 | +import com.somemore.review.repository.ReviewRepository; |
| 10 | +import com.somemore.review.usecase.CreateReviewUseCase; |
| 11 | +import com.somemore.volunteerapply.domain.VolunteerApply; |
| 12 | +import com.somemore.volunteerapply.usecase.VolunteerApplyQueryUseCase; |
| 13 | +import java.util.UUID; |
| 14 | +import lombok.RequiredArgsConstructor; |
| 15 | +import org.springframework.stereotype.Service; |
| 16 | +import org.springframework.transaction.annotation.Transactional; |
| 17 | + |
| 18 | +@RequiredArgsConstructor |
| 19 | +@Transactional |
| 20 | +@Service |
| 21 | +public class CreateReviewService implements CreateReviewUseCase { |
| 22 | + |
| 23 | + private final ReviewRepository reviewRepository; |
| 24 | + private final VolunteerApplyQueryUseCase volunteerApplyQueryUseCase; |
| 25 | + |
| 26 | + @Override |
| 27 | + public Long createReview(ReviewCreateRequestDto requestDto, UUID volunteerId, String imgUrl) { |
| 28 | + VolunteerApply apply = getVolunteerApply(requestDto.recruitBoardId(), volunteerId); |
| 29 | + validateReviewNotExist(apply); |
| 30 | + validateActivityCompletion(apply); |
| 31 | + |
| 32 | + Review review = requestDto.toEntity(apply, volunteerId, imgUrl); |
| 33 | + return reviewRepository.save(review).getId(); |
| 34 | + } |
| 35 | + |
| 36 | + private VolunteerApply getVolunteerApply(Long recruitBoardId, UUID volunteerId) { |
| 37 | + return volunteerApplyQueryUseCase.getByRecruitIdAndVolunteerId(recruitBoardId, volunteerId); |
| 38 | + } |
| 39 | + |
| 40 | + private void validateReviewNotExist(VolunteerApply apply) { |
| 41 | + if (reviewRepository.existsByVolunteerApplyId(apply.getId())) { |
| 42 | + throw new BadRequestException(REVIEW_ALREADY_EXISTS.getMessage()); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private void validateActivityCompletion(VolunteerApply apply) { |
| 47 | + if (apply.isVolunteerActivityCompleted()) { |
| 48 | + return; |
| 49 | + } |
| 50 | + throw new BadRequestException(REVIEW_RESTRICTED_TO_ATTENDED.getMessage()); |
| 51 | + } |
| 52 | +} |
0 commit comments