49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
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);
|
|
}
|
|
} |