This repository was archived by the owner on Nov 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesController.cs
More file actions
76 lines (68 loc) · 2.07 KB
/
Copy pathNotesController.cs
File metadata and controls
76 lines (68 loc) · 2.07 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WebNote.Core.Entities;
using WebNote.Core.Interfaces;
namespace WebNote.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class NotesController : ControllerBase
{
private readonly IAsyncRepository<NoteItem> _noteItemRepository;
public NotesController(IAsyncRepository<NoteItem> noteItemRepository)
{
_noteItemRepository = noteItemRepository;
}
// GET: api/[controller]
[HttpGet]
public async Task<IReadOnlyList<NoteItem>> GetNote()
{
return await _noteItemRepository.ListAllAsync();
}
// GET: api/[controller]/5
[HttpGet("{id}")]
public async Task<ActionResult<NoteItem>> Get(int id)
{
var note = await _noteItemRepository.GetByIdAsync(id);
if (note == null)
{
return NotFound();
}
return note;
}
// PUT: api/[controller]/5
[HttpPut("{id}")]
public async Task<IActionResult> Put(int id, NoteItem noteItem)
{
noteItem.Id = id;
if (id != noteItem.Id)
{
return BadRequest();
}
await _noteItemRepository.UpdateAsync(noteItem);
return NoContent();
}
// POST: api/[controller]
[HttpPost]
public async Task<ActionResult<NoteItem>> Post(NoteItem noteItem)
{
await _noteItemRepository.AddAsync(noteItem);
return CreatedAtAction("Get", new { id = noteItem.Id }, noteItem);
}
// DELETE: api/[controller]/5
[HttpDelete("{id}")]
public async Task<ActionResult<NoteItem>> Delete(int id)
{
var note = await _noteItemRepository.DeleteAsync(id);
if (note == null)
{
return NotFound();
}
return note;
}
}
}