11using System . Collections . Generic ;
22using System . Linq ;
3- using System . Security . Claims ;
43using System . Threading . Tasks ;
5- using Microsoft . AspNet . Http ;
6- using Microsoft . AspNet . Mvc ;
7- using Microsoft . Data . Entity ;
4+ using Microsoft . AspNetCore . Http ;
5+ using Microsoft . AspNetCore . Mvc ;
6+ using Microsoft . EntityFrameworkCore ;
87using ASP . NET_Core_SPAs . Contexts ;
98using ASP . NET_Core_SPAs . Models ;
10- using Microsoft . AspNet . Authorization ;
9+ using Microsoft . AspNetCore . Authorization ;
10+ using Microsoft . AspNetCore . Identity ;
1111
1212namespace ASP . NET_Core_SPAs . API
1313{
@@ -17,9 +17,12 @@ namespace ASP.NET_Core_SPAs.API
1717 public class ContactsApiController : Controller
1818 {
1919 private readonly ContactsDbContext _context ;
20+ private readonly UserManager < ApplicationUser > _userManager ;
2021
21- public ContactsApiController ( ContactsDbContext context )
22+ public ContactsApiController ( UserManager < ApplicationUser > userManager ,
23+ ContactsDbContext context )
2224 {
25+ _userManager = userManager ;
2326 _context = context ;
2427 }
2528
@@ -36,14 +39,14 @@ public async Task<IActionResult> GetContact([FromRoute] int id)
3639 {
3740 if ( ! ModelState . IsValid )
3841 {
39- return HttpBadRequest ( ModelState ) ;
42+ return BadRequest ( ModelState ) ;
4043 }
4144
4245 var contact = await GetContacts ( ) . SingleAsync ( m => m . Id == id ) ;
4346
4447 if ( contact == null )
4548 {
46- return HttpNotFound ( ) ;
49+ return NotFound ( ) ;
4750 }
4851
4952 return Ok ( contact ) ;
@@ -55,13 +58,13 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
5558 {
5659 if ( ! ModelState . IsValid )
5760 {
58- return HttpBadRequest ( ModelState ) ;
61+ return BadRequest ( ModelState ) ;
5962 }
6063
6164 if ( id != contact . Id ||
62- User . GetUserId ( ) != contact . UserId )
65+ _userManager . GetUserId ( User ) != contact . UserId )
6366 {
64- return HttpBadRequest ( ) ;
67+ return BadRequest ( ) ;
6568 }
6669
6770 _context . Entry ( contact ) . State = EntityState . Modified ;
@@ -74,13 +77,13 @@ public async Task<IActionResult> PutContact([FromRoute] int id, [FromBody] Conta
7477 {
7578 if ( ! ContactExists ( id ) )
7679 {
77- return HttpNotFound ( ) ;
80+ return NotFound ( ) ;
7881 }
7982
8083 throw ;
8184 }
8285
83- return new HttpStatusCodeResult ( StatusCodes . Status204NoContent ) ;
86+ return new StatusCodeResult ( StatusCodes . Status204NoContent ) ;
8487 }
8588
8689 // POST: api/ContactsApi
@@ -89,10 +92,10 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
8992 {
9093 if ( ! ModelState . IsValid )
9194 {
92- return HttpBadRequest ( ModelState ) ;
95+ return BadRequest ( ModelState ) ;
9396 }
9497
95- contact . UserId = User . GetUserId ( ) ;
98+ contact . UserId = _userManager . GetUserId ( User ) ;
9699 _context . Contacts . Add ( contact ) ;
97100 try
98101 {
@@ -102,7 +105,7 @@ public async Task<IActionResult> PostContact([FromBody] Contact contact)
102105 {
103106 if ( ContactExists ( contact . Id ) )
104107 {
105- return new HttpStatusCodeResult ( StatusCodes . Status409Conflict ) ;
108+ return new StatusCodeResult ( StatusCodes . Status409Conflict ) ;
106109 }
107110
108111 throw ;
@@ -117,13 +120,13 @@ public async Task<IActionResult> DeleteContact([FromRoute] int id)
117120 {
118121 if ( ! ModelState . IsValid )
119122 {
120- return HttpBadRequest ( ModelState ) ;
123+ return BadRequest ( ModelState ) ;
121124 }
122125
123126 var contact = await GetContacts ( ) . SingleAsync ( m => m . Id == id ) ;
124127 if ( contact == null )
125128 {
126- return HttpNotFound ( ) ;
129+ return NotFound ( ) ;
127130 }
128131
129132 _context . Contacts . Remove ( contact ) ;
@@ -148,7 +151,7 @@ private bool ContactExists(int id)
148151
149152 private IQueryable < Contact > GetContacts ( )
150153 {
151- return _context . Contacts . Where ( c => c . UserId == User . GetUserId ( ) ) ;
154+ return _context . Contacts . Where ( c => c . UserId == _userManager . GetUserId ( User ) ) ;
152155 }
153156 }
154157}
0 commit comments