Skip to content

Commit

Permalink
add unit repo, persistence busineess and validator
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Jan 21, 2025
1 parent a128711 commit 2464067
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 3 deletions.
1 change: 1 addition & 0 deletions backend/src/main/java/ch/puzzle/okr/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ private Constants() {
public static final String KEY_RESULT = "KeyResult";
public static final String CHECK_IN = "Check-in";
public static final String ACTION = "Action";
public static final String UNIT = "Unit";
public static final String ALIGNMENT = "Alignment";
public static final String COMPLETED = "Completed";
public static final String QUARTER = "Quarter";
Expand Down
3 changes: 2 additions & 1 deletion backend/src/main/java/ch/puzzle/okr/ErrorKey.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ public enum ErrorKey {
NOT_AUTHORIZED_TO_DELETE,
TOKEN_NULL,
TRIED_TO_DELETE_LAST_ADMIN,
TRIED_TO_REMOVE_LAST_OKR_CHAMPION
TRIED_TO_REMOVE_LAST_OKR_CHAMPION,
UNIT_NOT_FOUND
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import ch.puzzle.okr.models.keyresult.KeyResultMetric;
import ch.puzzle.okr.service.business.CheckInBusinessService;
import ch.puzzle.okr.service.business.ObjectiveBusinessService;
import ch.puzzle.okr.service.business.UnitBusinessService;
import ch.puzzle.okr.service.business.UserBusinessService;
import java.util.List;
import org.springframework.stereotype.Component;
Expand All @@ -21,16 +22,20 @@ public class KeyResultMetricMapper {
private final ObjectiveBusinessService objectiveBusinessService;
private final CheckInBusinessService checkInBusinessService;
private final ActionMapper actionMapper;
private final UnitBusinessService unitBusinessService;

public KeyResultMetricMapper(UserBusinessService userBusinessService,
ObjectiveBusinessService objectiveBusinessService,
CheckInBusinessService checkInBusinessService, ActionMapper actionMapper) {
CheckInBusinessService checkInBusinessService, ActionMapper actionMapper,
UnitBusinessService unitBusinessService) {
this.userBusinessService = userBusinessService;
this.objectiveBusinessService = objectiveBusinessService;
this.checkInBusinessService = checkInBusinessService;
this.actionMapper = actionMapper;
this.unitBusinessService = unitBusinessService;
}


public KeyResultDto toDto(KeyResultMetric keyResult, List<Action> actionList) {
KeyResultUserDto ownerDto = new KeyResultUserDto( //
keyResult.getOwner().getId(), //
Expand Down Expand Up @@ -73,7 +78,7 @@ public KeyResult toKeyResultMetric(KeyResultMetricDto keyResultMetricDto) {
.builder() //
.withBaseline(keyResultMetricDto.baseline()) //
.withStretchGoal(keyResultMetricDto.stretchGoal()) //
.withUnit(Unit.Builder.builder().unitName(keyResultMetricDto.unit()).build()) // TODO create unit businessservice and use find method
.withUnit(unitBusinessService.findUnitByName(keyResultMetricDto.unit()))
.withId(keyResultMetricDto.id()) //
.withVersion(keyResultMetricDto.version()) //
.withObjective(objectiveBusinessService.getEntityById(keyResultMetricDto.objective().id())) //
Expand Down
14 changes: 14 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/repository/UnitRepository.java
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);

}
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);
}
}
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);
}
}
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);
}
}

0 comments on commit 2464067

Please sign in to comment.