1- using System ;
2- using System . Collections . Generic ;
3- using System . Data ;
4- using System . Data . Entity . Infrastructure ;
51using System . Linq ;
62using System . Net ;
7- using System . Net . Http ;
83using System . Threading . Tasks ;
9- using System . Web . Http ;
10- using System . Web . Http . Description ;
114using webapi . Data ;
125using webapi . Models ;
136using Microsoft . EntityFrameworkCore ;
147using Microsoft . AspNetCore . Mvc ;
158
169namespace webapi . Controllers
1710{
11+ [ Route ( "api/[controller]" ) ]
1812 [ ApiController ]
1913 public class BooksController : ControllerBase
2014 {
2115 private webapiContext db = new webapiContext ( ) ;
2216 // GET: api/Books
17+ [ HttpGet ]
2318 public IQueryable < Book > GetBooks ( )
2419 {
2520 return db . Books . Include ( b => b . Author ) ;
2621 }
2722
2823 // GET: api/Books/5
29- [ ResponseType ( typeof ( Book ) ) ]
30- public async Task < IHttpActionResult > GetBook ( int id )
24+ [ HttpGet ( "{id}" ) ]
25+ public async Task < ActionResult < Book > > GetBook ( int id )
3126 {
3227 Book book = await db . Books . FindAsync ( id ) ;
3328 if ( book == null )
@@ -39,8 +34,8 @@ public async Task<IHttpActionResult> GetBook(int id)
3934 }
4035
4136 // PUT: api/Books/5
42- [ ResponseType ( typeof ( void ) ) ]
43- public async Task < IHttpActionResult > PutBook ( int id , Book book )
37+ [ HttpPut ( "{id}" ) ]
38+ public async Task < ActionResult > PutBook ( int id , Book book )
4439 {
4540 if ( ! ModelState . IsValid )
4641 {
@@ -69,12 +64,12 @@ public async Task<IHttpActionResult> PutBook(int id, Book book)
6964 }
7065 }
7166
72- return StatusCode ( HttpStatusCode . NoContent ) ;
67+ return NoContent ( ) ;
7368 }
7469
7570 // POST: api/Books
76- [ ResponseType ( typeof ( Book ) ) ]
77- public async Task < IHttpActionResult > PostBook ( Book book )
71+ [ HttpPost ( "{id}" ) ]
72+ public async Task < ActionResult < Book > > PostBook ( Book book )
7873 {
7974 if ( ! ModelState . IsValid )
8075 {
@@ -92,8 +87,8 @@ public async Task<IHttpActionResult> PostBook(Book book)
9287 }
9388
9489 // DELETE: api/Books/5
95- [ ResponseType ( typeof ( Book ) ) ]
96- public async Task < IHttpActionResult > DeleteBook ( int id )
90+ [ HttpDelete ( "{id}" ) ]
91+ public async Task < ActionResult < Book > > DeleteBook ( int id )
9792 {
9893 Book book = await db . Books . FindAsync ( id ) ;
9994 if ( book == null )
@@ -106,16 +101,6 @@ public async Task<IHttpActionResult> DeleteBook(int id)
106101 return Ok ( book ) ;
107102 }
108103
109- protected override void Dispose ( bool disposing )
110- {
111- if ( disposing )
112- {
113- db . Dispose ( ) ;
114- }
115-
116- base . Dispose ( disposing ) ;
117- }
118-
119104 private bool BookExists ( int id )
120105 {
121106 return db . Books . Count ( e => e . Id == id ) > 0 ;
0 commit comments