diff --git a/PyroFetes/DTO/Contact/Request/CreateContactDto.cs b/PyroFetes/DTO/Contact/Request/CreateContactDto.cs index f52343d..d163b2c 100644 --- a/PyroFetes/DTO/Contact/Request/CreateContactDto.cs +++ b/PyroFetes/DTO/Contact/Request/CreateContactDto.cs @@ -7,5 +7,7 @@ public class CreateContactDto public string? PhoneNumber { get; set; } public string? Email { get; set; } public string? Address { get; set; } + public string? City { get; set; } public string? Role { get; set; } + public int? CustomerId { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Contact/Request/UpdateContactDto.cs b/PyroFetes/DTO/Contact/Request/UpdateContactDto.cs index 42ffd3d..500b3ea 100644 --- a/PyroFetes/DTO/Contact/Request/UpdateContactDto.cs +++ b/PyroFetes/DTO/Contact/Request/UpdateContactDto.cs @@ -8,5 +8,7 @@ public class UpdateContactDto public string? PhoneNumber { get; set; } public string? Email { get; set; } public string? Address { get; set; } + public string? City { get; set; } public string? Role { get; set; } + public int? CustomerTypeId { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Contact/Response/GetContactDto.cs b/PyroFetes/DTO/Contact/Response/GetContactDto.cs index 38a25d8..c8e2cb9 100644 --- a/PyroFetes/DTO/Contact/Response/GetContactDto.cs +++ b/PyroFetes/DTO/Contact/Response/GetContactDto.cs @@ -8,5 +8,7 @@ public class GetContactDto public string? PhoneNumber { get; set; } public string? Email { get; set; } public string? Address { get; set; } + public string? City { get; set; } public string? Role { get; set; } + public int? CustomerId { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Customer/Request/CreateCustomerDto.cs b/PyroFetes/DTO/Customer/Request/CreateCustomerDto.cs index 6a90c75..6627c54 100644 --- a/PyroFetes/DTO/Customer/Request/CreateCustomerDto.cs +++ b/PyroFetes/DTO/Customer/Request/CreateCustomerDto.cs @@ -3,4 +3,5 @@ namespace PyroFetes.DTO.Customer.Request; public class CreateCustomerDto { public string? Note { get; set; } + public int CustomerTypeId { get; set; } } \ No newline at end of file diff --git a/PyroFetes/DTO/Customer/Response/GetCustomerDto.cs b/PyroFetes/DTO/Customer/Response/GetCustomerDto.cs index 53a00c5..54fb346 100644 --- a/PyroFetes/DTO/Customer/Response/GetCustomerDto.cs +++ b/PyroFetes/DTO/Customer/Response/GetCustomerDto.cs @@ -4,4 +4,6 @@ public class GetCustomerDto { public int Id { get; set; } public string? Note { get; set; } + + public int CustomerTypeId { get; set; } } \ No newline at end of file diff --git a/PyroFetes/Endpoints/Contact/CreateContactEndpoint.cs b/PyroFetes/Endpoints/Contact/CreateContactEndpoint.cs index 7710266..768cb2f 100644 --- a/PyroFetes/Endpoints/Contact/CreateContactEndpoint.cs +++ b/PyroFetes/Endpoints/Contact/CreateContactEndpoint.cs @@ -1,4 +1,5 @@ using FastEndpoints; +using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Contact.Request; using PyroFetes.DTO.Contact.Response; @@ -15,6 +16,15 @@ public class CreateContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endp 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, @@ -22,7 +32,9 @@ public class CreateContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endp 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); @@ -34,7 +46,9 @@ public class CreateContactEndpoint(PyroFetesDbContext pyroFetesDbContext) : Endp PhoneNumber = contact.PhoneNumber, Email = contact.Email, Address = contact.Address, + City = contact.City, Role = contact.Role, + CustomerId = contact.CustomerId, }; await Send.OkAsync(response, ct); diff --git a/PyroFetes/Endpoints/Customer/CreateCustomerEndpoint.cs b/PyroFetes/Endpoints/Customer/CreateCustomerEndpoint.cs index c2d88ee..c454f85 100644 --- a/PyroFetes/Endpoints/Customer/CreateCustomerEndpoint.cs +++ b/PyroFetes/Endpoints/Customer/CreateCustomerEndpoint.cs @@ -1,4 +1,5 @@ using FastEndpoints; +using Microsoft.EntityFrameworkCore; using PyroFetes.DTO.Customer.Request; using PyroFetes.DTO.Customer.Response; @@ -15,9 +16,20 @@ public class CreateCustomerEndpoint(PyroFetesDbContext pyroFetesDbContext) : End public override async Task HandleAsync(CreateCustomerDto req, CancellationToken ct) { - var customer = new Models.Customer + Models.CustomerType? databaseCustomerType = await pyroFetesDbContext.CustomerTypes.SingleOrDefaultAsync(x => x.Id == req.CustomerTypeId, cancellationToken: ct); + + if (databaseCustomerType == null) { - Note = req.Note + await Send.NotFoundAsync(ct); + Console.WriteLine("Customer Type not found"); + return; + } + + Models.Customer customer = new() + { + Note = req.Note, + CustomerTypeId = databaseCustomerType.Id, + }; pyroFetesDbContext.Add(customer); await pyroFetesDbContext.SaveChangesAsync(ct); @@ -25,7 +37,8 @@ public class CreateCustomerEndpoint(PyroFetesDbContext pyroFetesDbContext) : End GetCustomerDto response = new GetCustomerDto() { Id = customer.Id, - Note = customer.Note + Note = customer.Note, + CustomerTypeId = customer.CustomerTypeId }; await Send.OkAsync(response, ct); diff --git a/PyroFetes/Models/ContactCustomer.cs b/PyroFetes/Models/ContactCustomer.cs new file mode 100644 index 0000000..9d110d3 --- /dev/null +++ b/PyroFetes/Models/ContactCustomer.cs @@ -0,0 +1,14 @@ +using System.ComponentModel.DataAnnotations; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Models; + +[PrimaryKey(nameof(ContactId), nameof(CustomerId))] +public class ContactCustomer +{ + [Required] public int ContactId { get; set; } + [Required] public int CustomerId { get; set; } + + public Contact? Contact { get; set; } + public Customer? Customer { get; set; } +} \ No newline at end of file