mirror of
https://github.com/fergalmoran/podnoms.git
synced 2025-12-22 09:18:08 +00:00
Profile model working
This commit is contained in:
@@ -3,43 +3,50 @@ using System.Security.Claims;
|
||||
using System.Threading.Tasks;
|
||||
using AutoMapper;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using PodNoms.Api.Models;
|
||||
using PodNoms.Api.Models.ViewModels;
|
||||
using PodNoms.Api.Persistence;
|
||||
using PodNoms.Api.Services.Auth;
|
||||
|
||||
namespace PodNoms.Api.Controllers {
|
||||
[Authorize]
|
||||
[Route("[controller]")]
|
||||
public class ProfileController : Controller {
|
||||
private IUserRepository _userRepository;
|
||||
private readonly UserManager<ApplicationUser> _userManager;
|
||||
|
||||
public IUnitOfWork _unitOfWork { get; }
|
||||
|
||||
private readonly ClaimsPrincipal _caller;
|
||||
|
||||
public IMapper _mapper { get; }
|
||||
|
||||
public ProfileController(IUserRepository userRepository, IMapper mapper, IUnitOfWork unitOfWork) {
|
||||
public ProfileController(IUserRepository userRepository, IMapper mapper, IUnitOfWork unitOfWork,
|
||||
UserManager<ApplicationUser> userManager, IHttpContextAccessor httpContextAccessor) {
|
||||
this._caller = httpContextAccessor.HttpContext.User;
|
||||
this._mapper = mapper;
|
||||
this._unitOfWork = unitOfWork;
|
||||
this._userManager = userManager;
|
||||
this._userRepository = userRepository;
|
||||
}
|
||||
[HttpGet]
|
||||
public async Task<ActionResult<ProfileViewModel>> Get() {
|
||||
var email = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
|
||||
var user = await _userRepository.GetAsync(email);
|
||||
if (user != null) {
|
||||
var result = _mapper.Map<User, ProfileViewModel>(user);
|
||||
return new OkObjectResult(result);
|
||||
}
|
||||
return new NotFoundResult();
|
||||
var userId = _caller.Claims.Single(c => c.Type == "id");
|
||||
var user = await this._userManager.FindByIdAsync(userId.Value);
|
||||
|
||||
var result = _mapper.Map<ApplicationUser, ProfileViewModel>(user);
|
||||
return new OkObjectResult(result);
|
||||
}
|
||||
|
||||
[HttpPost]
|
||||
public async Task<ActionResult> Post([FromBody] ProfileViewModel item) {
|
||||
/* TODO: Update this to the new model
|
||||
var email = User.Claims.FirstOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
|
||||
var user = _userRepository.Get(email);
|
||||
|
||||
if (user == null || user.Id != item.Id)
|
||||
return new UnauthorizedResult();
|
||||
|
||||
user.Id = item.Id;
|
||||
user.EmailAddress = item.Email;
|
||||
user.FullName = item.Name;
|
||||
@@ -48,6 +55,7 @@ namespace PodNoms.Api.Controllers {
|
||||
|
||||
_userRepository.AddOrUpdate(user);
|
||||
await _unitOfWork.CompleteAsync();
|
||||
*/
|
||||
return new OkObjectResult(item);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user