1
- using System . Collections . Generic ;
2
1
using 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 ;
4
6
using System . Linq ;
5
7
using System . Net . Http ;
6
- using Newtonsoft . Json ;
7
- using Microsoft . AspNetCore . Mvc . Filters ;
8
8
using System . Net . Http . Headers ;
9
+ using System . Threading . Tasks ;
10
+ using TodoApi . Models ;
9
11
10
- #region TodoController
11
12
namespace TodoApi . Controllers
12
13
{
13
- [ Route ( "api/[controller]" ) ]
14
+ [ Route ( "api/[controller]" ) ]
15
+ [ ApiController ]
14
16
public class TodoController : Controller
15
17
{
16
18
private readonly TodoContext _context ;
17
- #endregion
18
19
19
20
public TodoController ( TodoContext context )
20
21
{
@@ -27,81 +28,90 @@ public TodoController(TodoContext context)
27
28
}
28
29
}
29
30
30
- #region snippet_GetAll
31
+ // GET: api/Todo
31
32
[ HttpGet ]
32
- public IEnumerable < TodoItem > GetAll ( )
33
+ public async Task < ActionResult < IEnumerable < TodoItem > > > GetTodoItem ( )
33
34
{
34
- return _context . TodoItems . ToList ( ) ;
35
+ return await _context . TodoItems . ToListAsync ( ) ;
35
36
}
36
37
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 )
40
41
{
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 )
43
45
{
44
46
return NotFound ( ) ;
45
47
}
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 ( ) ;
61
48
62
- return CreatedAtRoute ( "GetTodo" , new { id = item . Id } , item ) ;
49
+ return todoItem ;
63
50
}
64
- #endregion
65
51
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.
67
55
[ HttpPut ( "{id}" ) ]
68
- public IActionResult Update ( long id , [ FromBody ] TodoItem item )
56
+ public async Task < IActionResult > PutTodoItem ( long id , TodoItem todoItem )
69
57
{
70
- if ( item == null || item . Id != id )
58
+ if ( id != todoItem . Id )
71
59
{
72
60
return BadRequest ( ) ;
73
61
}
74
62
75
- var todo = _context . TodoItems . FirstOrDefault ( t => t . Id == id ) ;
76
- if ( todo == null )
63
+ _context . Entry ( todoItem ) . State = EntityState . Modified ;
64
+
65
+ try
77
66
{
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
+ }
79
79
}
80
80
81
- todo . IsComplete = item . IsComplete ;
82
- todo . Name = item . Name ;
81
+ return NoContent ( ) ;
82
+ }
83
83
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 ) ;
87
94
}
88
- #endregion
89
95
90
- #region snippet_Delete
96
+ // DELETE: api/Todo/5
91
97
[ HttpDelete ( "{id}" ) ]
92
- public IActionResult Delete ( long id )
98
+ public async Task < ActionResult < TodoItem > > DeleteTodoItem ( long id )
93
99
{
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 )
96
102
{
97
103
return NotFound ( ) ;
98
104
}
99
105
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 ) ;
103
115
}
104
- #endregion
105
116
}
106
117
}
107
-
0 commit comments