Skip to content

Commit

Permalink
Bugfix substitute classNames
Browse files Browse the repository at this point in the history
  • Loading branch information
PAException committed Nov 28, 2023
1 parent 7a6910f commit 5668c9e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;
import java.util.Set;

import static io.github.paexception.engelsburg.api.util.Constants.Substitute.NAME_KEY;

Expand Down Expand Up @@ -104,36 +104,32 @@ public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
* @return substitutes with specific filters
*/
public Result<GetSubstitutesResponseDTO> getSubstitutes(String classNameFilter, String teacherFilter) {
List<String> classes = classNameFilter == null || classNameFilter.isBlank()
? new ArrayList<>()
: Arrays.asList(classNameFilter.split(","));
List<String> teacher = teacherFilter == null || teacherFilter.isBlank()
? new ArrayList<>()
: Arrays.asList(teacherFilter.split(","));
Set<String> classes = classNameFilter == null || classNameFilter.isBlank()
? new HashSet<>()
: new HashSet<>(Arrays.asList(classNameFilter.split(",")));
Set<String> teachers = teacherFilter == null || teacherFilter.isBlank()
? new HashSet<>()
: new HashSet<>(Arrays.asList(teacherFilter.split(",")));
final Date date = new Date(System.currentTimeMillis());

//Get all substitute based on optional parameters
List<SubstituteModel> substitutes = new ArrayList<>();
if (teacher.isEmpty() && classes.isEmpty()) {
substitutes = this.substituteRepository.findAllByDateGreaterThanEqual(date);
final List<SubstituteModel> substitutes = new ArrayList<>();
if (classes.isEmpty() && teachers.isEmpty()) {
substitutes.addAll(this.substituteRepository.findAllByDateGreaterThanEqual(date));
} else {
substitutes = this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameIsNull(date);
}

if (!classes.isEmpty()) {
substitutes.addAll(this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameIn(date, classes));

substitutes.addAll(
classes.stream().filter(className -> className.length() >= 3 && !(Character.isDigit(className.charAt(1)) && className.length() == 3)).map(
className -> this.substituteRepository.findAllByDateGreaterThanEqualAndClassNameVariations(date, className)
).flatMap(Collection::stream).collect(Collectors.toList()));
}
if (!teacher.isEmpty()) {
substitutes.addAll(
this.substituteRepository.findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn(
date, teacher, date, teacher
)
);
for (SubstituteModel substitute : this.substituteRepository.findAllByDateGreaterThanEqual(date)) {
List<String> classNames = substitute.getClassName() == null
? new ArrayList<>()
: NotificationService.splitClasses(substitute.getClassName());

String teacher = substitute.getTeacher();
String substituteTeacher = substitute.getSubstituteTeacher();

if (classNames.isEmpty()) substitutes.add(substitute);
else if (classes.stream().anyMatch(classNames::contains)) substitutes.add(substitute);
else if (teacher != null && (teachers.contains(teacher) || teachers.contains(substituteTeacher))) {
substitutes.add(substitute);
}
}
}

//If no substitutes available return error
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.Collection;
import java.util.List;

@Repository
Expand Down Expand Up @@ -40,10 +41,10 @@ default List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameVariation

List<SubstituteModel> findByDateGreaterThanEqualAndClassNameIsLike(Date date, String className);

List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIn(Date date, List<String> classes);
List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIn(Date date, Collection<String> classes);

List<SubstituteModel> findAllByDateGreaterThanEqualAndTeacherInOrDateGreaterThanEqualAndSubstituteTeacherIn(
Date date, List<String> teacher, Date date2, List<String> substituteTeacher);
Date date, Collection<String> teacher, Date date2, Collection<String> substituteTeacher);

void deleteAllByDate(Date date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.validation.constraints.NotNull;
import java.time.Duration;
import java.time.Instant;
Expand Down Expand Up @@ -43,7 +44,7 @@ public class NotificationService implements LoggingComponent {
* @param className to split
* @return list of classNames
*/
private static List<String> splitClasses(String className) {
public static List<String> splitClasses(String className) {
if (className.length() <= 2 || (Character.isDigit(className.charAt(1)) && className.length() == 3)) {
return List.of(className);
} else { //5ab or 5ab6ab or E2Q2Q4
Expand Down

0 comments on commit 5668c9e

Please sign in to comment.