Skip to content

Instantly share code, notes, and snippets.

@IntegerMan
Created September 17, 2019 03:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IntegerMan/e74e7f9f0765e3066ee3dbaec3586c2f to your computer and use it in GitHub Desktop.
Save IntegerMan/e74e7f9f0765e3066ee3dbaec3586c2f to your computer and use it in GitHub Desktop.
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