Skip to content

Commit

Permalink
Hotfix substitutes
Browse files Browse the repository at this point in the history
  • Loading branch information
PAException committed Nov 27, 2023
1 parent 1a9fa51 commit 7a6910f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Date;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

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

Expand All @@ -37,7 +40,7 @@ public class SubstituteController {
/**
* Create a {@link SubstituteModel} out of a {@link SubstituteDTO}.
*
* @param dto with information
* @param dto with information
* @return created substitute model
*/
private static SubstituteModel createSubstitute(SubstituteDTO dto) {
Expand Down Expand Up @@ -77,7 +80,7 @@ public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
//Check if substitutes have been updated or newly created
List<SubstituteDTO> updated = new ArrayList<>(), created = new ArrayList<>();
List<SubstituteModel> toSave = new ArrayList<>();
for (SubstituteDTO dto: fetchedDTOs) {
for (SubstituteDTO dto : fetchedDTOs) {
if (current.stream().anyMatch(substituteDTO -> substituteDTO.sameBase(dto))) {
if (!current.contains(dto)) updated.add(dto);
} else created.add(dto);
Expand Down Expand Up @@ -113,9 +116,17 @@ public Result<GetSubstitutesResponseDTO> getSubstitutes(String classNameFilter,
List<SubstituteModel> substitutes = new ArrayList<>();
if (teacher.isEmpty() && classes.isEmpty()) {
substitutes = 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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import io.github.paexception.engelsburg.api.database.model.SubstituteModel;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

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

@Repository
public interface SubstituteRepository extends JpaRepository<SubstituteModel, Integer> {
Expand All @@ -19,6 +19,7 @@ public interface SubstituteRepository extends JpaRepository<SubstituteModel, Int
* 9c --> 9%c%
* 10c --> 10%c%
* should not be used vor E1 - Q4
*
* @param className to convert
* @return parsed parameter
*/
Expand All @@ -29,17 +30,15 @@ static String likeClassName(String className) {

List<SubstituteModel> findAllByDate(Date date);

Optional<SubstituteModel> findByDateAndLessonAndTeacher(Date date, int lesson, String teacher);

default Optional<SubstituteModel> findByDateAndLessonAndClassNameLike(Date date, int lesson, String className) {
return this.findByDateAndLessonAndClassNameIsLike(date, lesson, likeClassName(className));
}
List<SubstituteModel> findAllByDateGreaterThanEqual(Date date);

Optional<SubstituteModel> findByDateAndLessonAndClassNameIsLike(Date date, int lesson, String className);
List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameIsNull(Date date);

Optional<SubstituteModel> findByDateAndLessonAndSubject(Date date, int lesson, String subject);
default List<SubstituteModel> findAllByDateGreaterThanEqualAndClassNameVariations(Date date, String className) {
return this.findByDateGreaterThanEqualAndClassNameIsLike(date, likeClassName(className));
}

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

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public boolean sameBase(SubstituteDTO dto) {
if (lesson != dto.lesson) return false;
if (!Objects.equals(className, dto.className)) return false;

if (!Character.isDigit(className.charAt(0))) { //Only for E1 - Q4
if (className != null && !Character.isDigit(className.charAt(0))) { //Only for E1 - Q4
return Objects.equals(teacher, dto.teacher);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ private static String getSubstituteTitle(SubstituteNotificationDTO substitute, b
} else if (DateUtils.isSameDay(substitute.getDate(), Date.from(Instant.now().plus(Duration.ofDays(1))))) {
relationalDay = "morgen";
} else {
relationalDay = "den " + DateTimeFormatter.ofPattern("dd.MM.").format(substitute.getDate().toInstant());
relationalDay = "den " + DateTimeFormatter.ofPattern("dd.MM.").format(substitute.getDate().toLocalDate());
}

return actuality + " Vertretung für " + relationalDay;
Expand Down

0 comments on commit 7a6910f

Please sign in to comment.