Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/refactor delete #1397

Draft
wants to merge 12 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .run/OkrApplication-E2E.run.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<component name="ProjectRunConfigurationManager">
<configuration default="false" name="OkrApplication-E2E" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot">
<option name="ACTIVE_PROFILES" value="integration-test" />
<option name="ALTERNATIVE_JRE_PATH" value="$USER_HOME$/.sdkman/candidates/java/current" />
<option name="ALTERNATIVE_JRE_PATH" value="corretto-21" />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? maybe sdkman works best for all

<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<module name="backend" />
<option name="SPRING_BOOT_MAIN_CLASS" value="ch.puzzle.okr.OkrApplication" />
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/Action.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import java.util.Objects;

@Entity
public class Action implements WriteableInterface {
public class Action extends Deletable implements WriteableInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_action")
@SequenceGenerator(name = "sequence_action", allocationSize = 1)
Expand Down
19 changes: 19 additions & 0 deletions backend/src/main/java/ch/puzzle/okr/models/Deletable.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package ch.puzzle.okr.models;

import jakarta.persistence.*;
import org.hibernate.annotations.ColumnDefault;

@MappedSuperclass
public abstract class Deletable {
@Column(name = "is_deleted", nullable = false)
@ColumnDefault("false")
private boolean isDeleted = false;

public boolean isDeleted() {
return isDeleted;
}

public void setDeleted(boolean deleted) {
isDeleted = deleted;
}
}
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/Objective.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Entity
@Table(indexes = { @Index(name = "idx_objective_title", columnList = "title") })
public class Objective implements WriteableInterface {
public class Objective extends Deletable implements WriteableInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_objective")
@SequenceGenerator(name = "sequence_objective", allocationSize = 1)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/Team.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.Objects;

@Entity
public class Team implements WriteableInterface {
public class Team extends Deletable implements WriteableInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_team")
@SequenceGenerator(name = "sequence_team", allocationSize = 1)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/Unit.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Unit {
public class Unit extends Deletable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_unit")
@SequenceGenerator(name = "sequence_unit", allocationSize = 1)
Expand Down
2 changes: 1 addition & 1 deletion backend/src/main/java/ch/puzzle/okr/models/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Entity
// table cannot be named "user" since it is a reserved keyword of Postgres
@Table(name = "person")
public class User {
public class User extends Deletable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_person")
@SequenceGenerator(name = "sequence_person", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.puzzle.okr.models.checkin;

import ch.puzzle.okr.models.Deletable;
import ch.puzzle.okr.models.MessageKey;
import ch.puzzle.okr.models.User;
import ch.puzzle.okr.models.WriteableInterface;
Expand All @@ -15,7 +16,7 @@
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "check_in_type")
public abstract class CheckIn implements WriteableInterface {
public abstract class CheckIn extends Deletable implements WriteableInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_check_in")
@SequenceGenerator(name = "sequence_check_in", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Entity
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "key_result_type")
public abstract class KeyResult implements WriteableInterface {
public abstract class KeyResult extends Deletable implements WriteableInterface {
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator = "sequence_key_result")
@SequenceGenerator(name = "sequence_key_result", allocationSize = 1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

public interface ActionRepository extends CrudRepository<Action, Long> {
List<Action> getActionsByKeyResultIdOrderByPriorityAsc(Long keyResultId);
public interface ActionRepository extends DeleteRepository<Action, Long> {
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 CrudRepository<CheckIn, Long> {
List<CheckIn> findCheckInsByKeyResultIdOrderByCreatedOnDesc(Long keyResultId);
public interface CheckInRepository extends DeleteRepository<CheckIn, Long> {
List<CheckIn> findCheckInsByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(Long keyResultId);

CheckIn findFirstByKeyResultIdOrderByCreatedOnDesc(Long keyResultId);
CheckIn findFirstByKeyResultIdAndIsDeletedFalseOrderByCreatedOnDesc(Long keyResultId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package ch.puzzle.okr.repository;

import ch.puzzle.okr.models.Deletable;
import java.util.List;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.NoRepositoryBean;
import org.springframework.data.repository.query.Param;

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove comment seems unnecessary to me

// List<E> isDeleted(boolean isDeleted);

@Query("select e from #{#entityName} e where e.isDeleted = false ")
List<E> findAllVisible();

@Modifying
@Query("update #{#entityName} e set e.isDeleted = true where e.id = :id")
void markAsDeleted(@Param("id") I id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

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

public interface KeyResultRepository extends CrudRepository<KeyResult, Long> {
List<KeyResult> findByObjectiveId(Long objectiveId);
public interface KeyResultRepository extends DeleteRepository<KeyResult, Long> {
List<KeyResult> findByObjectiveIdAndIsDeletedFalse(Long objectiveId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import ch.puzzle.okr.models.Quarter;
import ch.puzzle.okr.models.Team;
import java.util.List;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface ObjectiveRepository extends CrudRepository<Objective, Long> {
public interface ObjectiveRepository extends DeleteRepository<Objective, Long> {

Integer countByTeamAndQuarter(Team team, Quarter quarter);
Integer countByTeamAndQuarterAndIsDeletedFalse(Team team, Quarter quarter);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is only used for testing, remove and remove tests or make separate task, up to you


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 CrudRepository<Team, Long> {
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 @@ -3,17 +3,16 @@
import ch.puzzle.okr.models.Unit;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UnitRepository extends CrudRepository<Unit, Long> {
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 existsUnitByUnitNameAndIsDeletedFalseAndIdNot(String unitName, Long id);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import ch.puzzle.okr.models.User;
import java.util.List;
import java.util.Optional;
import org.springframework.data.repository.CrudRepository;

public interface UserRepository extends CrudRepository<User, Long> {
Optional<User> findByEmail(String email);
public interface UserRepository extends DeleteRepository<User, Long> {
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 @@ -4,14 +4,15 @@

import ch.puzzle.okr.models.Action;
import ch.puzzle.okr.repository.ActionRepository;
import ch.puzzle.okr.service.persistence.customCrud.SoftDelete;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class ActionPersistenceService extends PersistenceBase<Action, Long, ActionRepository> {

protected ActionPersistenceService(ActionRepository repository) {
super(repository);
super(repository, new SoftDelete<>());
}

@Override
Expand All @@ -20,6 +21,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 @@ -4,14 +4,15 @@

import ch.puzzle.okr.models.checkin.CheckIn;
import ch.puzzle.okr.repository.CheckInRepository;
import ch.puzzle.okr.service.persistence.customCrud.SoftDelete;
import java.util.List;
import org.springframework.stereotype.Service;

@Service
public class CheckInPersistenceService extends PersistenceBase<CheckIn, Long, CheckInRepository> {

protected CheckInPersistenceService(CheckInRepository repository) {
super(repository);
super(repository, new SoftDelete<>());
}

@Override
Expand All @@ -20,10 +21,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 @@ -4,6 +4,7 @@

import ch.puzzle.okr.models.keyresult.KeyResult;
import ch.puzzle.okr.repository.KeyResultRepository;
import ch.puzzle.okr.service.persistence.customCrud.SoftDelete;
import jakarta.transaction.Transactional;
import java.util.List;
import org.springframework.stereotype.Service;
Expand All @@ -12,7 +13,7 @@
public class KeyResultPersistenceService extends PersistenceBase<KeyResult, Long, KeyResultRepository> {

protected KeyResultPersistenceService(KeyResultRepository repository) {
super(repository);
super(repository, new SoftDelete<>());
}

@Override
Expand All @@ -21,13 +22,14 @@ public String getModelName() {
}

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

@Transactional
public KeyResult recreateEntity(Long id, KeyResult keyResult) {
// delete entity in order to prevent duplicates in case of changed keyResultType
deleteById(id);
// deleteById(id, new HardDelete<>());

// reset id of key result, so it gets saved as a new entity
keyResult.resetId();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import ch.puzzle.okr.models.Team;
import ch.puzzle.okr.models.authorization.AuthorizationUser;
import ch.puzzle.okr.repository.ObjectiveRepository;
import ch.puzzle.okr.service.persistence.customCrud.SoftDelete;
import jakarta.persistence.EntityManager;
import jakarta.persistence.NoResultException;
import jakarta.persistence.TypedQuery;
Expand All @@ -29,7 +30,7 @@ public class ObjectivePersistenceService extends PersistenceBase<Objective, Long

protected ObjectivePersistenceService(ObjectiveRepository repository, EntityManager entityManager,
AuthorizationCriteria<Objective> authorizationCriteria) {
super(repository);
super(repository, new SoftDelete<>());
this.entityManager = entityManager;
this.authorizationCriteria = authorizationCriteria;
}
Expand All @@ -52,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 @@ -61,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
Loading
Loading