From cb55989c715be7a73222fcd3dcb25ad9b2687bb6 Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Fri, 5 Jul 2024 00:13:26 -0400 Subject: [PATCH 1/2] Fix some update / upload stuff with images. Added more info to minimalUser --- .../Users/Commands/UploadBannerPicture.cs | 10 ++++++++++ .../Users/Commands/UploadProfilePicture.cs | 10 ++++++++++ src/Application/Users/Commands/UploadWebsiteIcon.cs | 11 +++++++++++ .../Users/Queries/GetMinimalUser/GetMinimalUser.cs | 2 ++ .../Users/Queries/GetMinimalUser/MinimalUserDto.cs | 2 ++ src/Web/Endpoints/UpdateMyUser.cs | 12 ++++++------ 6 files changed, 41 insertions(+), 6 deletions(-) diff --git a/src/Application/Users/Commands/UploadBannerPicture.cs b/src/Application/Users/Commands/UploadBannerPicture.cs index ae8e783..e99f28a 100644 --- a/src/Application/Users/Commands/UploadBannerPicture.cs +++ b/src/Application/Users/Commands/UploadBannerPicture.cs @@ -3,15 +3,25 @@ using Hutopy.Application.Common.Interfaces; namespace Hutopy.Application.Users.Commands; +/// +/// Upload a banner picture. If the user has the url already, set the BannerPictureUrl in the user only without upload. +/// public class UploadBannerPictureCommand : IRequest { public required Stream BannerPicture { get; init; } + public string BannerPictureUrl { get; init; } = string.Empty; } public class UploadBannerPictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler { public async Task 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(); diff --git a/src/Application/Users/Commands/UploadProfilePicture.cs b/src/Application/Users/Commands/UploadProfilePicture.cs index 154ace7..ae15d31 100644 --- a/src/Application/Users/Commands/UploadProfilePicture.cs +++ b/src/Application/Users/Commands/UploadProfilePicture.cs @@ -3,15 +3,25 @@ using Hutopy.Application.Common.Interfaces; namespace Hutopy.Application.Users.Commands; +/// +/// Upload a profile picture. If the user has the url already, set the ProfilePictureUrl in the user only without upload. +/// public class UploadProfilePictureCommand : IRequest { public required Stream ProfilePicture { get; init; } + public string ProfilePictureUrl { get; init; } = string.Empty; } public class UploadProfilePictureCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler { public async Task 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(); diff --git a/src/Application/Users/Commands/UploadWebsiteIcon.cs b/src/Application/Users/Commands/UploadWebsiteIcon.cs index 26caa25..31f3f5f 100644 --- a/src/Application/Users/Commands/UploadWebsiteIcon.cs +++ b/src/Application/Users/Commands/UploadWebsiteIcon.cs @@ -3,15 +3,26 @@ using Hutopy.Application.Common.Interfaces; namespace Hutopy.Application.Users.Commands; +/// +/// Upload a website icon. If the user has the url already, set the WebsitePictureUrl in the user only without upload. +/// public class UploadWebsiteIconCommand : IRequest { public required Stream WebsiteIcon { get; init; } + + public string WebsitePictureUrl { get; init; } = string.Empty; } public class UploadWebsiteIconCommandHandler(IIdentityService identityService, IAzureBlobStorageService azureBlobStorageService) : IRequestHandler { public async Task 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(); diff --git a/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs b/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs index 124e9cf..454e8ae 100644 --- a/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs +++ b/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs @@ -31,9 +31,11 @@ public class GetMinimalUserQueryHandler( var user = new MinimalUserDto { + 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(), diff --git a/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs b/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs index 7648fa7..a38fd64 100644 --- a/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs +++ b/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs @@ -4,9 +4,11 @@ namespace Hutopy.Application.Users.Queries.GetMinimalUser; public class MinimalUserDto { + 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(); diff --git a/src/Web/Endpoints/UpdateMyUser.cs b/src/Web/Endpoints/UpdateMyUser.cs index 9aca4dc..8f13133 100644 --- a/src/Web/Endpoints/UpdateMyUser.cs +++ b/src/Web/Endpoints/UpdateMyUser.cs @@ -19,21 +19,21 @@ public class UpdateMyUser : EndpointGroupBase return await sender.Send(command); } - private static async Task UpdateCurrentUserProfilePicture(ISender sender, Stream stream) + private static async Task 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 UpdateCurrentUserBannerPicture(ISender sender, Stream stream) + private static async Task 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 UpdateCurrentUserWebsiteIcon(ISender sender, Stream stream) + private static async Task 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); } } From b80374649bb21637887d8e71fb7c04ffda798661 Mon Sep 17 00:00:00 2001 From: Dominic Villemure Date: Fri, 5 Jul 2024 00:15:54 -0400 Subject: [PATCH 2/2] Renamed minimalUser to user --- .../GetMinimalUser.cs => GetUser/GetUser.cs} | 12 ++++++------ .../MinimalUserDto.cs => GetUser/UserDto.cs} | 4 ++-- src/Web/Endpoints/Users.cs | 6 +++--- 3 files changed, 11 insertions(+), 11 deletions(-) rename src/Application/Users/Queries/{GetMinimalUser/GetMinimalUser.cs => GetUser/GetUser.cs} (76%) rename src/Application/Users/Queries/{GetMinimalUser/MinimalUserDto.cs => GetUser/UserDto.cs} (86%) diff --git a/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs b/src/Application/Users/Queries/GetUser/GetUser.cs similarity index 76% rename from src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs rename to src/Application/Users/Queries/GetUser/GetUser.cs index 454e8ae..3b36caa 100644 --- a/src/Application/Users/Queries/GetMinimalUser/GetMinimalUser.cs +++ b/src/Application/Users/Queries/GetUser/GetUser.cs @@ -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 +public record GetUserQuery : IRequest { public string? UserId { get; set; } = string.Empty; public string? UserName { get; set; } = string.Empty; }; -public class GetMinimalUserQueryHandler( +public class GetUserQueryHandler( IIdentityService identityService ) - : IRequestHandler + : IRequestHandler { - public async Task Handle(GetMinimalUserQuery request, CancellationToken cancellationToken) + public async Task Handle(GetUserQuery request, CancellationToken cancellationToken) { UserModel? identityUser = null; @@ -29,7 +29,7 @@ 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, diff --git a/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs b/src/Application/Users/Queries/GetUser/UserDto.cs similarity index 86% rename from src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs rename to src/Application/Users/Queries/GetUser/UserDto.cs index a38fd64..509743d 100644 --- a/src/Application/Users/Queries/GetMinimalUser/MinimalUserDto.cs +++ b/src/Application/Users/Queries/GetUser/UserDto.cs @@ -1,8 +1,8 @@ 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; } diff --git a/src/Web/Endpoints/Users.cs b/src/Web/Endpoints/Users.cs index bcfa964..39bc408 100644 --- a/src/Web/Endpoints/Users.cs +++ b/src/Web/Endpoints/Users.cs @@ -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 CreateUser(ISender sender, CreateUserCommand command) @@ -19,7 +19,7 @@ public class Users : EndpointGroupBase return await sender.Send(command); } - private static async Task GetMinimalUser(ISender sender, [AsParameters] GetMinimalUserQuery query) + private static async Task GetUser(ISender sender, [AsParameters] GetUserQuery query) { return await sender.Send(query); }