Search in sources :

Example 1 with GymNotFoundException

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));
            }
        }
    }
}
Also used : ExtractedResult(me.xdrop.fuzzywuzzy.model.ExtractedResult) GymNotFoundException(pokeraidbot.domain.errors.GymNotFoundException) UserMessedUpException(pokeraidbot.domain.errors.UserMessedUpException)

Example 2 with GymNotFoundException

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));
}
Also used : User(net.dv8tion.jda.core.entities.User) GymNotFoundException(pokeraidbot.domain.errors.GymNotFoundException) Gym(pokeraidbot.domain.gym.Gym) Test(org.junit.Test)

Aggregations

GymNotFoundException (pokeraidbot.domain.errors.GymNotFoundException)2 ExtractedResult (me.xdrop.fuzzywuzzy.model.ExtractedResult)1 User (net.dv8tion.jda.core.entities.User)1 Test (org.junit.Test)1 UserMessedUpException (pokeraidbot.domain.errors.UserMessedUpException)1 Gym (pokeraidbot.domain.gym.Gym)1