Skip to content

Commit

Permalink
find all should always return the visible entities
Browse files Browse the repository at this point in the history
  • Loading branch information
kcinay055679 committed Feb 11, 2025
1 parent e442d4a commit 2804d75
Show file tree
Hide file tree
Showing 20 changed files with 66 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import java.util.List;

import ch.puzzle.okr.models.Objective;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

public interface ActionRepository extends DeleteRepository<Action, Long> {
List<Action> getActionsByKeyResultIdOrderByPriorityAsc(Long keyResultId);
// @Query("select Action from Action a where ")
// TODO rename this
List<Action> getActionsByKeyResultIdAndIsDeletedFalseOrderByPriorityAsc(Long keyResultId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

import ch.puzzle.okr.models.checkin.CheckIn;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface CheckInRepository extends DeleteRepository<CheckIn, Long> {
List<CheckIn> findCheckInsByKeyResultIdOrderByCreatedOnDesc(Long keyResultId);
List<CheckIn> findCheckInsByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(Long keyResultId);

CheckIn findFirstByKeyResultIdOrderByCreatedOnDesc(Long keyResultId);
CheckIn findFirstByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(Long keyResultId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import java.util.List;

@NoRepositoryBean
public interface DeleteRepository<E extends Deletable, T> extends CrudRepository<E, T> {
public interface DeleteRepository<E extends Deletable, I> extends CrudRepository<E, I> {
// @Query("select #{#entityName} e set e.isDeleted = true where e.id = :id")
// List<E> isDeleted(boolean isDeleted);

Expand All @@ -19,5 +19,5 @@ public interface DeleteRepository<E extends Deletable, T> extends CrudRepository

@Modifying
@Query("update #{#entityName} e set e.isDeleted = true where e.id = :id")
void markAsDeleted(@Param("id") T id);
void markAsDeleted(@Param("id") I id);
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
package ch.puzzle.okr.repository;

import ch.puzzle.okr.models.Objective;
import ch.puzzle.okr.models.keyresult.KeyResult;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface KeyResultRepository extends DeleteRepository<KeyResult, Long> {
List<KeyResult> findByObjectiveId(Long objectiveId);
List<KeyResult> findByObjectiveIdAndIsDeletedFalse(Long objectiveId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Repository
public interface ObjectiveRepository extends DeleteRepository<Objective, Long> {

Integer countByTeamAndQuarter(Team team, Quarter quarter);
Integer countByTeamAndQuarterAndIsDeletedFalse(Team team, Quarter quarter);

List<Objective> findObjectivesByTeamId(Long id);
List<Objective> findObjectivesByTeamIdAndIsDeletedFalse(Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@

import ch.puzzle.okr.models.Team;
import java.util.List;
import org.springframework.data.repository.CrudRepository;

public interface TeamRepository extends DeleteRepository<Team, Long> {

List<Team> findTeamsByName(String name);
List<Team> findTeamsByNameAndIsDeletedFalse(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
@Repository
public interface UnitRepository extends DeleteRepository<Unit, Long> {

Optional<Unit> findUnitByUnitName(String name);
Optional<Unit> findUnitByUnitNameAndIsDeletedFalse(String name);

List<Unit> findAllByCreatedById(Long userId);
List<Unit> findAllByCreatedByIdAndIsDeletedFalse(Long userId);

boolean existsUnitByUnitName(String unitName);
boolean existsUnitByUnitNameAndIsDeletedFalse(String unitName);

boolean existsUnitByUnitNameAndIdNot(String unitName, Long id);
boolean existsUnitByUnitNameAndIdNotAndIsDeletedFalse(String unitName, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends DeleteRepository<User, Long> {
Optional<User> findByEmail(String email);
Optional<User> findByEmailAndIsDeletedFalse(String email);

List<User> findByOkrChampion(boolean isOkrChampion);
List<User> findByOkrChampionAndIsDeletedFalse(boolean isOkrChampion);
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public String getModelName() {
}

public List<Action> getActionsByKeyResultIdOrderByPriorityAsc(Long keyResultId) {
return getRepository().getActionsByKeyResultIdOrderByPriorityAsc(keyResultId);
return getRepository().getActionsByKeyResultIdAndIsDeletedFalseOrderByPriorityAsc(keyResultId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ public String getModelName() {
}

public List<CheckIn> getCheckInsByKeyResultIdOrderByCheckInDateDesc(Long keyResultId) {
return getRepository().findCheckInsByKeyResultIdOrderByCreatedOnDesc(keyResultId);
return getRepository().findCheckInsByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(keyResultId);
}

public CheckIn getLastCheckInOfKeyResult(Long keyResultId) {
return getRepository().findFirstByKeyResultIdOrderByCreatedOnDesc(keyResultId);
return getRepository().findFirstByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(keyResultId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public String getModelName() {
}

public List<KeyResult> getKeyResultsByObjective(Long objectiveId) {
return getRepository().findByObjectiveId(objectiveId);
return getRepository().findByObjectiveIdAndIsDeletedFalse(objectiveId);
}

@Transactional
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public String getModelName() {
* @return number of Objectives of team in quarter
*/
public Integer countByTeamAndQuarter(Team team, Quarter quarter) {
return getRepository().countByTeamAndQuarter(team, quarter);
return getRepository().countByTeamAndQuarterAndIsDeletedFalse(team, quarter);
}

public Objective findObjectiveById(Long objectiveId, AuthorizationUser authorizationUser,
Expand All @@ -62,7 +62,7 @@ public Objective findObjectiveById(Long objectiveId, AuthorizationUser authoriza
}

public List<Objective> findObjectiveByTeamId(Long teamId) {
return getRepository().findObjectivesByTeamId(teamId);
return getRepository().findObjectivesByTeamIdAndIsDeletedFalse(teamId);
}

public Objective findObjectiveByKeyResultId(Long keyResultId, AuthorizationUser authorizationUser,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public T save(T model) throws OkrResponseStatusException {
}

public List<T> findAll() {
return iteratorToList(repository.findAll());
return iteratorToList(this.deleteMethod.findAll());
}

public void deleteById(I id) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ public String getModelName() {
}

public List<Team> findTeamsByName(String name) {
return getRepository().findTeamsByName(name);
return getRepository().findTeamsByNameAndIsDeletedFalse(name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,21 @@ public String getModelName() {

public Unit findUnitByUnitName(String unitName) {
return getRepository()
.findUnitByUnitName(unitName)
.findUnitByUnitNameAndIsDeletedFalse(unitName)
.orElseThrow(() -> new OkrResponseStatusException(HttpStatus.NOT_FOUND,
ErrorKey.MODEL_NOT_FOUND_BY_PROPERTY,
List.of(Constants.UNIT, "unit name", unitName)));
}

public boolean existsUnitByUnitName(String unitName) {
return getRepository().existsUnitByUnitName(unitName);
return getRepository().existsUnitByUnitNameAndIsDeletedFalse(unitName);
}

public boolean existsUnitByUnitNameAndIdNot(String unitName, Long id) {
return getRepository().existsUnitByUnitNameAndIdNot(unitName, id);
return getRepository().existsUnitByUnitNameAndIdNotAndIsDeletedFalse(unitName, id);
}

public List<Unit> findUnitsByUser(Long userId) {
return getRepository().findAllByCreatedById(userId);
return getRepository().findAllByCreatedByIdAndIsDeletedFalse(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ public String getModelName() {
}

public synchronized User getOrCreateUser(User user) {
Optional<User> savedUser = getRepository().findByEmail(user.getEmail());
Optional<User> savedUser = getRepository().findByEmailAndIsDeletedFalse(user.getEmail());
return savedUser.orElseGet(() -> getRepository().save(user));
}

public Optional<User> findByEmail(String email) {
return getRepository().findByEmail(email);
return getRepository().findByEmailAndIsDeletedFalse(email);
}

@Override
Expand All @@ -39,7 +39,7 @@ public User save(User user) {
}

public List<User> findAllOkrChampions() {
return getRepository().findByOkrChampion(true);
return getRepository().findByOkrChampionAndIsDeletedFalse(true);
}

public Iterable<User> saveAll(List<User> userList) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
public abstract class DeleteMethod<T, I, R extends CrudRepository<T, I>> {
protected R repo;
public abstract void deleteById(I id);
public abstract Iterable<T> findAll();

public void setRepo(R repo){
this.repo = repo;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package ch.puzzle.okr.service.persistence.customCrud;

import ch.puzzle.okr.models.Deletable;
import org.springframework.data.repository.CrudRepository;

import java.util.List;


public class HardDelete<T, I, R extends CrudRepository<T, I>> extends DeleteMethod<T, I, R>{

@Override
public void deleteById(I id) {
this.repo.deleteById(id);
}

@Override
public Iterable<T> findAll() {
return this.repo.findAll();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,9 @@ public class SoftDelete <T extends Deletable, I, R extends DeleteRepository<T, I
public void deleteById(I id) {
repo.markAsDeleted(id);
}

@Override
public Iterable<T> findAll() {
return this.repo.findAllVisible();
}
}
Original file line number Diff line number Diff line change
@@ -1,2 +1,22 @@
ALTER TABLE team
add COLUMN is_deleted boolean not null default false;

ALTER TABLE objective
add COLUMN is_deleted boolean not null default false;

ALTER TABLE key_result
add COLUMN is_deleted boolean not null default false;


ALTER TABLE check_in
add COLUMN is_deleted boolean not null default false;


ALTER TABLE action
add COLUMN is_deleted boolean not null default false;

ALTER TABLE unit
add COLUMN is_deleted boolean not null default false;

ALTER TABLE person
add COLUMN is_deleted boolean not null default false;

0 comments on commit 2804d75

Please sign in to comment.