Skip to content

Commit

Permalink
Step 2: Adding the Pokémon service
Browse files Browse the repository at this point in the history
  • Loading branch information
4lejandrito committed Sep 18, 2020
1 parent 9b0ba80 commit 45f34de
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ apply plugin: "com.liferay.plugin"

dependencies {
compileOnly group: "com.liferay", name: "com.liferay.frontend.js.loader.modules.extender.api", version: "4.0.1"
compileOnly group: "com.liferay", name: "com.liferay.petra.string", version: "4.0.2"
compileOnly group: "com.liferay.portal", name: "com.liferay.portal.kernel", version: "5.4.0"
compileOnly group: "com.liferay.portal", name: "com.liferay.util.taglib", version: "5.0.3"
compileOnly group: "javax.portlet", name: "portlet-api", version: "3.0.0"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.pokemon.item.selector.web.pokemon;

import java.util.List;

/**
* @author Alejandro Tardín
*/
public interface Pokemon {

public String getImageURL();

public String getName();

public List<Stat> getStats();

public String getType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.pokemon.item.selector.web.pokemon;

import com.liferay.petra.string.StringBundler;
import com.liferay.portal.kernel.exception.PortalException;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.kernel.json.JSONFactoryUtil;
import com.liferay.portal.kernel.json.JSONObject;
import com.liferay.portal.kernel.util.Http;
import com.liferay.portal.kernel.util.ListUtil;
import com.liferay.portal.kernel.util.Validator;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.util.ArrayList;
import java.util.List;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;

/**
* @author Alejandro Tardín
*/
@Component(service = PokemonService.class)
public class PokemonService {

public int getPokemonCount(String keywords) throws PortalException {
List<Pokemon> pokemons = getPokemons(keywords, 0, _POKEMON_AMOUNT);

return pokemons.size();
}

public List<Pokemon> getPokemons(String keywords, int start, int end)
throws PortalException {

return ListUtil.subList(
ListUtil.filter(
_getPokemons(),
pokemon ->
Validator.isNull(keywords) ||
pokemon.getName(
).toLowerCase(
).contains(
keywords.toLowerCase()
)),
start, end);
}

private JSONObject _fetchJSONObject(String url) throws PortalException {
Http.Options options = new Http.Options();

options.setLocation(url);

try {
String responseJSON = _http.URLtoString(options);

Http.Response response = options.getResponse();

if (response.getResponseCode() != HttpURLConnection.HTTP_OK) {
throw new PortalException(
StringBundler.concat(
"Response code ", response.getResponseCode(), ": ",
responseJSON));
}

return JSONFactoryUtil.createJSONObject(responseJSON);
}
catch (IOException ioException) {
throw new PortalException(ioException);
}
}

private List<Pokemon> _getPokemons() throws PortalException {
if (!_pokemons.isEmpty()) {
return _pokemons;
}

JSONObject jsonObject = _fetchJSONObject(
String.format(
"https://pokeapi.co/api/v2/pokemon?limit=%s", _POKEMON_AMOUNT));

JSONArray resultsJSONArray = jsonObject.getJSONArray("results");

for (int i = 0; i < resultsJSONArray.length(); i++) {
JSONObject pokemonInitialJSONObject =
resultsJSONArray.getJSONObject(i);

JSONObject pokemonJSONObject = _fetchJSONObject(
pokemonInitialJSONObject.getString("url"));

_pokemons.add(
new Pokemon() {

@Override
public String getImageURL() {
return String.format(
"https://pokeres.bastionbot.org/images/pokemon" +
"/%d.png",
pokemonJSONObject.getInt("id"));
}

@Override
public String getName() {
return pokemonJSONObject.getString("name");
}

@Override
public List<Stat> getStats() {
JSONArray statsJSONArray =
pokemonJSONObject.getJSONArray("stats");
List<Stat> stats = new ArrayList<>();

for (int j = 0; j < statsJSONArray.length(); j++) {
JSONObject statJSONObject =
statsJSONArray.getJSONObject(j);

stats.add(
new Stat(
statJSONObject.getJSONObject(
"stat"
).getString(
"name"
),
statJSONObject.getInt("base_stat"), 255));
}

stats.add(
new Stat(
"experience",
pokemonJSONObject.getInt("base_experience"),
1000));

return stats;
}

@Override
public String getType() {
return pokemonJSONObject.getJSONArray(
"types"
).getJSONObject(
0
).getJSONObject(
"type"
).getString(
"name"
);
}

});
}

return _pokemons;
}

private static final int _POKEMON_AMOUNT = 150;

@Reference
private Http _http;

private List<Pokemon> _pokemons = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/**
* Copyright (c) 2000-present Liferay, Inc. All rights reserved.
*
* This library is free software; you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by the Free
* Software Foundation; either version 2.1 of the License, or (at your option)
* any later version.
*
* This library is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details.
*/

package com.liferay.pokemon.item.selector.web.pokemon;

import com.liferay.portal.kernel.util.HashMapBuilder;

import java.util.Map;

/**
* @author Alejandro Tardín
*/
public class Stat {

public Stat(String name, int value, int max) {
_name = name;
_value = value;
_max = max;
}

public String getAbbr() {
return _abbrs.getOrDefault(_name, _name);
}

public int getMax() {
return _max;
}

public String getName() {
return _name;
}

public int getValue() {
return _value;
}

private static Map<String, String> _abbrs = HashMapBuilder.put(
"attack", "ATK"
).put(
"defense", "DEF"
).put(
"experience", "EXP"
).put(
"special-attack", "SP ATK"
).put(
"special-defense", "SP DEF"
).put(
"speed", "SPD"
).build();

private int _max;
private String _name;
private int _value;

}

0 comments on commit 45f34de

Please sign in to comment.