Created
May 18, 2017 20:57
-
-
Save dcomartin/b3d5c051f19ddeed001353cc953ce261 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; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.AspNetCore.Mvc; | |
namespace Migrap.AspNetCore.Hateoas.Demo | |
{ | |
[Route("api/[controller]")] | |
public class TodoController : Controller | |
{ | |
private readonly TodoCollection _db; | |
public TodoController(TodoCollection db) | |
{ | |
_db = db; | |
} | |
[HttpGet] | |
public IEnumerable<TodoModel> Get() | |
{ | |
return _db; | |
} | |
[HttpGet("{id}")] | |
public TodoModel Get(Guid id) | |
{ | |
return _db.Single(x => x.TodoId == id); | |
} | |
[HttpPost] | |
public void Post([FromBody]TodoModel value) | |
{ | |
_db.Add(value); | |
} | |
[HttpDelete("{id}")] | |
public IActionResult Delete(Guid id) | |
{ | |
var todo = _db.SingleOrDefault(x => x.TodoId == id); | |
if (todo == null) return NotFound(); | |
_db.Remove(todo); | |
return new NoContentResult(); | |
} | |
[HttpPut("{id}/complete")] | |
public TodoModel Complete(Guid id) | |
{ | |
var todo = _db.SingleOrDefault(x => x.TodoId == id); | |
if (todo == null) return null; | |
todo.IsCompleted = true; | |
return todo; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment