Added Address Update Option
This commit is contained in:
parent
f68200d33e
commit
9bc0fc3848
@ -2,6 +2,7 @@ using System.Security.Claims;
|
|||||||
using API.Dtos;
|
using API.Dtos;
|
||||||
using API.Errors;
|
using API.Errors;
|
||||||
using API.Extensions;
|
using API.Extensions;
|
||||||
|
using AutoMapper;
|
||||||
using Core.Entities.Identity;
|
using Core.Entities.Identity;
|
||||||
using Core.Interfaces;
|
using Core.Interfaces;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
@ -15,8 +16,10 @@ namespace API.Controllers
|
|||||||
private readonly UserManager<AppUser> _userManager;
|
private readonly UserManager<AppUser> _userManager;
|
||||||
private readonly SignInManager<AppUser> _signInManager;
|
private readonly SignInManager<AppUser> _signInManager;
|
||||||
private readonly ITokenService _tokenService;
|
private readonly ITokenService _tokenService;
|
||||||
public AccountController(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager, ITokenService tokenService)
|
private readonly IMapper _mapper;
|
||||||
|
public AccountController(UserManager<AppUser> userManager, SignInManager<AppUser> signInManager, ITokenService tokenService, IMapper mapper)
|
||||||
{
|
{
|
||||||
|
_mapper = mapper;
|
||||||
_tokenService = tokenService;
|
_tokenService = tokenService;
|
||||||
_signInManager = signInManager;
|
_signInManager = signInManager;
|
||||||
_userManager = userManager;
|
_userManager = userManager;
|
||||||
@ -42,11 +45,22 @@ namespace API.Controllers
|
|||||||
|
|
||||||
[Authorize]
|
[Authorize]
|
||||||
[HttpGet("address")]
|
[HttpGet("address")]
|
||||||
public async Task<ActionResult<Address>> GetUserAddress()
|
public async Task<ActionResult<AddressDto>> GetUserAddress()
|
||||||
{
|
{
|
||||||
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
||||||
return user.Address;
|
return _mapper.Map<Address, AddressDto>(user.Address);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Authorize]
|
||||||
|
[HttpPut("address")]
|
||||||
|
public async Task<ActionResult<AddressDto>> UpdateUserAddress(AddressDto address)
|
||||||
|
{
|
||||||
|
var user = await _userManager.FindUserByClaimsPrincipleWithAddressAsync(User);
|
||||||
|
user.Address = _mapper.Map<AddressDto, Address>(address);
|
||||||
|
var result = await _userManager.UpdateAsync(user);
|
||||||
|
if(result.Succeeded) return Ok(_mapper.Map<Address, AddressDto>(user.Address));
|
||||||
|
|
||||||
|
return BadRequest("Problem updating user!");
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost("login")]
|
[HttpPost("login")]
|
||||||
|
17
API/Dtos/AddressDto.cs
Normal file
17
API/Dtos/AddressDto.cs
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
namespace API.Dtos
|
||||||
|
{
|
||||||
|
public class AddressDto
|
||||||
|
{
|
||||||
|
public string FirstName { get; set; }
|
||||||
|
public string LastName { get; set; }
|
||||||
|
public string Street { get; set; }
|
||||||
|
public string City { get; set; }
|
||||||
|
public string State { get; set; }
|
||||||
|
public string ZipCode { get; set; }
|
||||||
|
}
|
||||||
|
}
|
@ -1,6 +1,7 @@
|
|||||||
using API.Dtos;
|
using API.Dtos;
|
||||||
using AutoMapper;
|
using AutoMapper;
|
||||||
using Core.Entities;
|
using Core.Entities;
|
||||||
|
using Core.Entities.Identity;
|
||||||
|
|
||||||
namespace API.Helpers
|
namespace API.Helpers
|
||||||
{
|
{
|
||||||
@ -12,6 +13,8 @@ namespace API.Helpers
|
|||||||
.ForMember(d => d.ProductBrand, o => o.MapFrom(s => s.ProductBrand.Name))
|
.ForMember(d => d.ProductBrand, o => o.MapFrom(s => s.ProductBrand.Name))
|
||||||
.ForMember(d => d.ProductType, o => o.MapFrom(s => s.ProductType.Name))
|
.ForMember(d => d.ProductType, o => o.MapFrom(s => s.ProductType.Name))
|
||||||
.ForMember(d => d.PictureUrl, o => o.MapFrom<ProductUrlResolver>());
|
.ForMember(d => d.PictureUrl, o => o.MapFrom<ProductUrlResolver>());
|
||||||
|
|
||||||
|
CreateMap<Address, AddressDto>().ReverseMap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user