Skip to content

Commit

Permalink
fix getReviewDay method of service
Browse files Browse the repository at this point in the history
  • Loading branch information
nora-kauczor committed Nov 18, 2024
1 parent f5e7c01 commit f7bfdb4
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 82 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,45 +26,46 @@ public class ReviewDayService {

public ReviewDay getReviewDay(String languageString, String userId, LocalDate day) throws LanguageNotFoundException {
Language language = Language.getEnumByString(languageString);
Map<String, Boolean> idsOfTodaysVocabs = new HashMap<>();
try {
idsOfTodaysVocabs = getIdsOfTodaysVocabs(language, userId, day);
} catch (
LanguageNotFoundException languageNotFoundException) {
throw new RuntimeException("Couldn't create ReviewDay because Language was not found.", languageNotFoundException);
}
Optional<ReviewDay> optionalReviewDay = Optional.ofNullable(reviewDayRepo.getByDayAndUserIdAndLanguage(day, userId, language));
Map<String, Boolean> finalIdsOfTodaysVocabs = idsOfTodaysVocabs;
Map<String, Boolean> finalIdsOfTodaysVocabs1 = idsOfTodaysVocabs;
ReviewDay reviewDay = optionalReviewDay.map(oldReviewDay -> {
// falls es schon einen review day gibt:
List<String> idsOfReviewedVocabs= oldReviewDay.idsOfVocabsToReview().entrySet().stream()
.filter(Map.Entry::getValue)
.map(Map.Entry::getKey)
.toList();
// String reviewDayId = oldReviewDay.id();
try {
ReviewDay newReviewDay = createReviewDay(oldReviewDay.id(), language, userId, day);
return newReviewDay.idsOfVocabsToReview().entrySet()
.stream().filter(pair -> !idsOfReviewedVocabs.contains(pair.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
} catch (LanguageNotFoundException languageNotFoundException) {
throw new RuntimeException("Couldn't create ReviewDay because Language was not found.", languageNotFoundException);
}
})
// falls es nch keinen review day gibt:
List<String> idsOfReviewedVocabs = oldReviewDay.idsOfVocabsToReview().entrySet().stream()
.filter(Map.Entry::getValue)
.map(Map.Entry::getKey)
.toList();
Map<String, Boolean> idsOfNonReviewedVocabs = finalIdsOfTodaysVocabs.entrySet()
.stream().filter(pair -> !idsOfReviewedVocabs.contains(pair.getKey()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
ReviewDay newReviewDay = new ReviewDay(oldReviewDay.id(), oldReviewDay.day(),
oldReviewDay.language(), oldReviewDay.userId(), idsOfNonReviewedVocabs);
return reviewDayRepo.save(newReviewDay);
})
.orElseGet(() -> {
try {
return createReviewDay(language, userId, day);
} catch (LanguageNotFoundException languageNotFoundException) {
throw new RuntimeException("Couldn't create ReviewDay because Language was not found.", languageNotFoundException);
}
});
assert reviewDay != null;
ReviewDay newReviewDay = new ReviewDay(null, day,
language, userId, finalIdsOfTodaysVocabs1);
return reviewDayRepo.save(newReviewDay);
});
return reviewDayRepo.save(reviewDay);
}

public ReviewDay createReviewDay(String reviewDayId, Language language, String userId, LocalDate day) throws LanguageNotFoundException {
public Map<String, Boolean> getIdsOfTodaysVocabs(Language language, String userId, LocalDate day) throws LanguageNotFoundException {
List<Vocab> allVocabsOfLanguage = vocabService.getAllVocabsOfLanguage(language.getStringOfEnum(), userId);
if (allVocabsOfLanguage.isEmpty()) {
return new ReviewDay(reviewDayId, day, language, userId, new HashMap<>());
return new HashMap<>();
}
List<Vocab> activeVocabs = allVocabsOfLanguage.stream()
.filter(vocab -> vocab.getDatesPerUser().containsKey(userId))
.toList();
if (activeVocabs.isEmpty()) {
return new ReviewDay(reviewDayId, day, language, userId, new HashMap<>());
return new HashMap<>();
}
List<Vocab> todaysVocabs = activeVocabs.stream()
.filter(vocab -> vocab.getDatesPerUser().get(userId).contains(day))
Expand All @@ -73,37 +74,16 @@ public ReviewDay createReviewDay(String reviewDayId, Language language, String u
for (Vocab vocab : todaysVocabs) {
idsToReview.put(vocab.getId(), false);
}
return new ReviewDay(reviewDayId, day, language, userId, idsToReview);
return idsToReview;
}

public ReviewDay createReviewDay(Language language, String userId, LocalDate day) throws LanguageNotFoundException {
List<Vocab> allVocabsOfLanguage = vocabService.getAllVocabsOfLanguage(language.getStringOfEnum(), userId);
if (allVocabsOfLanguage.isEmpty()) {
return new ReviewDay(null, day, language, userId, new HashMap<>());
}
List<Vocab> activeVocabs = allVocabsOfLanguage.stream()
.filter(vocab -> vocab.getDatesPerUser().containsKey(userId))
.toList();
if (activeVocabs.isEmpty()) {
return new ReviewDay(null, day, language, userId, new HashMap<>());
}
List<Vocab> todaysVocabs = activeVocabs.stream()
.filter(vocab -> vocab.getDatesPerUser().get(userId).contains(day))
.toList();
Map<String, Boolean> idsToReview = new HashMap<>();
for (Vocab vocab : todaysVocabs) {
idsToReview.put(vocab.getId(), false);
}
return new ReviewDay(null, day, language, userId, idsToReview);
}

public ReviewDay setVocabReviewed(String vocabId, String languageString, String userId, LocalDate day) throws LanguageNotFoundException {
Language language = Language.getEnumByString(languageString);
Optional<ReviewDay> oldReviewDay = Optional.of(reviewDayRepo.getByDayAndUserIdAndLanguage(day, userId, language));
Map<String, Boolean> newIdsOfVocabsToReview = oldReviewDay.get().idsOfVocabsToReview();
newIdsOfVocabsToReview.put(vocabId, true);
ReviewDay updatedReviewDay = new ReviewDay(oldReviewDay.get().id(), oldReviewDay.get().day(), language, userId, newIdsOfVocabsToReview);
// reviewDayRepo.deleteById(oldReviewDay.get().id());
return reviewDayRepo.save(updatedReviewDay);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,41 +55,41 @@ void getReviewDay_shouldThrowLanguageNotFoundException_whenCalledWithNonExistent
reviewService.getReviewDay("Esperanto", "user id", date));
}

@Test
void createReviewDay_shouldCreateANewReviewDay_whenCalledWithAReviewDayId() throws LanguageNotFoundException {
LocalDate date = LocalDate.of(2025, 1, 1);
Map<String, List<LocalDate>> datesPerUser = new HashMap<>();
datesPerUser.put("user id", List.of(date));
Vocab testVocab1 = new Vocab("vocab id", "la prueba",
"test", "", Language.SPANISH, datesPerUser, "Wordio");
Vocab testVocab2 = new Vocab("vocab id 2", "la prueba",
"test", "", Language.SPANISH, new HashMap<>(), "Wordio");
Map<String, Boolean> oldIdsOfVocabsToReview = new HashMap<>();
oldIdsOfVocabsToReview.put("vocab id", false);
oldIdsOfVocabsToReview.put("vocab id 2", false);
ReviewDay oldReviewDay = new ReviewDay("123", date,Language.SPANISH, "user id", oldIdsOfVocabsToReview);
when(mockVocabService.getAllVocabsOfLanguage("Spanish", "user id")).thenReturn(List.of(testVocab1, testVocab2));
Map<String, Boolean> idsOfVocabsToReview = new HashMap<>();
idsOfVocabsToReview.put("vocab id", false);
ReviewDay expected = new ReviewDay(null, date,Language.SPANISH, "user id", idsOfVocabsToReview);
ReviewDay actual = reviewService.createReviewDay(Language.SPANISH,"user id", date);
assertEquals(expected, actual);
}
// @Test
// void createReviewDay_shouldCreateANewReviewDay_whenCalledWithAReviewDayId() throws LanguageNotFoundException {
// LocalDate date = LocalDate.of(2025, 1, 1);
// Map<String, List<LocalDate>> datesPerUser = new HashMap<>();
// datesPerUser.put("user id", List.of(date));
// Vocab testVocab1 = new Vocab("vocab id", "la prueba",
// "test", "", Language.SPANISH, datesPerUser, "Wordio");
// Vocab testVocab2 = new Vocab("vocab id 2", "la prueba",
// "test", "", Language.SPANISH, new HashMap<>(), "Wordio");
// Map<String, Boolean> oldIdsOfVocabsToReview = new HashMap<>();
// oldIdsOfVocabsToReview.put("vocab id", false);
// oldIdsOfVocabsToReview.put("vocab id 2", false);
// ReviewDay oldReviewDay = new ReviewDay("123", date,Language.SPANISH, "user id", oldIdsOfVocabsToReview);
// when(mockVocabService.getAllVocabsOfLanguage("Spanish", "user id")).thenReturn(List.of(testVocab1, testVocab2));
// Map<String, Boolean> idsOfVocabsToReview = new HashMap<>();
// idsOfVocabsToReview.put("vocab id", false);
// ReviewDay expected = new ReviewDay(null, date,Language.SPANISH, "user id", idsOfVocabsToReview);
// ReviewDay actual = reviewService.createReviewDay(Language.SPANISH,"user id", date);
// assertEquals(expected, actual);
// }

@Test
void createReviewDay_shouldCreateANewReviewDay_whenCalledWithoutAReviewDayId() throws LanguageNotFoundException {
LocalDate date = LocalDate.of(2025, 1, 1);
Map<String, List<LocalDate>> datesPerUser = new HashMap<>();
datesPerUser.put("user id", List.of(date));
Vocab testVocab = new Vocab("vocab id", "la prueba",
"test", "", Language.SPANISH, datesPerUser, "Wordio");
when(mockVocabService.getAllVocabsOfLanguage("Spanish", "user id")).thenReturn(List.of(testVocab));
Map<String, Boolean> idsOfVocabsToReview = new HashMap<>();
idsOfVocabsToReview.put("vocab id", false);
ReviewDay expected = new ReviewDay(null, date,Language.SPANISH, "user id", idsOfVocabsToReview);
ReviewDay actual = reviewService.createReviewDay( Language.SPANISH,"user id", date);
assertEquals(expected, actual);
}
// @Test
// void createReviewDay_shouldCreateANewReviewDay_whenCalledWithoutAReviewDayId() throws LanguageNotFoundException {
// LocalDate date = LocalDate.of(2025, 1, 1);
// Map<String, List<LocalDate>> datesPerUser = new HashMap<>();
// datesPerUser.put("user id", List.of(date));
// Vocab testVocab = new Vocab("vocab id", "la prueba",
// "test", "", Language.SPANISH, datesPerUser, "Wordio");
// when(mockVocabService.getAllVocabsOfLanguage("Spanish", "user id")).thenReturn(List.of(testVocab));
// Map<String, Boolean> idsOfVocabsToReview = new HashMap<>();
// idsOfVocabsToReview.put("vocab id", false);
// ReviewDay expected = new ReviewDay(null, date,Language.SPANISH, "user id", idsOfVocabsToReview);
// ReviewDay actual = reviewService.createReviewDay( Language.SPANISH,"user id", date);
// assertEquals(expected, actual);
// }


@Test
Expand Down

0 comments on commit f7bfdb4

Please sign in to comment.