68 lines
1.7 KiB
C#
68 lines
1.7 KiB
C#
using Hutopy.Infrastructure.Security;
|
|
using Hutopy.Modules.Creators.Data;
|
|
|
|
namespace Hutopy.Modules.Creators.Features;
|
|
|
|
[PublicAPI]
|
|
public record ChangePhoneNumberRequest(
|
|
Guid CreatorId,
|
|
string? PhoneNumber);
|
|
|
|
[PublicAPI]
|
|
public sealed class ChangePhoneNumberRequestValidator : Validator<ChangePhoneNumberRequest>
|
|
{
|
|
public ChangePhoneNumberRequestValidator()
|
|
{
|
|
RuleFor(x => x.CreatorId)
|
|
.NotEmpty()
|
|
.WithMessage("Creator ID is required");
|
|
|
|
RuleFor(x => x.PhoneNumber)
|
|
.Must(phone => phone == null || !string.IsNullOrWhiteSpace(phone))
|
|
.WithMessage("Phone number cannot be empty if provided");
|
|
}
|
|
}
|
|
|
|
[PublicAPI]
|
|
public class ChangePhoneNumberHandler(
|
|
CreatorsDbContext context)
|
|
: Endpoint<ChangePhoneNumberRequest>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Post("/api/creators/{CreatorId}/phone");
|
|
Options(o => o.WithTags("Creators"));
|
|
}
|
|
|
|
public override async Task HandleAsync(
|
|
ChangePhoneNumberRequest request,
|
|
CancellationToken ct)
|
|
{
|
|
var creator = await context
|
|
.Creators
|
|
.Include(c => c.Presentation)
|
|
.SingleOrDefaultAsync(
|
|
c => c.Id == request.CreatorId,
|
|
cancellationToken: ct);
|
|
|
|
if (creator is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
// Check if the current user is the creator
|
|
if (creator.CreatedBy != User.GetUserId())
|
|
{
|
|
await SendUnauthorizedAsync(ct);
|
|
return;
|
|
}
|
|
|
|
creator.Presentation.PhoneNumber = request.PhoneNumber?.Trim();
|
|
|
|
await context.SaveChangesAsync(ct);
|
|
|
|
await SendOkAsync(ct);
|
|
}
|
|
}
|