46 lines
1.3 KiB
C#
46 lines
1.3 KiB
C#
using ApiEfCoreLibrary.DTO.Login.Request;
|
|
using ApiEfCoreLibrary.DTO.Login.Response;
|
|
using ApiEfCoreLibrary.DTO.Book.Response;
|
|
using FastEndpoints;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ApiEfCoreLibrary.Endpoints.Login;
|
|
|
|
public class UpdateLoginEndpoint(LibraryDbContext database) : Endpoint<UpdateLoginDto, GetLoginDto>
|
|
{
|
|
public override void Configure()
|
|
{
|
|
Put("/api/logins/{@Id}", x => new {x.Id});
|
|
AllowAnonymous();
|
|
}
|
|
|
|
public override async Task HandleAsync(UpdateLoginDto req, CancellationToken ct)
|
|
{
|
|
var login = await database.Logins.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
|
|
|
if (login == null)
|
|
{
|
|
await Send.NotFoundAsync(ct);
|
|
return;
|
|
}
|
|
|
|
string? salt = BCrypt.Net.BCrypt.GenerateSalt(24);
|
|
|
|
login.Username = req.Username;
|
|
login.FullName = req.FullName;
|
|
login.Password = BCrypt.Net.BCrypt.HashPassword(req.Password + salt);
|
|
login.Salt = salt;
|
|
await database.SaveChangesAsync(ct);
|
|
|
|
GetLoginDto responseDto = new()
|
|
{
|
|
Id = login.Id,
|
|
Username = login.Username,
|
|
FullName = login.FullName,
|
|
Password = login.Password,
|
|
Salt = login.Salt
|
|
};
|
|
|
|
await Send.OkAsync(responseDto, ct);
|
|
}
|
|
} |