-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add unit repo, persistence busineess and validator
- Loading branch information
1 parent
a128711
commit 2464067
Showing
7 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
backend/src/main/java/ch/puzzle/okr/repository/UnitRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ch.puzzle.okr.repository; | ||
|
||
import ch.puzzle.okr.models.Unit; | ||
import org.springframework.data.repository.CrudRepository; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.util.Optional; | ||
|
||
@Repository | ||
public interface UnitRepository extends CrudRepository<Unit, Long> { | ||
|
||
Optional<Unit> findUnitByUnitName(String name); | ||
|
||
} |
64 changes: 64 additions & 0 deletions
64
backend/src/main/java/ch/puzzle/okr/service/business/UnitBusinessService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package ch.puzzle.okr.service.business; | ||
|
||
import ch.puzzle.okr.ErrorKey; | ||
import ch.puzzle.okr.exception.OkrResponseStatusException; | ||
import ch.puzzle.okr.models.Action; | ||
import ch.puzzle.okr.models.Unit; | ||
import ch.puzzle.okr.models.keyresult.KeyResult; | ||
import ch.puzzle.okr.service.persistence.ActionPersistenceService; | ||
import ch.puzzle.okr.service.persistence.UnitPersistenceService; | ||
import ch.puzzle.okr.service.validation.ActionValidationService; | ||
import ch.puzzle.okr.service.validation.UnitValidationService; | ||
import jakarta.transaction.Transactional; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
@Service | ||
public class UnitBusinessService { | ||
private final UnitPersistenceService unitPersistenceService; | ||
private final UnitValidationService validator; | ||
|
||
public UnitBusinessService(UnitPersistenceService unitPersistenceService, UnitValidationService validator) { | ||
this.unitPersistenceService = unitPersistenceService; | ||
this.validator = validator; | ||
} | ||
|
||
public Unit findUnitByName(String unitName) { | ||
return unitPersistenceService.findUnitByUnitName(unitName).orElseThrow(() -> OkrResponseStatusException.of(ErrorKey.UNIT_NOT_FOUND, unitName)); | ||
} | ||
|
||
public Unit getEntityById(Long id) { | ||
validator.validateOnGet(id); | ||
return unitPersistenceService.findById(id); | ||
} | ||
|
||
|
||
@Transactional | ||
public Unit createEntity(Unit action) { | ||
validator.validateOnCreate(action); | ||
return unitPersistenceService.save(action); | ||
} | ||
|
||
@Transactional | ||
public List<Unit> updateEntities(List<Unit> actionList) { | ||
List<Unit> savedActions = new ArrayList<>(); | ||
|
||
return savedActions; | ||
} | ||
|
||
@Transactional | ||
public Unit updateEntity(Long id, Unit action) { | ||
validator.validateOnUpdate(id, action); | ||
return unitPersistenceService.save(action); | ||
} | ||
|
||
@Transactional | ||
public void deleteEntityById(Long id) { | ||
validator.validateOnDelete(id); | ||
unitPersistenceService.deleteById(id); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
backend/src/main/java/ch/puzzle/okr/service/persistence/UnitPersistenceService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ch.puzzle.okr.service.persistence; | ||
|
||
import ch.puzzle.okr.models.Action; | ||
import ch.puzzle.okr.models.Unit; | ||
import ch.puzzle.okr.repository.ActionRepository; | ||
import ch.puzzle.okr.repository.UnitRepository; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.List; | ||
import java.util.Optional; | ||
|
||
import static ch.puzzle.okr.Constants.ACTION; | ||
import static ch.puzzle.okr.Constants.UNIT; | ||
|
||
@Service | ||
public class UnitPersistenceService extends PersistenceBase<Unit, Long, UnitRepository> { | ||
|
||
protected UnitPersistenceService(UnitRepository repository) { | ||
super(repository); | ||
} | ||
|
||
@Override | ||
public String getModelName() { | ||
return UNIT; | ||
} | ||
|
||
public Optional<Unit> findUnitByUnitName(String unitName) { | ||
return getRepository().findUnitByUnitName(unitName); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
backend/src/main/java/ch/puzzle/okr/service/validation/UnitValidationService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ch.puzzle.okr.service.validation; | ||
|
||
import ch.puzzle.okr.models.Unit; | ||
import ch.puzzle.okr.repository.UnitRepository; | ||
import ch.puzzle.okr.service.persistence.UnitPersistenceService; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service | ||
public class UnitValidationService extends ValidationBase<Unit, Long, UnitRepository, UnitPersistenceService> { | ||
|
||
private final KeyResultValidationService keyResultValidationService; | ||
|
||
public UnitValidationService(UnitPersistenceService unitPersistenceService, | ||
KeyResultValidationService keyResultValidationService) { | ||
super(unitPersistenceService); | ||
this.keyResultValidationService = keyResultValidationService; | ||
} | ||
|
||
public void validateOnGetByKeyResultId(Long keyResultId) { | ||
keyResultValidationService.validateOnGet(keyResultId); | ||
} | ||
|
||
@Override | ||
public void validateOnCreate(Unit model) { | ||
throwExceptionWhenModelIsNull(model); | ||
throwExceptionWhenIdIsNotNull(model.getId()); | ||
|
||
validate(model); | ||
} | ||
|
||
@Override | ||
public void validateOnUpdate(Long id, Unit model) { | ||
throwExceptionWhenModelIsNull(model); | ||
throwExceptionWhenIdIsNull(model.getId()); | ||
throwExceptionWhenIdHasChanged(id, model.getId()); | ||
validate(model); | ||
} | ||
} |