Created
September 17, 2019 03:47
-
-
Save IntegerMan/e74e7f9f0765e3066ee3dbaec3586c2f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Collections.Generic; | |
using MattEland.Starship.Logic; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.AspNetCore.Mvc; | |
namespace MattEland.Starship.ProcessingService.Controllers | |
{ | |
[Route("api/[controller]")] | |
[ApiController] | |
public class GamesController : ControllerBase | |
{ | |
private readonly GameRepository _repository = new GameRepository(); | |
// GET api/games | |
[HttpGet] | |
public ActionResult<IEnumerable<GameState>> LoadGame() | |
{ | |
return Ok(_repository.Games); | |
} | |
// GET api/games/42 | |
[HttpGet("{id}")] | |
public ActionResult<GameState> LoadGame(int id) | |
{ | |
var game = _repository.GetGame(id); | |
if (game != null) | |
{ | |
return Ok(game); | |
} | |
return new NotFoundResult(); | |
} | |
// POST api/games | |
[HttpPost] | |
public CreatedResult NewGame() | |
{ | |
var game = _repository.CreateNewGame(); | |
return new CreatedResult($"/api/games/{game.Id}", game); | |
} | |
// DELETE api/games/42 | |
[HttpDelete("{id}")] | |
public StatusCodeResult Delete(int id) | |
{ | |
bool deleted = _repository.DeleteGame(id); | |
if (deleted) | |
{ | |
return new StatusCodeResult(StatusCodes.Status204NoContent); | |
} | |
return new NotFoundResult(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment