/
NotesController.cs
44 lines (39 loc) · 1.08 KB
/
NotesController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Linq;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
namespace LeanAspNetCore.Api.Notes
{
[Route("api/notes")]
public class NotesController : Controller
{
private NotesStore _notesStore;
public NotesController(NotesStore notesStore)
{
_notesStore = notesStore;
}
[HttpGet]
public IActionResult Get()
{
var notes = _notesStore.GetAll().OrderByDescending(n => n.CreatedAt);
return Ok(notes);
}
[HttpPost]
public IActionResult Add([FromBody]Note note)
{
if (ModelState.IsValid)
{
note = _notesStore.AddNote(note);
return Created($"api/notes/{note.Id}", note);
}
return BadRequest(ModelState);
}
[HttpDelete("{noteId}")]
public IActionResult Remove(string noteId)
{
_notesStore.RemoveNote(noteId);
return NoContent();
}
}
}