Created and updated methods to pull data from db.

This commit is contained in:
Grant Duchars 2021-01-28 22:50:07 -05:00 committed by Gnarwhal
parent e7e5168073
commit c160703048
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
4 changed files with 179 additions and 65 deletions

View file

@ -1,5 +1,7 @@
package achievements.controllers;
import achievements.data.Achievements;
import achievements.data.Games;
import achievements.data.InternalError;
import achievements.services.DbService;
import com.fasterxml.jackson.core.JsonProcessingException;
@ -19,11 +21,16 @@ public class Controller {
public Controller() {}
@RequestMapping(value = "/achievements", method = GET, produces = "application/json")
public ResponseEntity<String> index() {
@RequestMapping(value = "/achievements/{Name}", method = GET, produces = "application/json")
public ResponseEntity<String> fetchAchievements(@PathVariable("Name") String getName) {
var achievements = (Achievements) null;
if (getName == null) {
achievements = db.getAchievements("%");
} else {
achievements = db.getAchievements(getName);
}
var mapper = new ObjectMapper();
try {
var achievements = db.getAchievements();
var mapper = new ObjectMapper();
if (achievements == null) {
return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get achievements from database")), HttpStatus.INTERNAL_SERVER_ERROR);
} else {
@ -34,4 +41,25 @@ public class Controller {
return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@RequestMapping(value = "/games/{Name}", method = GET, produces = "text/html")
public ResponseEntity<String> fetchGames(@PathVariable("Name") String getName) {
var games = (Games) null;
if (getName == null) {
games = db.getGames("%");
} else {
games = db.getGames(getName);
}
var mapper = new ObjectMapper();
try {
if (games == null) {
return new ResponseEntity(mapper.writeValueAsString(new InternalError("Could not get games from database")), HttpStatus.INTERNAL_SERVER_ERROR);
} else {
return new ResponseEntity(mapper.writeValueAsString(games), HttpStatus.OK);
}
} catch (JsonProcessingException e) {
e.printStackTrace();
return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}