67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Socialize.Api.Data;
|
|
using Socialize.Api.Modules.Organizations.Data;
|
|
using Socialize.Api.Modules.Organizations.Services;
|
|
|
|
namespace Socialize.Api.Modules.Organizations.Handlers;
|
|
|
|
public record UpdateOrganizationRequest(
|
|
string Name);
|
|
|
|
public class UpdateOrganizationRequestValidator
|
|
: Validator<UpdateOrganizationRequest>
|
|
{
|
|
public UpdateOrganizationRequestValidator()
|
|
{
|
|
RuleFor(x => x.Name).NotEmpty().MaximumLength(256);
|
|
}
|
|
}
|
|
|
|
public class UpdateOrganizationHandler(
|
|
AppDbContext dbContext,
|
|
OrganizationAccessService organizationAccessService)
|
|
: Endpoint<UpdateOrganizationRequest, OrganizationDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("/api/organizations/{organizationId:guid}");
|
|
Options(o => o.WithTags("Organizations"));
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateOrganizationRequest request, CancellationToken ct)
|
|
{
|
|
ArgumentNullException.ThrowIfNull(request);
|
|
|
|
Guid organizationId = Route<Guid>("organizationId");
|
|
|
|
Organization? organization = await dbContext.Organizations
|
|
.SingleOrDefaultAsync(candidate => candidate.Id == organizationId, ct);
|
|
if (organization is null)
|
|
{
|
|
await SendNotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
if (!await organizationAccessService.HasOrganizationPermissionAsync(
|
|
User,
|
|
organizationId,
|
|
OrganizationPermissions.ManageOrganizationSettings,
|
|
ct))
|
|
{
|
|
await SendForbiddenAsync(ct);
|
|
return;
|
|
}
|
|
|
|
organization.Name = request.Name.Trim();
|
|
await dbContext.SaveChangesAsync(ct);
|
|
|
|
IReadOnlyCollection<string> currentUserPermissions = await organizationAccessService.GetUserOrganizationPermissionsAsync(
|
|
User,
|
|
organizationId,
|
|
ct);
|
|
|
|
await SendOkAsync(OrganizationDto.FromOrganization(organization, currentUserPermissions), ct);
|
|
}
|
|
}
|