Skip to content
Snippets Groups Projects
Commit 6bd0b75b authored by ivarnm's avatar ivarnm
Browse files

#10 Add getGames()

parent bb640854
No related branches found
No related tags found
1 merge request!34Resolve "SinglePlayerSelectGameState"
......@@ -7,15 +7,20 @@ import com.badlogic.gdx.Net.HttpRequest;
import com.badlogic.gdx.Net.HttpResponse;
import com.badlogic.gdx.net.HttpParametersUtils;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.gameware.game.models.Game;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
public class QueryIntermediate {
private static String baseUrl = "http://localhost:3001/api/";
private static Json json = new Json();
private static JsonReader jsonReader = new JsonReader();
private static String[] sendGetRequest(String route) {
HttpRequest request = new HttpRequest(HttpMethods.GET);
......@@ -81,7 +86,7 @@ public class QueryIntermediate {
}
// ---------------- Games methods ----------------
public static Game getGameById(String gameId) throws IOException {
public static Game getGameById(String gameId) throws IOException, NoSuchElementException {
String route = "games/gamename/" + gameId;
String[] response = sendGetRequest(route);
checkStatusCode(response);
......@@ -90,4 +95,18 @@ public class QueryIntermediate {
return game;
}
public static List<Game> getGames() throws IOException {
String route = "games/";
List<Game> games = new ArrayList<>();
String[] response = sendGetRequest(route);
checkStatusCode(response);
JsonValue base = jsonReader.parse(response[1]);
JsonValue.JsonIterator iterator = base.iterator();
while (iterator.hasNext()) {
Game game = json.fromJson(Game.class, iterator.next().toString());
games.add(game);
}
return games;
}
}
......@@ -22,6 +22,11 @@ public class Game {
return name;
}
@Override
public String toString() {
return this.name;
}
public void reset() {
this._id = null;
this.name = null;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment