update-current-user WIP

This commit is contained in:
Dominic Villemure
2024-06-29 22:45:17 -04:00
parent 623972bc36
commit 0e318eae32
13 changed files with 855 additions and 34 deletions

View File

@@ -1,21 +1,27 @@
using Google.Apis.Oauth2.v2.Data;
using Hutopy.Application.Common.Models;
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Common.Interfaces;
public interface IIdentityService
{
Task<string?> GetUserNameAsync(string userId);
Task<Result> CreateUserAsync(string email, string userName, string firstName, string lastName, string password);
Task<UserModel?> FindUserByIdAsync(string id);
Task<Result<string>> CreateUserAsync(Userinfo userInfo);
Task<Result<string>> CreateUserAsync(string email, string userName, string firstName, string lastName, string password);
Task<UserModel?> GetCurrentUserAsync();
Task<UserModel?> FindUserByEmailAsync(string id);
Task<string?> LoginAsync(string email, string password);
Task<Result<string>> UpdateCurrentUserAsync(string id, string firstName, string lastName, string occupation,
string phoneNumber, string birthDate, string country, string city, string address, string about,
string description,
SocialNetworksModel socialNetworks);
Task<IList<string>> GetCurrentUserRolesAsync();
Task<UserModel?> FindUserByIdAsync(string id);
Task<UserModel?> FindUserByEmailAsync(string email);
Task<UserModel?> GetUserByUserNameAsync(string userName);
Task<string?> LoginAsync(string email, string password);
Task<bool> IsInRoleAsync(string userId, string role);
Task<bool> AuthorizeAsync(string userId, string policyName);
Task<string?> GetUserNameAsync(string userId);
Task<Result> AddRoleAsync(string userId, string role);
Task<IList<string>> GetCurrentUserRolesAsync();
Task<(Result Result, string UserId)> CreateUserAsync(Userinfo userInfo);
Task<Result> DeleteUserAsync(string userId);
}

View File

@@ -5,9 +5,8 @@ public class Result(
IEnumerable<string> errors)
{
public bool Succeeded { get; init; } = succeeded;
public string[] Errors { get; init; } = errors.ToArray();
public static Result Success()
{
return new Result(true, Array.Empty<string>());
@@ -18,3 +17,27 @@ public class Result(
return new Result(false, errors);
}
}
public class Result<T>(
bool succeeded,
IEnumerable<string> errors)
{
public bool Succeeded { get; init; } = succeeded;
public string[] Errors { get; init; } = errors.ToArray();
public T? Value { get; set; }
public T GetValueOrDefault()
{
return Value ?? default(T)!;
}
public static Result<T> Success()
{
return new Result<T>(true, Array.Empty<string>());
}
public static Result<T> Failure(IEnumerable<string> errors)
{
return new Result<T>(false, errors);
}
}

View File

@@ -1,3 +1,5 @@
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Common.Models;
// TODO: Review nullable affectation here
@@ -7,6 +9,15 @@ public class UserModel
public string? UserName { get; set; }
public string? FirstName { get; set; }
public string? LastName { get; set; }
public string? Email { get; set; }
public string? Occupation { get; set; }
public string? Email { get; init; } = String.Empty;
public string? Phone { get; init; } = String.Empty;
public string? BirthDate { get; init; } = String.Empty;
public string? Country { get; init; } = String.Empty;
public string? City { get; init; } = String.Empty;
public string? Address { get; init; } = String.Empty;
public string? About { get; init; } = String.Empty;
public string? Description { get; init; } = String.Empty;
public SocialNetworksModel SocialNetworks { get; init; } = new();
public string? PortraitUrl { get; set; }
}

View File

@@ -0,0 +1,40 @@
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Users.Commands;
public class UpdateCurrentUserCommand : IRequest<string>
{
public required string FirstName { get; init; }
public required string LastName { get; init; }
public required string Occupation { get; init; }
public required string PhoneNumber { get; init; }
public required string BirthDate { get; init; }
public required string Country { get; init; }
public required string City { get; init; }
public required string Address { get; init; }
public required string About { get; init; }
public required string Description { get; init; }
public required SocialNetworksModel SocialNetworks { get; init; }
}
public class UpdateCurrentUserCommandHandler(IApplicationDbContext context, IIdentityService identityService) :
IRequestHandler<UpdateCurrentUserCommand, string>
{
public async Task<string> Handle(UpdateCurrentUserCommand request, CancellationToken cancellationToken)
{
var identityUser = await identityService.GetCurrentUserAsync();
if (identityUser?.Id is null) return string.Empty;
var result = await identityService.UpdateCurrentUserAsync(identityUser.Id, request.FirstName, request.LastName,
request.Occupation, request.PhoneNumber, request.BirthDate,
request.Country, request.City, request.Address, request.About,
request.Description, request.SocialNetworks);
await context.SaveChangesAsync(cancellationToken);
return result.GetValueOrDefault();
}
}

View File

@@ -0,0 +1,13 @@
namespace Hutopy.Application.Users.Models;
public class SocialNetworksModel
{
public string FacebookUrl { get; init; } = String.Empty;
public string InstagramUrl { get; init; } = String.Empty;
public string XUrl { get; init; } = String.Empty;
public string LinkedInUrl { get; init; } = String.Empty;
public string TikTokUrl { get; init; } = String.Empty;
public string YoutubeUrl { get; init; } = String.Empty;
public string RedditUrl { get; init; } = String.Empty;
public string YourWebsiteUrl { get; init; } = String.Empty;
}

View File

@@ -28,12 +28,22 @@ public class GetCurrentUserQueryHandler(
var user = new UserDto
{
Id = currentUserId,
FirstName = identityUser?.FirstName ?? "",
LastName = identityUser?.LastName ?? "",
UserName = identityUser?.UserName ?? "",
FirstName = identityUser.FirstName ?? "",
LastName = identityUser.LastName ?? "",
UserName = identityUser.UserName ?? "",
Occupation = identityUser.Occupation ?? "",
Phone = identityUser.Phone ?? "",
Email = identityUser.Email ?? "",
BirthDate = identityUser.BirthDate ?? "",
Country = identityUser.Country ?? "",
City = identityUser.City ?? "",
Address = identityUser.Address ?? "",
About = identityUser.About ?? "",
Description = identityUser.Description ?? "",
SocialNetworks = identityUser.SocialNetworks,
UserTransactions = transactions,
TotalBalance = transactions.Sum(x => x.Amount),
UserRoles = roles
UserRoles = roles,
};
return user;

View File

@@ -1,3 +1,5 @@
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Users.Queries.GetCurrentUser;
public class UserDto
@@ -6,6 +8,16 @@ public class UserDto
public required string FirstName { get; init; }
public required string LastName { get; init; }
public string UserName { get; init; } = String.Empty;
public string Occupation { get; init; } = String.Empty;
public string Email { get; init; } = String.Empty;
public string Phone { get; init; } = String.Empty;
public string BirthDate { get; init; } = String.Empty;
public string Country { get; init; } = String.Empty;
public string City { get; init; } = String.Empty;
public string Address { get; init; } = String.Empty;
public string About { get; init; } = String.Empty;
public string Description { get; init; } = String.Empty;
public SocialNetworksModel SocialNetworks { get; init; } = new();
public List<UserTransactionDto> UserTransactions { get; init; } = [];
public IList<string> UserRoles { get; init; } = [];
public required decimal TotalBalance { get; init; }