#27 added userTransactions

This commit is contained in:
Dominic Villemure
2024-04-22 15:55:49 -04:00
parent cbde9838d1
commit b63d53f109
17 changed files with 696 additions and 28 deletions

View File

@@ -1,15 +1,14 @@
using Hutopy.Domain.Interfaces;
using System.Security.Claims;
using Hutopy.Domain.Interfaces;
using Hutopy.Domain.Models;
using Hutopy.Infrastructure.Identity;
using Microsoft.AspNetCore.Http.HttpResults;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
namespace Hutopy.Infrastructure.Services;
public class UserService(UserManager<ApplicationUser> userManager) : IUserService
public class UserService(UserManager<ApplicationUser> userManager, IHttpContextAccessor httpContextAccessor) : IUserService
{
private readonly UserManager<ApplicationUser> _userManager = userManager;
public async Task CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
{
var applicationUser = new ApplicationUser
@@ -21,7 +20,7 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
};
//todo: Need to handle errors better for the user.
var response = await _userManager.CreateAsync(applicationUser, password);
var response = await userManager.CreateAsync(applicationUser, password);
if (response.Errors.Any())
{
@@ -31,7 +30,7 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
public async Task<UserModel?> FindUserByIdAsync(string id)
{
var response = await _userManager.FindByIdAsync(id);
var response = await userManager.FindByIdAsync(id);
if (response == null) return null;
@@ -46,10 +45,21 @@ public class UserService(UserManager<ApplicationUser> userManager) : IUserServic
return userModel;
}
public async Task<UserModel?> GetCurrentUserAsync()
{
var userId = httpContextAccessor.HttpContext?.User.FindFirstValue(ClaimTypes.NameIdentifier);
if (string.IsNullOrEmpty(userId))
{
return null;
}
return await FindUserByIdAsync(userId);
}
public async Task<UserModel?> FindUserByEmailAsync(string email)
{
var response = await _userManager.FindByEmailAsync(email);
var response = await userManager.FindByEmailAsync(email);
if (response == null) return null;