Initial backend

This commit is contained in:
Gnarwhal 2021-01-27 03:27:04 -05:00
parent f10ca87343
commit a06a558d7b
Signed by: Gnarwhal
GPG key ID: 0989A73D8C421174
19 changed files with 303 additions and 0 deletions

View file

@ -0,0 +1,37 @@
package achievements.controllers;
import achievements.data.InternalError;
import achievements.services.DbService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import static org.springframework.web.bind.annotation.RequestMethod.GET;
@RestController
public class Controller {
@Autowired
private DbService db;
public Controller() {}
@RequestMapping(value = "/achievements", method = GET, produces = "application/json")
public ResponseEntity index() {
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 {
return new ResponseEntity(mapper.writeValueAsString(achievements), HttpStatus.OK);
}
} catch (JsonProcessingException e) {
e.printStackTrace();
return new ResponseEntity("{}", HttpStatus.INTERNAL_SERVER_ERROR);
}
}
}