Initial backend
This commit is contained in:
parent
f10ca87343
commit
a06a558d7b
19 changed files with 303 additions and 0 deletions
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue