Skip to content

Commit

Permalink
Fixed substitutes
Browse files Browse the repository at this point in the history
  • Loading branch information
PAException committed Nov 1, 2023
1 parent f28b0b6 commit dd997a0
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,13 @@ public List<String> getTimetableTokens(SubstituteNotificationDTO dto) {
//Add all possible combinations
List<String> combinations = new ArrayList<>();
for (int i = lower; i <= upper; i++) {
combinations.add(prefix + "." + day + "." + i + "." + teacher);
combinations.add(prefix + "." + day + "." + i + "." + className);
if (teacher != null && !teacher.isEmpty()) {
combinations.add(prefix + "." + day + "." + i + "." + teacher);
}

if (className != null && !className.isEmpty()) {
combinations.add(prefix + "." + day + "." + i + "." + className);
}
}

//Return all tokens that matched the combinations
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

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

/**
Expand Down Expand Up @@ -75,38 +75,22 @@ private static SubstituteModel createSubstitute(int substituteId, SubstituteDTO
*/
@Transactional
public void updateSubstitutes(List<SubstituteDTO> fetchedDTOs, Date date) {
//Get all substitutes by date, remove all which are also in fetched dtos
for (SubstituteModel substitute : this.substituteRepository.findAllByDate(date))
fetchedDTOs.remove(substitute.toResponseDTO());
List<SubstituteDTO> current = new ArrayList<>();
for (SubstituteModel substitute : this.substituteRepository.findAllByDate(date)) {
current.add(substitute.toResponseDTO());
}
this.substituteRepository.deleteAllByDate(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) {
//Get optional substitute based on dto information
Optional<SubstituteModel> optionalSubstitute;
if (Character.isDigit(dto.getClassName().charAt(0))) { //5a-10e
optionalSubstitute = this.substituteRepository
.findByDateAndLessonAndClassNameLike(date, dto.getLesson(), dto.getClassName());
} else if (notBlank(dto.getTeacher())) { //E1-Q4 with teacher
optionalSubstitute = this.substituteRepository
.findByDateAndLessonAndTeacher(date, dto.getLesson(), dto.getTeacher());
} else { //E1-Q4 without teacher
optionalSubstitute = this.substituteRepository
.findByDateAndLessonAndSubject(date, dto.getLesson(), dto.getSubject());
}

//Check if the substitute was newly created or updated
if (optionalSubstitute.isPresent()) {
//Update substitute and add to updated list
toSave.add(createSubstitute(optionalSubstitute.get().getSubstituteId(), dto));
updated.add(dto);
} else {
//Save newly created substitute and add to created list
toSave.add(createSubstitute(-1, dto));
created.add(dto);
}
for (SubstituteDTO dto: fetchedDTOs) {
if (current.contains(dto)) updated.add(dto);
else created.add(dto);

toSave.add(createSubstitute(-1, dto));
}

this.substituteRepository.saveAll(toSave);

//Send notifications if lists are not empty
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@
@Repository
public interface SubstituteRepository extends JpaRepository<SubstituteModel, Integer> {

/**
* Converts the class to a like parameter.
* 9c --> 9%c%
* 10c --> 10%c%
* should not be used vor E1 - Q4
* @param className to convert
* @return parsed parameter
*/
static String likeClassName(String className) {
if (className.length() == 2) return className.charAt(0) + "%" + className.charAt(1) + "%";
else return className.substring(0, 2) + "%" + className.charAt(2) + "%";
Expand All @@ -37,4 +45,6 @@ default Optional<SubstituteModel> findByDateAndLessonAndClassNameLike(Date date,

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

void deleteAllByDate(Date date);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.Data;
import lombok.NoArgsConstructor;
import java.sql.Date;
import java.util.Objects;

@Data
@NoArgsConstructor
Expand Down Expand Up @@ -42,4 +43,25 @@ public SubstituteDTO appendText(String text) {
return this;
}

@Override
public boolean equals(Object other) {
if (this == other) return true;
if (other == null || getClass() != other.getClass()) return false;

SubstituteDTO dto = (SubstituteDTO) other;
if (!Objects.equals(date, dto.date)) return false;
if (lesson != dto.lesson) return false;
if (!Objects.equals(className, dto.className)) return false;

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

return true;
}

@Override
public int hashCode() {
return Objects.hash(date, className, lesson, teacher);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package io.github.paexception.engelsburg.api.test.service.scheduled;

import io.github.paexception.engelsburg.api.endpoint.dto.SubstituteDTO;
import io.github.paexception.engelsburg.api.service.scheduled.SubstituteUpdateService;
import org.junit.jupiter.api.Test;

import java.sql.Date;
import java.util.Objects;

public class SubstituteParseTest {

@Test
public void testSubstituteParse() {
new SubstituteUpdateService(null, null, null).updateSubstitutes();
}

@Test
public void substituteDTOEqualTest() {
//Correct 5a - 10e
SubstituteDTO dto1 = new SubstituteDTO(
new Date(169879761),
"10c",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto2 = new SubstituteDTO(
new Date(169879761),
"10c",
2,
"D",
"GRB",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert Objects.equals(dto1, dto2);

//Different lesson 5a - 10e
SubstituteDTO dto3 = new SubstituteDTO(
new Date(169879761),
"10a",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto4 = new SubstituteDTO(
new Date(169879761),
"10c",
3,
"D",
"GRB",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert !Objects.equals(dto3, dto4);

//Different class
SubstituteDTO dto5 = new SubstituteDTO(
new Date(169879761),
"Q3",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto6 = new SubstituteDTO(
new Date(169879761),
"10c",
2,
"D",
"GRB",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert !Objects.equals(dto5, dto6);

//Different date
SubstituteDTO dto7 = new SubstituteDTO(
new Date(169761),
"10c",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto8 = new SubstituteDTO(
new Date(169879761),
"10c",
2,
"D",
"GRB",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert !Objects.equals(dto7, dto8);

//Different teacher E1 - Q4
SubstituteDTO dto9 = new SubstituteDTO(
new Date(169761),
"Q3",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto10 = new SubstituteDTO(
new Date(169879761),
"Q3",
2,
"D",
"GRB",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert !Objects.equals(dto9, dto10);

//Correct teacher E1 - Q4
SubstituteDTO dto11 = new SubstituteDTO(
new Date(169761),
"Q3",
2,
"M",
"BSU",
"GAR",
"Vertretung",
"Mo-21.2. / 4",
"H301",
"Aufg. vorhanden"
);
SubstituteDTO dto12 = new SubstituteDTO(
new Date(169879761),
"Q3",
2,
"D",
"BSU",
"KLE",
"Betreuung",
"asg",
"dshssdssh",
"dshgdhsen"
);

assert !Objects.equals(dto11, dto12);
}

}

0 comments on commit dd997a0

Please sign in to comment.