Fusion des patchs Tel et Email en un Patch Contact

This commit is contained in:
oistig
2026-03-19 16:25:34 +01:00
parent 00b79a58d0
commit ecd038f020
3 changed files with 47 additions and 30 deletions

View File

@@ -1,7 +1,8 @@
namespace Knots.DTO.User;
public class UpdateUserEmailDto
public class UpdateUserContactDto
{
public int Id { get; set; }
public string? Email { get; set; }
public string? Tel { get; set; }
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUserContactEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserContactDto>
{
public override void Configure()
{
Patch("/users/{@Id}/contact/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserContactDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
if (databaseUser.Email != req.Email)
{
databaseUser.Email = req.Email;
}
else
{
databaseUser.Email = databaseUser.Email;
}
if (databaseUser.Tel != req.Tel)
{
databaseUser.Tel = req.Tel;
}else
{
databaseUser.Tel = databaseUser.Tel;
}
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -1,29 +0,0 @@
using FastEndpoints;
using Knots.DTO.User;
using Microsoft.EntityFrameworkCore;
namespace Knots.Endpoints.User;
public class PatchUserEmailEndpoint(KnotsDbContext knotsDbContext) : Endpoint<UpdateUserEmailDto>
{
public override void Configure()
{
Patch("/users/{@Id}/email/", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(UpdateUserEmailDto req, CancellationToken ct)
{
Models.User? databaseUser = await knotsDbContext.Users.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
if (databaseUser is null)
{
await Send.NotFoundAsync(ct);
return;
}
databaseUser.Email = req.Email;
await knotsDbContext.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}