Merged PR 93: Update and upload + getUser

This commit is contained in:
Dominic Villemure
2024-07-05 04:35:12 +00:00
7 changed files with 52 additions and 17 deletions

View File

@@ -3,15 +3,25 @@ using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands;
/// <summary>
/// Upload a banner picture. If the user has the url already, set the BannerPictureUrl in the user only without upload.
/// </summary>
public class UploadBannerPictureCommand : IRequest<string>
{
public required Stream BannerPicture { get; init; }
public string BannerPictureUrl { get; init; } = string.Empty;
}
public class UploadBannerPictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadBannerPictureCommand, string>
{
public async Task<string> Handle(UploadBannerPictureCommand request, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(request.BannerPictureUrl))
{
await identityService.UpdateCurrentUserBannerPictureUrlAsync(request.BannerPictureUrl);
return request.BannerPictureUrl;
}
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "").ToString();

View File

@@ -3,15 +3,25 @@ using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands;
/// <summary>
/// Upload a profile picture. If the user has the url already, set the ProfilePictureUrl in the user only without upload.
/// </summary>
public class UploadProfilePictureCommand : IRequest<string>
{
public required Stream ProfilePicture { get; init; }
public string ProfilePictureUrl { get; init; } = string.Empty;
}
public class UploadProfilePictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadProfilePictureCommand, string>
{
public async Task<string> Handle(UploadProfilePictureCommand request, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(request.ProfilePictureUrl))
{
await identityService.UpdateCurrentUserProfilePictureUrlAsync(request.ProfilePictureUrl);
return request.ProfilePictureUrl;
}
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "").ToString();

View File

@@ -3,15 +3,26 @@ using Hutopy.Application.Common.Interfaces;
namespace Hutopy.Application.Users.Commands;
/// <summary>
/// Upload a website icon. If the user has the url already, set the WebsitePictureUrl in the user only without upload.
/// </summary>
public class UploadWebsiteIconCommand : IRequest<string>
{
public required Stream WebsiteIcon { get; init; }
public string WebsitePictureUrl { get; init; } = string.Empty;
}
public class UploadWebsiteIconCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler<UploadWebsiteIconCommand, string>
{
public async Task<string> Handle(UploadWebsiteIconCommand request, CancellationToken cancellationToken)
{
if (!string.IsNullOrEmpty(request.WebsitePictureUrl))
{
await identityService.UpdateCurrentUserWebsiteIconUrlAsync(request.WebsitePictureUrl);
return request.WebsitePictureUrl;
}
var identityUser = await identityService.GetCurrentUserAsync();
var currentUserId = new Guid(identityUser?.Id ?? "").ToString();

View File

@@ -1,20 +1,20 @@
using Hutopy.Application.Common.Interfaces;
using Hutopy.Application.Common.Models;
namespace Hutopy.Application.Users.Queries.GetMinimalUser;
namespace Hutopy.Application.Users.Queries.GetUser;
public record GetMinimalUserQuery : IRequest<MinimalUserDto>
public record GetUserQuery : IRequest<UserDto>
{
public string? UserId { get; set; } = string.Empty;
public string? UserName { get; set; } = string.Empty;
};
public class GetMinimalUserQueryHandler(
public class GetUserQueryHandler(
IIdentityService identityService
)
: IRequestHandler<GetMinimalUserQuery, MinimalUserDto>
: IRequestHandler<GetUserQuery, UserDto>
{
public async Task<MinimalUserDto> Handle(GetMinimalUserQuery request, CancellationToken cancellationToken)
public async Task<UserDto> Handle(GetUserQuery request, CancellationToken cancellationToken)
{
UserModel? identityUser = null;
@@ -29,11 +29,13 @@ public class GetMinimalUserQueryHandler(
identityUser = await identityService.GetUserByUserNameAsync(request.UserName);
}
var user = new MinimalUserDto
var user = new UserDto
{
Id = identityUser?.Id ?? string.Empty,
FirstName = identityUser?.FirstName ?? string.Empty,
LastName = identityUser?.LastName ?? string.Empty,
UserName = identityUser?.UserName ?? string.Empty,
Occupation = identityUser?.Occupation ?? string.Empty,
SocialNetworks = identityUser?.SocialNetworks ?? new(),
ProfileColors = identityUser?.ProfileColors ?? new(),
StoredDataUrls = identityUser?.StoredDataUrls ?? new(),

View File

@@ -1,12 +1,14 @@
using Hutopy.Application.Users.Models;
namespace Hutopy.Application.Users.Queries.GetMinimalUser;
namespace Hutopy.Application.Users.Queries.GetUser;
public class MinimalUserDto
public class UserDto
{
public required string Id { get; init; }
public required string FirstName { get; init; }
public required string LastName { get; init; }
public required string UserName { get; init; } = String.Empty;
public required string Occupation { get; init; } = String.Empty;
public SocialNetworksModel SocialNetworks { get; init; } = new();
public ProfileColorsModel ProfileColors { get; init; } = new();

View File

@@ -19,21 +19,21 @@ public class UpdateMyUser : EndpointGroupBase
return await sender.Send(command);
}
private static async Task<string> UpdateCurrentUserProfilePicture(ISender sender, Stream stream)
private static async Task<string> UpdateCurrentUserProfilePicture(ISender sender, Stream stream, string url = "")
{
var command = new UploadProfilePictureCommand { ProfilePicture = stream };
var command = new UploadProfilePictureCommand { ProfilePicture = stream, ProfilePictureUrl = url};
return await sender.Send(command);
}
private static async Task<string> UpdateCurrentUserBannerPicture(ISender sender, Stream stream)
private static async Task<string> UpdateCurrentUserBannerPicture(ISender sender, Stream stream, string url = "")
{
var command = new UploadBannerPictureCommand { BannerPicture = stream };
var command = new UploadBannerPictureCommand { BannerPicture = stream, BannerPictureUrl = url};
return await sender.Send(command);
}
private static async Task<string> UpdateCurrentUserWebsiteIcon(ISender sender, Stream stream)
private static async Task<string> UpdateCurrentUserWebsiteIcon(ISender sender, Stream stream, string url = "")
{
var command = new UploadWebsiteIconCommand { WebsiteIcon = stream };
var command = new UploadWebsiteIconCommand { WebsiteIcon = stream, WebsitePictureUrl = url};
return await sender.Send(command);
}
}

View File

@@ -1,5 +1,5 @@
using Hutopy.Application.Users.Commands;
using Hutopy.Application.Users.Queries.GetMinimalUser;
using Hutopy.Application.Users.Queries.GetUser;
namespace Hutopy.Web.Endpoints;
@@ -11,7 +11,7 @@ public class Users : EndpointGroupBase
.MapPost(CreateUser)
.MapPost(Login, "/login")
.MapPost(UploadProfilePicture, "/upload-profile-picture")
.MapGet(GetMinimalUser);
.MapGet(GetUser);
}
private static async Task<Guid> CreateUser(ISender sender, CreateUserCommand command)
@@ -19,7 +19,7 @@ public class Users : EndpointGroupBase
return await sender.Send(command);
}
private static async Task<MinimalUserDto> GetMinimalUser(ISender sender, [AsParameters] GetMinimalUserQuery query)
private static async Task<UserDto> GetUser(ISender sender, [AsParameters] GetUserQuery query)
{
return await sender.Send(query);
}