|
| 1 | +package com.somemore.volunteerapply.service; |
| 2 | + |
| 3 | +import static com.somemore.global.exception.ExceptionMessage.NOT_EXISTS_VOLUNTEER_APPLY; |
| 4 | +import static com.somemore.global.exception.ExceptionMessage.UNAUTHORIZED_VOLUNTEER_APPLY; |
| 5 | + |
| 6 | +import com.somemore.global.exception.BadRequestException; |
| 7 | +import com.somemore.volunteerapply.domain.VolunteerApply; |
| 8 | +import com.somemore.volunteerapply.repository.VolunteerApplyRepository; |
| 9 | +import com.somemore.volunteerapply.usecase.WithdrawVolunteerApplyUseCase; |
| 10 | +import java.util.UUID; |
| 11 | +import lombok.RequiredArgsConstructor; |
| 12 | +import org.springframework.stereotype.Service; |
| 13 | +import org.springframework.transaction.annotation.Transactional; |
| 14 | + |
| 15 | +@RequiredArgsConstructor |
| 16 | +@Transactional |
| 17 | +@Service |
| 18 | +public class WithdrawVolunteerApplyService implements WithdrawVolunteerApplyUseCase { |
| 19 | + |
| 20 | + private final VolunteerApplyRepository volunteerApplyRepository; |
| 21 | + |
| 22 | + @Override |
| 23 | + public void withdraw(Long id, UUID volunteerId) { |
| 24 | + VolunteerApply apply = getApply(id); |
| 25 | + validateVolunteerIdentity(apply, volunteerId); |
| 26 | + |
| 27 | + apply.markAsDeleted(); |
| 28 | + volunteerApplyRepository.save(apply); |
| 29 | + } |
| 30 | + |
| 31 | + private VolunteerApply getApply(Long id) { |
| 32 | + return volunteerApplyRepository.findById(id).orElseThrow( |
| 33 | + () -> new BadRequestException(NOT_EXISTS_VOLUNTEER_APPLY) |
| 34 | + ); |
| 35 | + } |
| 36 | + |
| 37 | + private void validateVolunteerIdentity(VolunteerApply apply, UUID volunteerId) { |
| 38 | + if (apply.isOwnApplication(volunteerId)) { |
| 39 | + return; |
| 40 | + } |
| 41 | + |
| 42 | + throw new BadRequestException(UNAUTHORIZED_VOLUNTEER_APPLY); |
| 43 | + } |
| 44 | +} |
0 commit comments