Skip to content

Commit 09f1cd7

Browse files
committed
feat: Add model and services #36
1 parent 92bcf57 commit 09f1cd7

7 files changed

Lines changed: 236 additions & 0 deletions

File tree

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package ch.puzzle.pcts.dto.certificate;
2+
3+
public record CertificateDto(Long id, String name, double points, String comment) {
4+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package ch.puzzle.pcts.mapper;
2+
3+
import ch.puzzle.pcts.dto.certificate.CertificateDto;
4+
import ch.puzzle.pcts.model.certificate.Certificate;
5+
import java.util.List;
6+
import org.springframework.stereotype.Component;
7+
8+
@Component
9+
public class CertificateMapper {
10+
11+
public List<CertificateDto> toDto(List<Certificate> models) {
12+
return models.stream().map(this::toDto).toList();
13+
}
14+
15+
public List<Certificate> fromDto(List<CertificateDto> dtos) {
16+
return dtos.stream().map(this::fromDto).toList();
17+
}
18+
19+
public CertificateDto toDto(Certificate model) {
20+
return new CertificateDto(model.getId(), model.getName(), model.getPoints(), model.getComment());
21+
}
22+
23+
public Certificate fromDto(CertificateDto dto) {
24+
return new Certificate(dto.id(), dto.name(), dto.points(), false, dto.comment());
25+
}
26+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package ch.puzzle.pcts.model.certificate;
2+
3+
import jakarta.persistence.*;
4+
5+
@Entity
6+
public class Certificate {
7+
@Id
8+
@GeneratedValue(strategy = GenerationType.IDENTITY)
9+
private Long id;
10+
11+
private String name;
12+
13+
@Column(nullable = false)
14+
private double points;
15+
16+
@Column(nullable = false)
17+
private boolean is_deleted;
18+
19+
private String comment;
20+
21+
public Certificate(long id, String name, double points, boolean is_deleted, String comment) {
22+
this.id = id;
23+
this.name = name;
24+
this.points = points;
25+
this.is_deleted = is_deleted;
26+
this.comment = comment;
27+
}
28+
29+
public Certificate() {
30+
}
31+
32+
public Long getId() {
33+
return id;
34+
}
35+
36+
public void setId(Long id) {
37+
this.id = id;
38+
}
39+
40+
public String getName() {
41+
return name;
42+
}
43+
44+
public void setName(String name) {
45+
this.name = name;
46+
}
47+
48+
public double getPoints() {
49+
return points;
50+
}
51+
52+
public void setPoints(double points) {
53+
this.points = points;
54+
}
55+
56+
public boolean isDeleted() {
57+
return is_deleted;
58+
}
59+
60+
public void setDeleted(boolean deleted) {
61+
is_deleted = deleted;
62+
}
63+
64+
public String getComment() {
65+
return comment;
66+
}
67+
68+
public void setComment(String comment) {
69+
this.comment = comment;
70+
}
71+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package ch.puzzle.pcts.repository;
2+
3+
import ch.puzzle.pcts.model.certificate.Certificate;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.stereotype.Repository;
6+
7+
@Repository
8+
public interface CertificateRepository extends JpaRepository<Certificate, Long> {
9+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package ch.puzzle.pcts.service.business;
2+
3+
import ch.puzzle.pcts.model.certificate.Certificate;
4+
import ch.puzzle.pcts.service.persistence.CertificatePersistenceService;
5+
import ch.puzzle.pcts.service.validation.CertificateValidationService;
6+
import java.util.List;
7+
import org.springframework.stereotype.Service;
8+
9+
@Service
10+
public class CertificateBusinessService {
11+
private final CertificateValidationService validationService;
12+
private final CertificatePersistenceService persistenceService;
13+
14+
public CertificateBusinessService(CertificateValidationService certificateValidationService,
15+
CertificatePersistenceService certificatePersistenceService) {
16+
this.validationService = certificateValidationService;
17+
this.persistenceService = certificatePersistenceService;
18+
}
19+
20+
public List<Certificate> getAll() {
21+
22+
}
23+
24+
public Certificate getById(long id) {
25+
validationService.validateOnGetById(id);
26+
}
27+
28+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package ch.puzzle.pcts.service.persistence;
2+
3+
import ch.puzzle.pcts.model.certificate.Certificate;
4+
import ch.puzzle.pcts.repository.CertificateRepository;
5+
import java.util.List;
6+
import java.util.Optional;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.stereotype.Component;
9+
10+
@Component
11+
public class CertificatePersistenceService {
12+
13+
private final CertificateRepository repository;
14+
15+
@Autowired
16+
public CertificatePersistenceService(CertificateRepository certificateRepository) {
17+
this.repository = certificateRepository;
18+
}
19+
20+
public Certificate create(Certificate certificate) {
21+
return repository.save(certificate);
22+
}
23+
24+
public Optional<Certificate> getById(Long id) {
25+
return repository.findById(id);
26+
}
27+
28+
public List<Certificate> getAll() {
29+
return repository.findAll();
30+
}
31+
32+
public Certificate update(Long id, Certificate certificate) {
33+
certificate.setId(id);
34+
return repository.save(certificate);
35+
}
36+
37+
public void delete(Long id) {
38+
repository.deleteById(id);
39+
}
40+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package ch.puzzle.pcts.service.validation;
2+
3+
import ch.puzzle.pcts.exception.PCTSException;
4+
import ch.puzzle.pcts.model.certificate.Certificate;
5+
import ch.puzzle.pcts.model.error.ErrorKey;
6+
import ch.puzzle.pcts.service.persistence.CertificatePersistenceService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.stereotype.Component;
10+
11+
@Component
12+
public class CertificateValidationService {
13+
private final CertificatePersistenceService persistenceService;
14+
15+
@Autowired
16+
public CertificateValidationService(CertificatePersistenceService persistenceService) {
17+
this.persistenceService = persistenceService;
18+
}
19+
20+
public void validateOnCreate(Certificate certificate) {
21+
22+
}
23+
24+
public void validateOnGetById(Long id) {
25+
26+
}
27+
28+
public void validateOnUpdate(Certificate certificate) {
29+
30+
}
31+
32+
public void validateOnDelete(Long id) {
33+
}
34+
35+
private void validateIfIdIsNull(Long id) {
36+
if (id != null) {
37+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Id needs to be undefined", ErrorKey.ID_IS_NOT_NULL);
38+
}
39+
}
40+
41+
private void validateName(String name) {
42+
if (name == null) {
43+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be null", ErrorKey.ROLE_NAME_IS_NULL);
44+
}
45+
46+
if (name.isBlank()) {
47+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be empty", ErrorKey.ROLE_NAME_IS_EMPTY);
48+
}
49+
}
50+
51+
private void validateIfExists(long id) {
52+
persistenceService
53+
.getById(id)
54+
.orElseThrow(() -> new PCTSException(HttpStatus.NOT_FOUND,
55+
"Role with id: " + id + " does not exist.",
56+
ErrorKey.NOT_FOUND));
57+
}
58+
}

0 commit comments

Comments
 (0)