This commit is contained in:
commit
0e79a8eb5e
43 changed files with 1282 additions and 0 deletions
40
src/Core/Users/UpdateUserEndpoint.cs
Normal file
40
src/Core/Users/UpdateUserEndpoint.cs
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
using TestWeb.Infra;
|
||||
using TestWeb.Utils;
|
||||
|
||||
namespace TestWeb.Core.Users;
|
||||
|
||||
public static class UpdateUserEndpoint
|
||||
{
|
||||
public record Request(string DisplayName);
|
||||
|
||||
public class RequestValidator : AbstractValidator<Request>
|
||||
{
|
||||
public RequestValidator()
|
||||
{
|
||||
RuleFor(x => x.DisplayName).NotEmpty().MaximumLength(250);
|
||||
}
|
||||
}
|
||||
|
||||
public static async Task<Results<Ok, NotFound>> Handle(
|
||||
int id,
|
||||
Request request,
|
||||
AppDbContext db,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var result = await db.Users
|
||||
.Where(x => x.Id == id)
|
||||
.SingleOrDefaultAsync(ct);
|
||||
|
||||
if (result == null)
|
||||
{
|
||||
return TypedResults.NotFound();
|
||||
}
|
||||
|
||||
result.DisplayName = request.DisplayName;
|
||||
result.LastUpdated = DateTime.UtcNow;
|
||||
|
||||
await db.SaveChangesAsync(ct);
|
||||
|
||||
return TypedResults.Ok();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue