|
| 1 | +package com.cmg.comtogether.certification.service; |
| 2 | + |
| 3 | +import com.cmg.comtogether.certification.repository.CertificationRepository; |
| 4 | +import com.cmg.comtogether.certification.dto.CertificationResponseDto; |
| 5 | +import com.cmg.comtogether.certification.entity.Certification; |
| 6 | +import com.cmg.comtogether.common.exception.BusinessException; |
| 7 | +import com.cmg.comtogether.common.exception.ErrorCode; |
| 8 | +import com.cmg.comtogether.common.s3.service.S3Service; |
| 9 | +import com.cmg.comtogether.user.service.UserService; |
| 10 | +import lombok.RequiredArgsConstructor; |
| 11 | +import org.springframework.stereotype.Service; |
| 12 | +import org.springframework.transaction.annotation.Transactional; |
| 13 | + |
| 14 | +import java.time.LocalDateTime; |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@Service |
| 18 | +@RequiredArgsConstructor |
| 19 | +public class CertificationService { |
| 20 | + |
| 21 | + private final CertificationRepository certificationRepository; |
| 22 | + private final UserService userService; |
| 23 | + private final S3Service s3Service; |
| 24 | + |
| 25 | + public CertificationResponseDto createCertification(Long userId, String fileKey) { |
| 26 | + String publicUrl = s3Service.getPublicUrl(fileKey); |
| 27 | + |
| 28 | + Certification cert = Certification.builder() |
| 29 | + .userId(userId) |
| 30 | + .fileKey(fileKey) |
| 31 | + .status(Certification.Status.PENDING) |
| 32 | + .requestedAt(LocalDateTime.now()) |
| 33 | + .build(); |
| 34 | + |
| 35 | + certificationRepository.save(cert); |
| 36 | + |
| 37 | + return CertificationResponseDto.fromEntity(cert, publicUrl); |
| 38 | + } |
| 39 | + |
| 40 | + public List<CertificationResponseDto> getCertifications(Long userId) { |
| 41 | + return certificationRepository.findAllByUserIdOrderByRequestedAtDesc(userId) |
| 42 | + .stream() |
| 43 | + .map(c -> CertificationResponseDto.fromEntity( |
| 44 | + c, |
| 45 | + s3Service.getPublicUrl(c.getFileKey()) |
| 46 | + )) |
| 47 | + .toList(); |
| 48 | + } |
| 49 | + |
| 50 | + public List<CertificationResponseDto> getAllCertifications() { |
| 51 | + return certificationRepository.findAllByOrderByRequestedAtDesc() |
| 52 | + .stream() |
| 53 | + .map(c -> CertificationResponseDto.fromEntity( |
| 54 | + c, |
| 55 | + s3Service.getPublicUrl(c.getFileKey()) |
| 56 | + )) |
| 57 | + .toList(); |
| 58 | + } |
| 59 | + |
| 60 | + @Transactional |
| 61 | + public CertificationResponseDto approveCertification(Long certId) { |
| 62 | + Certification cert = certificationRepository.findById(certId) |
| 63 | + .orElseThrow(() -> new BusinessException(ErrorCode.CERTIFICATION_NOT_FOUND)); |
| 64 | + cert.approve(); |
| 65 | + userService.updateRoleToExpert(cert.getUserId()); |
| 66 | + String publicUrl = s3Service.getPublicUrl(cert.getFileKey()); |
| 67 | + |
| 68 | + return CertificationResponseDto.fromEntity(cert, publicUrl); |
| 69 | + } |
| 70 | + |
| 71 | + @Transactional |
| 72 | + public CertificationResponseDto rejectCertification(Long certId, String reason) { |
| 73 | + Certification cert = certificationRepository.findById(certId) |
| 74 | + .orElseThrow(() -> new BusinessException(ErrorCode.CERTIFICATION_NOT_FOUND)); |
| 75 | + cert.reject(reason); |
| 76 | + String publicUrl = s3Service.getPublicUrl(cert.getFileKey()); |
| 77 | + |
| 78 | + return CertificationResponseDto.fromEntity(cert, publicUrl); |
| 79 | + } |
| 80 | + |
| 81 | + @Transactional |
| 82 | + public void deleteCertification(Long certificationId, Long userId) { |
| 83 | + Certification c = certificationRepository.findById(certificationId) |
| 84 | + .orElseThrow(() -> new BusinessException(ErrorCode.CERTIFICATION_NOT_FOUND)); |
| 85 | + |
| 86 | + if (!c.getUserId().equals(userId)) { |
| 87 | + throw new BusinessException(ErrorCode.CERTIFICATION_ACCESS_DENIED); |
| 88 | + } |
| 89 | + |
| 90 | + if (c.getFileKey() != null) { |
| 91 | + s3Service.deleteObject(c.getFileKey()); |
| 92 | + } |
| 93 | + |
| 94 | + certificationRepository.delete(c); |
| 95 | + } |
| 96 | +} |
0 commit comments