1- using System . Collections . Generic ;
21using Microsoft . AspNetCore . Mvc ;
3- using TodoApi . Models ;
2+ using Microsoft . AspNetCore . Mvc . Filters ;
3+ using Microsoft . EntityFrameworkCore ;
4+ using Newtonsoft . Json ;
5+ using System . Collections . Generic ;
46using System . Linq ;
57using System . Net . Http ;
6- using Newtonsoft . Json ;
7- using Microsoft . AspNetCore . Mvc . Filters ;
88using System . Net . Http . Headers ;
9+ using System . Threading . Tasks ;
10+ using TodoApi . Models ;
911
10- #region TodoController
1112namespace TodoApi . Controllers
1213{
13- [ Route ( "api/[controller]" ) ]
14+ [ Route ( "api/[controller]" ) ]
15+ [ ApiController ]
1416 public class TodoController : Controller
1517 {
1618 private readonly TodoContext _context ;
17- #endregion
1819
1920 public TodoController ( TodoContext context )
2021 {
@@ -27,81 +28,90 @@ public TodoController(TodoContext context)
2728 }
2829 }
2930
30- #region snippet_GetAll
31+ // GET: api/Todo
3132 [ HttpGet ]
32- public IEnumerable < TodoItem > GetAll ( )
33+ public async Task < ActionResult < IEnumerable < TodoItem > > > GetTodoItem ( )
3334 {
34- return _context . TodoItems . ToList ( ) ;
35+ return await _context . TodoItems . ToListAsync ( ) ;
3536 }
3637
37- #region snippet_GetByID
38- [ HttpGet ( "{id}" , Name = "GetTodo" ) ]
39- public IActionResult GetById ( long id )
38+ // GET: api/Todo/5
39+ [ HttpGet ( "{id}" ) ]
40+ public async Task < ActionResult < TodoItem > > GetTodoItem ( long id )
4041 {
41- var item = _context . TodoItems . FirstOrDefault ( t => t . Id == id ) ;
42- if ( item == null )
42+ var todoItem = await _context . TodoItems . FindAsync ( id ) ;
43+
44+ if ( todoItem == null )
4345 {
4446 return NotFound ( ) ;
4547 }
46- return new ObjectResult ( item ) ;
47- }
48- #endregion
49- #endregion
50- #region snippet_Create
51- [ HttpPost ]
52- public IActionResult Create ( [ FromBody ] TodoItem item )
53- {
54- if ( item == null )
55- {
56- return BadRequest ( ) ;
57- }
58-
59- _context . TodoItems . Add ( item ) ;
60- _context . SaveChanges ( ) ;
6148
62- return CreatedAtRoute ( "GetTodo" , new { id = item . Id } , item ) ;
49+ return todoItem ;
6350 }
64- #endregion
6551
66- #region snippet_Update
52+ // PUT: api/Todo/5
53+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
54+ // more details see https://aka.ms/RazorPagesCRUD.
6755 [ HttpPut ( "{id}" ) ]
68- public IActionResult Update ( long id , [ FromBody ] TodoItem item )
56+ public async Task < IActionResult > PutTodoItem ( long id , TodoItem todoItem )
6957 {
70- if ( item == null || item . Id != id )
58+ if ( id != todoItem . Id )
7159 {
7260 return BadRequest ( ) ;
7361 }
7462
75- var todo = _context . TodoItems . FirstOrDefault ( t => t . Id == id ) ;
76- if ( todo == null )
63+ _context . Entry ( todoItem ) . State = EntityState . Modified ;
64+
65+ try
7766 {
78- return NotFound ( ) ;
67+ await _context . SaveChangesAsync ( ) ;
68+ }
69+ catch ( DbUpdateConcurrencyException )
70+ {
71+ if ( ! TodoItemExists ( id ) )
72+ {
73+ return NotFound ( ) ;
74+ }
75+ else
76+ {
77+ throw ;
78+ }
7979 }
8080
81- todo . IsComplete = item . IsComplete ;
82- todo . Name = item . Name ;
81+ return NoContent ( ) ;
82+ }
8383
84- _context . TodoItems . Update ( todo ) ;
85- _context . SaveChanges ( ) ;
86- return new NoContentResult ( ) ;
84+ // POST: api/Todo
85+ // To protect from overposting attacks, please enable the specific properties you want to bind to, for
86+ // more details see https://aka.ms/RazorPagesCRUD.
87+ [ HttpPost ]
88+ public async Task < ActionResult < TodoItem > > PostTodoItem ( TodoItem todoItem )
89+ {
90+ _context . TodoItems . Add ( todoItem ) ;
91+ await _context . SaveChangesAsync ( ) ;
92+
93+ return CreatedAtAction ( "GetTodoItem" , new { id = todoItem . Id } , todoItem ) ;
8794 }
88- #endregion
8995
90- #region snippet_Delete
96+ // DELETE: api/Todo/5
9197 [ HttpDelete ( "{id}" ) ]
92- public IActionResult Delete ( long id )
98+ public async Task < ActionResult < TodoItem > > DeleteTodoItem ( long id )
9399 {
94- var todo = _context . TodoItems . FirstOrDefault ( t => t . Id == id ) ;
95- if ( todo == null )
100+ var todoItem = await _context . TodoItems . FindAsync ( id ) ;
101+ if ( todoItem == null )
96102 {
97103 return NotFound ( ) ;
98104 }
99105
100- _context . TodoItems . Remove ( todo ) ;
101- _context . SaveChanges ( ) ;
102- return new NoContentResult ( ) ;
106+ _context . TodoItems . Remove ( todoItem ) ;
107+ await _context . SaveChangesAsync ( ) ;
108+
109+ return todoItem ;
110+ }
111+
112+ private bool TodoItemExists ( long id )
113+ {
114+ return _context . TodoItems . Any ( e => e . Id == id ) ;
103115 }
104- #endregion
105116 }
106117}
107-
0 commit comments