First Commit 09/10
This commit is contained in:
49
PyroFetes/Endpoints/Contact/UpdateContactRequest.cs
Normal file
49
PyroFetes/Endpoints/Contact/UpdateContactRequest.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using PyroFetes.DTO.Contact.Request;
|
||||
using PyroFetes.DTO.Contact.Response;
|
||||
|
||||
namespace PyroFetes.Endpoints.Contact;
|
||||
|
||||
public class UpdateContactRequest(PyroFetesDbContext pyroFetesDbContext) : Endpoint <UpdateContactDto, GetContactDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put ("/api/contacts/{@Id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateContactDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Contact? databaseContact = await pyroFetesDbContext.Contacts.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (databaseContact == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
else
|
||||
{
|
||||
databaseContact.LastName = req.LastName;
|
||||
databaseContact.FirstName = req.FirstName;
|
||||
databaseContact.PhoneNumber = req.PhoneNumber;
|
||||
databaseContact.Email = req.Email;
|
||||
databaseContact.Address = req.Address;
|
||||
databaseContact.Role = req.Role;
|
||||
}
|
||||
await pyroFetesDbContext.SaveChangesAsync(ct);
|
||||
|
||||
GetContactDto dto = new()
|
||||
{
|
||||
Id = databaseContact.Id,
|
||||
LastName = req.LastName,
|
||||
FirstName = req.FirstName,
|
||||
PhoneNumber = req.PhoneNumber,
|
||||
Email = req.Email,
|
||||
Address = req.Address,
|
||||
Role = req.Role,
|
||||
};
|
||||
|
||||
await Send.OkAsync(dto, ct);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user