From 7bb21be0e6c18131cef4a62f225051af87ba012a Mon Sep 17 00:00:00 2001 From: sanchezvem Date: Mon, 27 Oct 2025 12:55:25 +0100 Subject: [PATCH] add patch from user for the password --- .../User/PatchUserPasswordEndpoint.cs | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 PyroFetes/Endpoints/User/PatchUserPasswordEndpoint.cs diff --git a/PyroFetes/Endpoints/User/PatchUserPasswordEndpoint.cs b/PyroFetes/Endpoints/User/PatchUserPasswordEndpoint.cs new file mode 100644 index 0000000..4049b89 --- /dev/null +++ b/PyroFetes/Endpoints/User/PatchUserPasswordEndpoint.cs @@ -0,0 +1,39 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.User.Request; +using PyroFetes.DTO.User.Response; + +namespace PyroFetes.Endpoints.User; + +public class PatchUserPasswordEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Patch("/api/users/{@Id}/Password", x => new { x.Id }); + AllowAnonymous(); + } + + public override async Task HandleAsync(PatchUserPasswordDto req, CancellationToken ct) + { + var user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + if (user == null) + { + await Send.NotFoundAsync(ct); + return; + } + + user.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + user.Salt); + await database.SaveChangesAsync(ct); + + GetUserDto responseDto = new() + { + Id = user.Id, + Name = user.Name, + Password = user.Password, + Salt = user.Salt, + Email = user.Email, + Fonction = user.Fonction + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file