Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
AngularASPNETCore2WebApiAuth/src/Controllers/AccountsController.cs
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
50 lines (40 sloc)
1.61 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| |
using System.Threading.Tasks; | |
using AngularASPNETCore2WebApiAuth.Data; | |
using AngularASPNETCore2WebApiAuth.Helpers; | |
using AngularASPNETCore2WebApiAuth.Models.Entities; | |
using AngularASPNETCore2WebApiAuth.ViewModels; | |
using AutoMapper; | |
using Microsoft.AspNetCore.Identity; | |
using Microsoft.AspNetCore.Mvc; | |
namespace AngularASPNETCore2WebApiAuth.Controllers | |
{ | |
[Route("api/[controller]")] | |
public class AccountsController : Controller | |
{ | |
private readonly ApplicationDbContext _appDbContext; | |
private readonly UserManager<AppUser> _userManager; | |
private readonly IMapper _mapper; | |
public AccountsController(UserManager<AppUser> userManager, IMapper mapper, ApplicationDbContext appDbContext) | |
{ | |
_userManager = userManager; | |
_mapper = mapper; | |
_appDbContext = appDbContext; | |
} | |
// POST api/accounts | |
[HttpPost] | |
public async Task<IActionResult> Post([FromBody]RegistrationViewModel model) | |
{ | |
if (!ModelState.IsValid) | |
{ | |
return BadRequest(ModelState); | |
} | |
var userIdentity = _mapper.Map<AppUser>(model); | |
var result = await _userManager.CreateAsync(userIdentity, model.Password); | |
if (!result.Succeeded) return new BadRequestObjectResult(Errors.AddErrorsToModelState(result, ModelState)); | |
await _appDbContext.Customers.AddAsync(new Customer { IdentityId = userIdentity.Id, Location = model.Location }); | |
await _appDbContext.SaveChangesAsync(); | |
return new OkObjectResult("Account created"); | |
} | |
} | |
} |