Creating of git for project ApiEfCoreLibrary
This commit is contained in:
43
ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs
Normal file
43
ApiEfCoreLibrary/Endpoints/User/UpdateUserEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using ApiEfCoreLibrary.DTO.User.Request;
|
||||
using ApiEfCoreLibrary.DTO.User.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ApiEfCoreLibrary.Endpoints.User;
|
||||
|
||||
public class UpdateUserEndpoint(LibraryDbContext database) : Endpoint<UpdateUserDto, GetUserDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/users/{@Id}", x => new {x.Id});
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateUserDto req, CancellationToken ct)
|
||||
{
|
||||
var user = await database.Users.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
|
||||
|
||||
if (user == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
user.Name = req.Name;
|
||||
user.FirstName = req.FirstName;
|
||||
user.Email = req.Email;
|
||||
user.BirthDate = req.BirthDate;
|
||||
await database.SaveChangesAsync(ct);
|
||||
|
||||
GetUserDto responseDto = new()
|
||||
{
|
||||
Id = user.Id,
|
||||
Name = user.Name,
|
||||
FirstName = user.FirstName,
|
||||
Email = user.Email,
|
||||
BirthDate = user.BirthDate
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user