use of pokeraidbot.domain.errors.GymNotFoundException in project pokeraidbot by magnusmickelsson.
the class GymRepository method search.
public Gym search(User user, String query, String region) {
if (region == null || StringUtils.isEmpty(query)) {
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH, LocaleService.DEFAULT));
}
final Set<Gym> gyms = getAllGymsForRegion(region);
final Locale localeForUser = localeService.getLocaleForUser(user);
final Optional<Gym> gym = get(query, region);
if (gym.isPresent()) {
return gym.get();
} else {
// 70 seems like a reasonable cutoff here...
List<ExtractedResult> candidates = extractTop(query, gyms.stream().map(s -> s.getName()).collect(Collectors.toList()), 6, 70);
if (candidates.size() == 1) {
return findByName(candidates.iterator().next().getString(), region);
} else if (candidates.size() < 1) {
throw new GymNotFoundException(query, localeService, LocaleService.SWEDISH, region);
} else {
List<Gym> matchingPartial = getMatchingPartial(query, region, candidates);
if (matchingPartial.size() == 1) {
return matchingPartial.get(0);
}
if (candidates.size() <= 5) {
String possibleMatches = candidates.stream().map(s -> findByName(s.getString(), region).getName()).collect(Collectors.joining(", "));
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_OPTIONS, localeForUser, possibleMatches));
} else {
throw new UserMessedUpException(user, localeService.getMessageFor(LocaleService.GYM_SEARCH_MANY_RESULTS, localeForUser));
}
}
}
}
use of pokeraidbot.domain.errors.GymNotFoundException in project pokeraidbot by magnusmickelsson.
the class GymRepositoryTest method addTemporaryGymToUppsala.
@Test
public void addTemporaryGymToUppsala() {
Gym gym;
try {
gym = repo.findByName("Mongo", "uppsala");
fail("Gym should not exist yet.");
} catch (GymNotFoundException e) {
// Expected
}
final User userMock = mock(User.class);
when(userMock.getName()).thenReturn("User");
repo.addTemporary(userMock, new Gym("Mongo", "66666666", "50.0001", "25.00001", "Uppsala", false), "uppsala");
gym = repo.findByName("Mongo", "uppsala");
assertThat(gym.getName(), is("Mongo"));
assertThat(gym.isExGym(), is(false));
}
Aggregations