Adds ChangePortrait for User

This commit is contained in:
2024-09-04 13:52:47 -04:00
parent f12418bc79
commit 3e341e0019
5 changed files with 103 additions and 48 deletions

View File

@@ -1,35 +0,0 @@
using Hutopy.Application.AzureBlobStorage.Constants;
using Hutopy.Application.Common.Interfaces;
using Microsoft.AspNetCore.Http;
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<IResult>
{
public required IFormFile File { get; init; }
}
public class UploadProfilePictureCommandHandler(
IHttpContextAccessor contextAccessor,
IIdentityService identityService,
IBlobStorage blobStorage) : IRequestHandler<UploadProfilePictureCommand, IResult>
{
public async Task<IResult> Handle(UploadProfilePictureCommand request, CancellationToken ct)
{
var identityUser = await identityService.GetCurrentUserAsync();
var url = await blobStorage.UploadFileAsync(
ContainerNames.Users,
$"{identityUser.Id}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}",
request.File.OpenReadStream(),
request.File.ContentType,
ct);
await identityService.UpdateCurrentUserPortraitUrlAsync(url);
return Results.Ok(url);
}
}

View File

@@ -9,7 +9,6 @@ public class UpdateMyUser : EndpointGroupBase
{ {
app.MapGroup(this) app.MapGroup(this)
.RequireAuthorization() .RequireAuthorization()
.MapPost(UpdateCurrentUserProfilePicture, "/profile-picture")
.MapPatch("/profile", UpdateCurrentUser); .MapPatch("/profile", UpdateCurrentUser);
} }
@@ -17,12 +16,4 @@ public class UpdateMyUser : EndpointGroupBase
{ {
return await sender.Send(command); return await sender.Send(command);
} }
private static async Task<IResult> UpdateCurrentUserProfilePicture(
ISender sender,
IFormFile formFile)
{
var command = new UploadProfilePictureCommand { File = formFile };
return await sender.Send(command);
}
} }

View File

@@ -9,11 +9,15 @@ public record ChangeBannerRequest(
Guid CreatorId, Guid CreatorId,
IFormFile File); IFormFile File);
[PublicAPI]
public record ChangeBannerResponse(
string BlobUrl);
[PublicAPI] [PublicAPI]
public class ChangeBannerHandler( public class ChangeBannerHandler(
ContentDbContext context, ContentDbContext context,
IBlobStorage blobStorage) IBlobStorage blobStorage)
: Endpoint<ChangeBannerRequest> : Endpoint<ChangeBannerRequest, ChangeBannerResponse>
{ {
public override void Configure() public override void Configure()
{ {
@@ -22,7 +26,9 @@ public class ChangeBannerHandler(
AllowFileUploads(); AllowFileUploads();
} }
public override async Task HandleAsync(ChangeBannerRequest request, CancellationToken ct) public override async Task HandleAsync(
ChangeBannerRequest request,
CancellationToken ct)
{ {
var creator = await context var creator = await context
.Creators .Creators
@@ -49,6 +55,8 @@ public class ChangeBannerHandler(
await context.SaveChangesAsync(ct); await context.SaveChangesAsync(ct);
await SendOkAsync(blobUrl, ct); await SendOkAsync(
new ChangeBannerResponse(blobUrl),
ct);
} }
} }

View File

@@ -9,6 +9,21 @@ public record ChangeLogoRequest(
Guid CreatorId, Guid CreatorId,
IFormFile File); IFormFile File);
[PublicAPI]
public sealed class ChangeLogoRequestValidator : Validator<ChangeLogoRequest>
{
public ChangeLogoRequestValidator()
{
RuleFor(x => x.CreatorId)
.NotNull()
.NotEmpty();
RuleFor(x => x.File)
.NotNull()
.NotEmpty();
}
}
[PublicAPI] [PublicAPI]
public class ChangeLogoHandler( public class ChangeLogoHandler(
ContentDbContext context, ContentDbContext context,
@@ -22,7 +37,9 @@ public class ChangeLogoHandler(
AllowFileUploads(); AllowFileUploads();
} }
public override async Task HandleAsync(ChangeLogoRequest request, CancellationToken ct) public override async Task HandleAsync(
ChangeLogoRequest request,
CancellationToken ct)
{ {
var creator = await context var creator = await context
.Creators .Creators

View File

@@ -0,0 +1,74 @@
using Hutopy.Application.AzureBlobStorage.Constants;
using Hutopy.Application.Common.Interfaces;
using Hutopy.Infrastructure.Identity;
using Hutopy.Web.Common;
namespace Hutopy.Web.Features.Users.Handlers;
[PublicAPI]
public record ChangePortraitRequest(
IFormFile File);
[PublicAPI]
public record ChangePortraitResponse(
string BlobUrl);
[PublicAPI]
public sealed class ChangePortraitRequestValidator : Validator<ChangePortraitRequest>
{
public ChangePortraitRequestValidator()
{
RuleFor(x => x.File)
.NotNull()
.NotEmpty();
}
}
[PublicAPI]
public class ChangePortraitHandler(
ApplicationUserManager userManager,
IBlobStorage blobStorage)
: Endpoint<ChangePortraitRequest, ChangePortraitResponse>
{
public override void Configure()
{
Post("/api/users/portrait");
Options(o => o.WithTags("Users"));
AllowFileUploads();
}
public override async Task HandleAsync(
ChangePortraitRequest request,
CancellationToken ct)
{
var user = await userManager.FindByIdAsync(HttpContext.User.GetUserId().ToString());
if (user is null)
{
await SendNotFoundAsync(ct);
return;
}
var blobUrl = await blobStorage.UploadFileAsync(
ContainerNames.Users,
$"{user.Id}/{SubDirectoryNames.Profile}/{CommonFileNames.ProfilePicture}",
request.File.OpenReadStream(),
request.File.ContentType,
ct);
user.PortraitUrl = blobUrl;
var result = await userManager.UpdateAsync(user);
if (result.Succeeded)
{
await SendOkAsync(
new ChangePortraitResponse(blobUrl),
ct);
}
else
{
await SendUnauthorizedAsync(ct);
}
}
}