Added Address Update Option

This commit is contained in:
cshowalter 2022-05-19 19:33:40 -07:00
parent f68200d33e
commit 9bc0fc3848
3 changed files with 37 additions and 3 deletions

View File

@ -2,6 +2,7 @@ using System.Security.Claims;
using API.Dtos;
using API.Errors;
using API.Extensions;
using AutoMapper;
using Core.Entities.Identity;
using Core.Interfaces;
using Microsoft.AspNetCore.Authorization;
@ -15,8 +16,10 @@ namespace API.Controllers
private readonly UserManager<AppUser> _userManager;
private readonly SignInManager<AppUser> _signInManager;
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;
_signInManager = signInManager;
_userManager = userManager;
@ -42,11 +45,22 @@ namespace API.Controllers
[Authorize]
[HttpGet("address")]
public async Task<ActionResult<Address>> GetUserAddress()
public async Task<ActionResult<AddressDto>> GetUserAddress()
{
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")]

17
API/Dtos/AddressDto.cs Normal file
View 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; }
}
}

View File

@ -1,6 +1,7 @@
using API.Dtos;
using AutoMapper;
using Core.Entities;
using Core.Entities.Identity;
namespace API.Helpers
{
@ -12,6 +13,8 @@ namespace API.Helpers
.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.PictureUrl, o => o.MapFrom<ProductUrlResolver>());
CreateMap<Address, AddressDto>().ReverseMap();
}
}
}