update-current-user WIP

This commit is contained in:
Dominic Villemure
2024-06-29 22:45:17 -04:00
committed by Jonathan Bourdon
parent 72e243cf84
commit 5282fcfd49
13 changed files with 855 additions and 34 deletions

View File

@@ -3,6 +3,8 @@ using Google.Apis.Oauth2.v2.Data;
using System.Security.Claims;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models;
using Hutopy.Application.Users.Models;
using Hutopy.Infrastructure.Identity.OwnedEntities;
using Hutopy.Infrastructure.Utils;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
@@ -45,23 +47,10 @@ public class IdentityService(
return userModel;
}
public async Task<(Result Result, string UserId)> CreateUserAsync(string userName, string password)
{
var user = new ApplicationUser
{
UserName = userName,
Email = userName,
};
var result = await userManager.CreateAsync(user, password);
return (result.ToApplicationResult(), user.Id);
}
public async Task<(Result Result, string UserId)> CreateUserAsync(Userinfo userInfo)
public async Task<Result<string>> CreateUserAsync(Userinfo userInfo)
{
var user = new ApplicationUser
var applicationUser = new ApplicationUser
{
UserName = userInfo.Name,
Email = userInfo.Email,
@@ -71,12 +60,18 @@ public class IdentityService(
var password = Guid.NewGuid().ToString("N")[..32];
var result = await userManager.CreateAsync(user, password);
var identityResult = await userManager.CreateAsync(applicationUser, password);
return (result.ToApplicationResult(), user.Id);
var applicationResult = identityResult.ToApplicationResult();
var result = new Result<string>(applicationResult.Succeeded, applicationResult.Errors);
result.Value = applicationUser.Id;
return result;
}
public async Task<Result> CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
public async Task<Result<string>> CreateUserAsync(string email, string userName, string firstName, string lastName, string password)
{
var applicationUser = new ApplicationUser
{
@@ -88,7 +83,52 @@ public class IdentityService(
var response = await userManager.CreateAsync(applicationUser, password);
return response.ToApplicationResult();
var result = new Result<string>(response.Succeeded, response.ToApplicationResult().Errors);
result.Value = applicationUser.Id;
return result;
}
public async 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)
{
var applicationUser = await userManager.FindByIdAsync(id);
if (applicationUser is null) return Result<string>.Failure(new[] { "User not found." });
applicationUser.FirstName = firstName;
applicationUser.LastName = lastName;
applicationUser.Occupation = occupation;
applicationUser.PhoneNumber = phoneNumber;
applicationUser.BirthDate = birthDate;
applicationUser.Country = country;
applicationUser.City = city;
applicationUser.Address = address;
applicationUser.About = about;
applicationUser.Description = description;
applicationUser.SocialNetworks = new SocialNetworks()
{
FacebookUrl = socialNetworks.FacebookUrl,
InstagramUrl = socialNetworks.InstagramUrl,
XUrl = socialNetworks.XUrl,
LinkedInUrl = socialNetworks.LinkedInUrl,
TikTokUrl = socialNetworks.TikTokUrl,
YoutubeUrl = socialNetworks.YoutubeUrl,
RedditUrl = socialNetworks.RedditUrl,
YourWebsiteUrl = socialNetworks.YourWebsiteUrl
};
var response = await userManager.UpdateAsync(applicationUser);
var applicationResult = response.ToApplicationResult();
var result = new Result<string>(applicationResult.Succeeded,
applicationResult.Errors);
result.Value = id;
return result;
}
public async Task<UserModel?> FindUserByIdAsync(string id)
@@ -97,13 +137,32 @@ public class IdentityService(
if (response == null) return null;
var userModel = new UserModel()
var userModel = new UserModel
{
Id = response.Id,
UserName = response.UserName,
FirstName = response.FirstName,
LastName = response.LastName,
Email = response.Email,
Occupation = response.Occupation,
Phone = response.PhoneNumber,
BirthDate = response.BirthDate,
Country = response.Country,
City = response.City,
Address = response.Address,
About = response.About,
Description = response.Description,
SocialNetworks = new SocialNetworksModel
{
FacebookUrl = response.SocialNetworks.FacebookUrl,
InstagramUrl = response.SocialNetworks.InstagramUrl,
XUrl = response.SocialNetworks.XUrl,
LinkedInUrl = response.SocialNetworks.LinkedInUrl,
TikTokUrl = response.SocialNetworks.TikTokUrl,
YoutubeUrl = response.SocialNetworks.YoutubeUrl,
RedditUrl = response.SocialNetworks.RedditUrl,
YourWebsiteUrl = response.SocialNetworks.YourWebsiteUrl,
}
};
return userModel;