Skip to content

Commit b3a270e

Browse files
committed
feat: not finished servies and add name dto #38
1 parent f32e9a1 commit b3a270e

6 files changed

Lines changed: 141 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.degreeType;
2+
3+
public record DegreeTypeNameDto(Long id, String name) {
4+
}

backend/src/main/java/ch/puzzle/pcts/model/error/ErrorKey.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ public enum ErrorKey {
88
NOT_FOUND,
99
ROLE_NAME_IS_NULL,
1010
ROLE_NAME_IS_EMPTY,
11+
DEGREE_TYPE_NAME_IS_NULL,
12+
DEGREE_TYPE_NAME_IS_EMPTY,
1113
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
package ch.puzzle.pcts.repository;
22

3+
import ch.puzzle.pcts.dto.degreeType.DegreeTypeNameDto;
34
import ch.puzzle.pcts.model.degreeType.DegreeType;
45
import org.springframework.data.jpa.repository.JpaRepository;
6+
import org.springframework.data.jpa.repository.Query;
57
import org.springframework.stereotype.Repository;
68

9+
import java.util.List;
10+
711
@Repository
812
public interface DegreeTypeRepository extends JpaRepository<DegreeType, Long> {
13+
14+
@Query("SELECT id, name FROM DegreeType")
15+
List<DegreeTypeNameDto> findAllNames();
916
}

backend/src/main/java/ch/puzzle/pcts/service/business/DegreeTypeBusinessService.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package ch.puzzle.pcts.service.business;
22

3+
import ch.puzzle.pcts.dto.degreeType.DegreeTypeNameDto;
4+
import ch.puzzle.pcts.exception.PCTSException;
5+
import ch.puzzle.pcts.model.degreeType.DegreeType;
6+
import ch.puzzle.pcts.model.error.ErrorKey;
37
import ch.puzzle.pcts.service.persistence.DegreeTypePersistenceService;
48
import ch.puzzle.pcts.service.validation.DegreeTypeValidationService;
9+
import java.util.List;
10+
import org.springframework.http.HttpStatus;
511
import org.springframework.stereotype.Service;
612

713
@Service
@@ -14,4 +20,36 @@ public DegreeTypeBusinessService(DegreeTypeValidationService validationService,
1420
this.validationService = validationService;
1521
this.persistenceService = persistenceService;
1622
}
23+
24+
public List<DegreeType> getAll() {
25+
return persistenceService.getAll();
26+
}
27+
28+
public DegreeType create(DegreeType degreeType) {
29+
validationService.validateOnCreate(degreeType);
30+
return persistenceService.create(degreeType);
31+
}
32+
33+
public List<DegreeTypeNameDto> getAllNames() {
34+
return persistenceService.getAllNames();
35+
}
36+
37+
public DegreeType getById(Long id) {
38+
validationService.validateOnGetById(id);
39+
return persistenceService
40+
.getById(id)
41+
.orElseThrow(() -> new PCTSException(HttpStatus.NOT_FOUND,
42+
"Degree type with id: " + id + " does not exist.",
43+
ErrorKey.NOT_FOUND));
44+
}
45+
46+
public DegreeType update(Long id, DegreeType degreeType) {
47+
validationService.validateOnUpdate(id, degreeType);
48+
return persistenceService.update(id, degreeType);
49+
}
50+
51+
public void delete(Long id) {
52+
validationService.validateOnDelete(id);
53+
persistenceService.delete(id);
54+
}
1755
}

backend/src/main/java/ch/puzzle/pcts/service/persistence/DegreeTypePersistenceService.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package ch.puzzle.pcts.service.persistence;
22

3+
import ch.puzzle.pcts.dto.degreeType.DegreeTypeNameDto;
4+
import ch.puzzle.pcts.model.degreeType.DegreeType;
35
import ch.puzzle.pcts.repository.DegreeTypeRepository;
6+
import java.util.List;
7+
import java.util.Optional;
48
import org.springframework.beans.factory.annotation.Autowired;
59
import org.springframework.stereotype.Component;
610

@@ -12,4 +16,29 @@ public class DegreeTypePersistenceService {
1216
public DegreeTypePersistenceService(DegreeTypeRepository repository) {
1317
this.repository = repository;
1418
}
19+
20+
public List<DegreeType> getAll() {
21+
return repository.findAll();
22+
}
23+
24+
public DegreeType create(DegreeType degreeType) {
25+
return repository.save(degreeType);
26+
}
27+
28+
public Optional<DegreeType> getById(Long id) {
29+
return repository.findById(id);
30+
}
31+
32+
public DegreeType update(Long id, DegreeType degreeType) {
33+
degreeType.setId(id);
34+
return repository.save(degreeType);
35+
}
36+
37+
public void delete(Long id) {
38+
repository.deleteById(id);
39+
}
40+
41+
public List<DegreeTypeNameDto> getAllNames() {
42+
return repository.findAllNames();
43+
}
1544
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,68 @@
11
package ch.puzzle.pcts.service.validation;
22

3+
import ch.puzzle.pcts.exception.PCTSException;
4+
import ch.puzzle.pcts.model.degreeType.DegreeType;
5+
import ch.puzzle.pcts.model.error.ErrorKey;
6+
import ch.puzzle.pcts.service.persistence.DegreeTypePersistenceService;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.http.HttpStatus;
39
import org.springframework.stereotype.Component;
410

11+
import java.math.BigDecimal;
12+
513
@Component
614
public class DegreeTypeValidationService {
15+
private final DegreeTypePersistenceService persistenceService;
16+
17+
@Autowired
18+
public DegreeTypeValidationService(DegreeTypePersistenceService persistenceService) {
19+
this.persistenceService = persistenceService;
20+
}
21+
22+
public void validateOnCreate(DegreeType degreeType) {
23+
validateIfIdIsNull(degreeType.getId());
24+
validateName(degreeType.getName());
25+
}
26+
27+
public void validateOnGetById(Long id) {
28+
validateIfExists(id);
29+
}
30+
31+
public void validateOnUpdate(Long id, DegreeType degreeType) {
32+
validateIfExists(id);
33+
validateIfIdIsNull(degreeType.getId());
34+
validateName(degreeType.getName());
35+
}
36+
37+
public void validateOnDelete(Long id) {
38+
validateIfExists(id);
39+
}
40+
41+
private void validateIfExists(long id) {
42+
persistenceService
43+
.getById(id)
44+
.orElseThrow(() -> new PCTSException(HttpStatus.NOT_FOUND,
45+
"Degree type with id: " + id + " does not exist.",
46+
ErrorKey.NOT_FOUND));
47+
}
48+
49+
private void validateIfIdIsNull(Long id) {
50+
if (id != null) {
51+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Id needs to be undefined", ErrorKey.ID_IS_NOT_NULL);
52+
}
53+
}
54+
55+
private void validateName(String name) {
56+
if (name == null) {
57+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be null", ErrorKey.DEGREE_TYPE_NAME_IS_NULL);
58+
}
59+
60+
if (name.isBlank()) {
61+
throw new PCTSException(HttpStatus.BAD_REQUEST, "Name must not be empty", ErrorKey.DEGREE_TYPE_NAME_IS_EMPTY);
62+
}
63+
}
64+
65+
private void validateRelevantPoints(BigDecimal highlyRelevantPoints, BigDecimal limitedRelevantPoints, BigDecimal littleRelevantPoints) {
66+
if ()
67+
}
768
}

0 commit comments

Comments
 (0)