using FastEndpoints; using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Contact.Request; using PyroFetes.DTO.Contact.Response; namespace PyroFetes.Endpoints.Contact; public class CreateContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endpoint { public override void Configure() { Post("/contacts"); AllowAnonymous(); } public override async Task HandleAsync(CreateContactDto req, CancellationToken ct) { Models.Customer? databaseCustomer = await pyroFetesDbContext.Customers.SingleOrDefaultAsync(x => x.Id == req.CustomerId, cancellationToken: ct); if (databaseCustomer == null) { await Send.NotFoundAsync(ct); Console.WriteLine("Customer not found"); return; } Models.Contact contact = new() { LastName = req.LastName, FirstName = req.FirstName, PhoneNumber = req.PhoneNumber, Email = req.Email, Address = req.Address, City = req.City, Role = req.Role, CustomerId = databaseCustomer.Id, }; pyroFetesDbContext.Add(contact); await pyroFetesDbContext.SaveChangesAsync(ct); GetContactDto response = new() { LastName = contact.LastName, FirstName = contact.FirstName, PhoneNumber = contact.PhoneNumber, Email = contact.Email, Address = contact.Address, City = contact.City, Role = contact.Role, CustomerId = contact.CustomerId, }; await Send.OkAsync(response, ct); } }