1
|
|
|
package com.osomapps.pt.admin.certificate; |
2
|
|
|
|
3
|
|
|
import com.osomapps.pt.ResourceNotFoundException; |
4
|
|
|
import com.osomapps.pt.UnauthorizedException; |
5
|
|
|
import java.util.HashMap; |
6
|
|
|
import java.util.List; |
7
|
|
|
import java.util.stream.Collectors; |
8
|
|
|
import org.springframework.stereotype.Service; |
9
|
|
|
import org.springframework.validation.MapBindingResult; |
10
|
|
|
|
11
|
|
|
@Service |
12
|
|
|
class AdminCertificateService { |
13
|
|
|
|
14
|
|
|
private final CertificateRepository certificateRepository; |
15
|
|
|
private final CertificateValidator certificateValidator; |
16
|
|
|
private final AmountOfDaysValidator amountOfDaysValidator; |
17
|
|
|
|
18
|
|
|
AdminCertificateService( |
19
|
|
|
CertificateRepository certificateRepository, |
20
|
|
|
CertificateValidator certificateValidator, |
21
|
|
|
AmountOfDaysValidator amountOfDaysValidator) { |
22
|
|
|
this.certificateRepository = certificateRepository; |
23
|
|
|
this.certificateValidator = certificateValidator; |
24
|
|
|
this.amountOfDaysValidator = amountOfDaysValidator; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
List<CertificateResponseDTO> findAll() { |
28
|
|
|
return certificateRepository.findAll().stream() |
29
|
|
|
.map(AdminCertificateService::certifivateToDto) |
30
|
|
|
.collect(Collectors.toList()); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
private static CertificateResponseDTO certifivateToDto(Certificate certificate) { |
34
|
|
|
return CertificateResponseDTO.builder() |
35
|
|
|
.id(certificate.getId()) |
36
|
|
|
.code(certificate.getCode()) |
37
|
|
|
.amountOfDays(certificate.getAmount_of_days()) |
38
|
|
|
.activated(certificate.getActivated()) |
39
|
|
|
.build(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
CertificateResponseDTO findOne(Long id) { |
43
|
|
|
final Certificate certificate = certificateRepository.findById(id).orElse(null); |
44
|
|
|
if (certificate == null) { |
45
|
|
|
throw new ResourceNotFoundException("Certificate not found in database: " + id); |
46
|
|
|
} |
47
|
|
|
return certifivateToDto(certificate); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
CertificateResponseDTO create(CertificateRequestDTO certificateRequestDTO) { |
51
|
|
|
final MapBindingResult certificateErrors = |
52
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
53
|
|
|
certificateValidator.validate(certificateRequestDTO.getCode(), certificateErrors); |
54
|
|
|
if (certificateErrors.hasErrors()) { |
55
|
|
|
throw new UnauthorizedException( |
56
|
|
|
certificateErrors.getAllErrors().get(0).getDefaultMessage()); |
57
|
|
|
} |
58
|
|
|
final MapBindingResult amountOfDaysErrors = |
59
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
60
|
|
|
amountOfDaysValidator.validate(certificateRequestDTO.getAmountOfDays(), amountOfDaysErrors); |
61
|
|
|
if (amountOfDaysErrors.hasErrors()) { |
62
|
|
|
throw new UnauthorizedException( |
63
|
|
|
amountOfDaysErrors.getAllErrors().get(0).getDefaultMessage()); |
64
|
|
|
} |
65
|
|
|
final Certificate certificate = new Certificate(); |
66
|
|
|
certificate.setCode(certificateRequestDTO.getCode()); |
67
|
|
|
certificate.setAmount_of_days(certificateRequestDTO.getAmountOfDays()); |
68
|
|
|
certificate.setActivated(Boolean.FALSE); |
69
|
|
|
return certifivateToDto(certificateRepository.save(certificate)); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
CertificateResponseDTO update(Long id, CertificateRequestDTO certificateRequestDTO) { |
73
|
|
|
final Certificate certificate = certificateRepository.findById(id).orElse(null); |
74
|
|
|
if (certificate == null) { |
75
|
|
|
throw new ResourceNotFoundException("Certificate not found in database: " + id); |
76
|
|
|
} |
77
|
|
|
final MapBindingResult certificateErrors = |
78
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
79
|
|
|
certificateValidator.validate(certificateRequestDTO.getCode(), certificateErrors); |
80
|
|
|
if (certificateErrors.hasErrors()) { |
81
|
|
|
throw new UnauthorizedException( |
82
|
|
|
certificateErrors.getAllErrors().get(0).getDefaultMessage()); |
83
|
|
|
} |
84
|
|
|
final MapBindingResult amountOfDaysErrors = |
85
|
|
|
new MapBindingResult(new HashMap<>(), String.class.getName()); |
86
|
|
|
amountOfDaysValidator.validate(certificateRequestDTO.getAmountOfDays(), amountOfDaysErrors); |
87
|
|
|
if (amountOfDaysErrors.hasErrors()) { |
88
|
|
|
throw new UnauthorizedException( |
89
|
|
|
amountOfDaysErrors.getAllErrors().get(0).getDefaultMessage()); |
90
|
|
|
} |
91
|
|
|
certificate.setCode(certificateRequestDTO.getCode()); |
92
|
|
|
certificate.setAmount_of_days(certificateRequestDTO.getAmountOfDays()); |
93
|
|
|
return certifivateToDto(certificateRepository.save(certificate)); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
CertificateResponseDTO delete(Long id) { |
97
|
|
|
final Certificate certificate = certificateRepository.findById(id).orElse(null); |
98
|
|
|
if (certificate == null) { |
99
|
|
|
throw new ResourceNotFoundException("Certificate not found in database: " + id); |
100
|
|
|
} |
101
|
|
|
final CertificateResponseDTO certificateResponseDTO = certifivateToDto(certificate); |
102
|
|
|
certificateRepository.delete(certificate); |
103
|
|
|
return certificateResponseDTO; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|