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 f7bfdb4 commit 6297188
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,18 @@ private List<VocabIdsOfDate> getVocabIdsOfMonth(YearMonth yearMonth, Language la
}

public VocabIdsOfDate getVocabIdsOfDate(LocalDate date, Language language, String userName) {
List<String> ids = getVocabIdsOfDateAsList(date, language, userName);
return new VocabIdsOfDate(date, ids);
}

public List<String> getVocabIdsOfDateAsList(LocalDate date, Language language, String userName) {
List<Vocab> allVocabs = vocabRepo.findAll();
List<Vocab> vocabsOfDate = allVocabs.stream()
.filter(vocab -> vocab.getLanguage().equals(language))
.filter(vocab -> vocab.getDatesPerUser() != null && vocab.getDatesPerUser().containsKey(userName))
.filter(vocab -> vocab.getDatesPerUser().get(userName).stream()
.anyMatch(reviewDate -> reviewDate.equals(date))).toList();
List<String> ids = vocabsOfDate.stream().map(Vocab::getId).toList();
return new VocabIdsOfDate(date, ids);
.anyMatch(reviewDate -> reviewDate.equals(date))).toList();
return vocabsOfDate.stream().map(Vocab::getId).toList();
}

private VocabIdsOfDate[][] createEmptyCalendar() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,82 +2,58 @@


import lombok.RequiredArgsConstructor;
import org.example.backend.calendar.CalendarService;
import org.example.backend.exception.LanguageNotFoundException;

import org.example.backend.vocab.Language;
import org.example.backend.vocab.Vocab;

import org.example.backend.vocab.VocabService;

import org.springframework.stereotype.Service;


import java.time.LocalDate;
import java.util.*;
import java.util.stream.Collectors;



@RequiredArgsConstructor
@Service
public class ReviewDayService {

private final ReviewDayRepo reviewDayRepo;
private final VocabService vocabService;
private final CalendarService calendarService;

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);
}
List<String> idList = calendarService.getVocabIdsOfDateAsList(day, language, userId);
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 -> {
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));
Map<String, Boolean> idMap = new HashMap<>();
for (String id : idList) {
if (idsOfReviewedVocabs.contains(id))
{idMap.put(id, true);} else {
idMap.put(id, false);
}
}
ReviewDay newReviewDay = new ReviewDay(oldReviewDay.id(), oldReviewDay.day(),
oldReviewDay.language(), oldReviewDay.userId(), idsOfNonReviewedVocabs);
oldReviewDay.language(), oldReviewDay.userId(), idMap);
return reviewDayRepo.save(newReviewDay);
})
.orElseGet(() -> {
ReviewDay newReviewDay = new ReviewDay(null, day,
language, userId, finalIdsOfTodaysVocabs1);
return reviewDayRepo.save(newReviewDay);
Map<String, Boolean> idMap = new HashMap<>();
for (String id : idList) {
idMap.put(id, false);
}
ReviewDay newReviewDay = new ReviewDay(null, day,
language, userId, idMap);
return reviewDayRepo.save(newReviewDay);
});
return reviewDayRepo.save(reviewDay);
}

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 HashMap<>();
}
List<Vocab> activeVocabs = allVocabsOfLanguage.stream()
.filter(vocab -> vocab.getDatesPerUser().containsKey(userId))
.toList();
if (activeVocabs.isEmpty()) {
return 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 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));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package org.example.backend.review;

import org.example.backend.calendar.CalendarService;
import org.example.backend.exception.LanguageNotFoundException;
import org.example.backend.vocab.Language;
import org.example.backend.vocab.Vocab;
import org.example.backend.vocab.VocabService;
import org.junit.jupiter.api.Test;

import java.time.LocalDate;
Expand All @@ -17,8 +17,8 @@

class ReviewDayServiceTest {
private final ReviewDayRepo mockReviewRepo = mock(ReviewDayRepo.class);
private final VocabService mockVocabService = mock(VocabService.class);
private final ReviewDayService reviewService = new ReviewDayService(mockReviewRepo, mockVocabService);
private final CalendarService mockCalendarService = mock(CalendarService.class);
private final ReviewDayService reviewService = new ReviewDayService(mockReviewRepo, mockCalendarService);

@Test
void getReviewDay_shouldReturnReviewDayOfCurrentDate_ifItExists_whenCalled() throws LanguageNotFoundException {
Expand All @@ -33,8 +33,8 @@ void getReviewDay_shouldReturnReviewDayOfCurrentDate_ifItExists_whenCalled() thr
"000", date, Language.SPANISH, "user id", idsOfVocabsToReview);
when(mockReviewRepo.getByDayAndUserIdAndLanguage(date, "user id", Language.SPANISH))
.thenReturn(expected);
when(mockVocabService.getAllVocabsOfLanguage("Spanish", "user id"))
.thenReturn(List.of(testVocab));
when(mockCalendarService.getVocabIdsOfDateAsList(date,Language.SPANISH, "user id"))
.thenReturn(List.of("000"));
when(mockReviewRepo.save(expected)).thenReturn(expected);
ReviewDay actual = reviewService.getReviewDay("Spanish", "user id", date);
assertEquals(expected, actual);
Expand All @@ -55,42 +55,17 @@ 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_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 getIdsOfTodaysVocabs(){
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");

}

@Test
void setVocabReviewed_shouldSetBooleanToTrue_whenCalledWithIdOfVocab() throws LanguageNotFoundException {
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ function App() {
axios.put(`/api/review/${id}?language=${language}`)
.then(() => {
console.log(`Vocab with ID ${id} was marked as reviewed for today.`)
setTimeout(() => {
// setTimeout(() => {
getVocabsToReview()
}, 10000);
// }, 10000);
})
.catch(error => {
console.error(error)
Expand Down

0 comments on commit 6297188

Please sign in to comment.