Skip to content

Commit

Permalink
[backend/frontend] Remove linked to lessons learned
Browse files Browse the repository at this point in the history
  • Loading branch information
RomuDeuxfois committed Oct 19, 2024
1 parent f638c2e commit eb00fe0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,9 @@ public class ExerciseService {
private final ArticleRepository articleRepository;
private final ExerciseRepository exerciseRepository;
private final TeamRepository teamRepository;
private final ExerciseTeamUserRepository exerciseTeamUserRepository;
private final InjectRepository injectRepository;
private final ExerciseTeamUserRepository exerciseTeamUserRepository;
private final InjectRepository injectRepository;
private final LessonsCategoryRepository lessonsCategoryRepository;

// region properties
@Value("${openbas.mail.imap.enabled}")
Expand All @@ -79,18 +80,19 @@ public class ExerciseService {
private OpenBASConfig openBASConfig;
// endregion

public List<ExerciseSimple> exercises(){
public List<ExerciseSimple> exercises() {
// We get the exercises depending on whether or not we are granted
List<RawExerciseSimple> exercises = currentUser().isAdmin() ? exerciseRepository.rawAll()
: exerciseRepository.rawAllGranted(currentUser().getId());

return exercises.stream().map(exercise->exerciseMapper.fromRawExerciseSimple(exercise)).collect(Collectors.toList());
return exercises.stream().map(exercise -> exerciseMapper.fromRawExerciseSimple(exercise))
.collect(Collectors.toList());
}

public Page<ExerciseSimple> exercises(
Specification<Exercise> specification,
Specification<Exercise> specificationCount,
Pageable pageable) {
Specification<Exercise> specification,
Specification<Exercise> specificationCount,
Pageable pageable) {
CriteriaBuilder cb = this.entityManager.getCriteriaBuilder();

CriteriaQuery<Tuple> cq = cb.createTupleQuery();
Expand Down Expand Up @@ -120,9 +122,11 @@ public Page<ExerciseSimple> exercises(
List<ExerciseSimple> exercises = execution(query);

for (ExerciseSimple exercise : exercises) {
if (exercise.getInjectIds() != null) {
exercise.setExpectationResultByTypes(resultUtils.getResultsByTypes(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
exercise.setTargets(resultUtils.getInjectTargetWithResults(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
if (exercise.getInjectIds() != null) {
exercise.setExpectationResultByTypes(
resultUtils.getResultsByTypes(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
exercise.setTargets(
resultUtils.getInjectTargetWithResults(new HashSet<>(Arrays.asList(exercise.getInjectIds()))));
}
}

Expand Down Expand Up @@ -189,7 +193,7 @@ private List<ExerciseSimple> execution(TypedQuery<Tuple> query) {

// -- CREATION --

@Transactional(rollbackFor = Exception.class)
@Transactional(rollbackFor = Exception.class)
public Exercise createExercise(@NotNull final Exercise exercise) {
if (imapEnabled) {
exercise.setFrom(imapUsername);
Expand Down Expand Up @@ -421,6 +425,8 @@ public Iterable<Team> removeTeams(@NotBlank final String exerciseId, @NotNull fi
this.exerciseTeamUserRepository.deleteTeamsFromAllReferences(teamIds);
// Remove all association between injects and teams
this.injectRepository.removeTeamsForExercise(exerciseId, teamIds);
// Remove all association between lessons learned and teams
this.lessonsCategoryRepository.removeTeamsForExercise(exerciseId, teamIds);
return teamRepository.findAllById(teamIds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public class ScenarioService {
private final FileService fileService;
private final InjectDuplicateService injectDuplicateService;
private final InjectRepository injectRepository;

private final LessonsCategoryRepository lessonsCategoryRepository;

@Transactional
public Scenario createScenario(@NotNull final Scenario scenario) {
Expand Down Expand Up @@ -469,6 +469,8 @@ public Iterable<Team> removeTeams(@NotBlank final String scenarioId, @NotNull fi
this.scenarioTeamUserRepository.deleteTeamFromAllReferences(teamIds);
// Remove all association between injects and teams
this.injectRepository.removeTeamsForScenario(scenarioId, teamIds);
// Remove all association between lessons learned and teams
this.lessonsCategoryRepository.removeTeamsForExercise(scenarioId, teamIds);
return teamRepository.findAllById(teamIds);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,45 @@

import io.openbas.database.model.LessonsCategory;
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.query.Param;
import org.springframework.stereotype.Repository;

import jakarta.validation.constraints.NotNull;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Repository
public interface LessonsCategoryRepository extends CrudRepository<LessonsCategory, String>, JpaSpecificationExecutor<LessonsCategory> {

@NotNull
Optional<LessonsCategory> findById(@NotNull String id);

// -- TEAM -

@Modifying
@Query(
value = "DELETE FROM lessons_categories_teams lct "
+ "WHERE lct.team_id IN :teamIds "
+ "AND EXISTS (SELECT 1 FROM lessons_categories lc WHERE lct.lessons_category_id = lc.lessons_category_id AND lc.lessons_category_exercise = :exerciseId)",
nativeQuery = true
)
@Transactional
void removeTeamsForExercise(@Param("exerciseId") final String exerciseId, @Param("teamIds") final List<String> teamIds);

@Modifying
@Query(
value = "DELETE FROM lessons_categories_teams lct "
+ "WHERE lct.team_id IN :teamIds "
+ "AND EXISTS (SELECT 1 FROM lessons_categories lc WHERE lct.lessons_category_id = lc.lessons_category_id AND lc.lessons_category_scenario = :scenarioId)",
nativeQuery = true
)
@Transactional
void removeTeamsForScenario(@Param("scenarioId") final String scenarioId, @Param("teamIds") final List<String> teamIds);


}

0 comments on commit eb00fe0

Please sign in to comment.