diff --git a/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs new file mode 100644 index 0000000..6232481 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/CreateSupplierEndpoint.cs @@ -0,0 +1,43 @@ +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; +using FastEndpoints; + +namespace PyroFetes.Endpoints.Supplier; + +public class CreateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Post("/api/suppliers"); + } + + public override async Task HandleAsync(CreateSupplierDto req, CancellationToken ct) + { + var supplier = new Models.Supplier() + { + Name = req.Name, + Email = req.Email, + Phone = req.Phone, + Address = req.Address, + City = req.City, + ZipCode = req.ZipCode, + DeliveryDelay = req.DeliveryDelay + }; + + database.Suppliers.Add(supplier); + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs new file mode 100644 index 0000000..28b4487 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/DeleteSupplierEndpoint.cs @@ -0,0 +1,33 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; + +namespace PyroFetes.Endpoints.Supplier; + +public class DeleteSupplierRequest +{ + public int Id { get; set; } +} + +public class DeleteSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Delete("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(DeleteSupplierRequest req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + database.Suppliers.Remove(supplier); + await database.SaveChangesAsync(ct); + + await Send.NoContentAsync(ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs new file mode 100644 index 0000000..b550c85 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/GetAllSuppliersEndpoint.cs @@ -0,0 +1,32 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.PurchaseProduct.Response; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class GetAllSuppliersEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/api/suppliers"); + } + + public override async Task HandleAsync(CancellationToken ct) + { + var supplier = await database.Suppliers + .Select(supplier => new GetSupplierDto() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }).ToListAsync(ct); + + await Send.OkAsync(supplier, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs new file mode 100644 index 0000000..40b6e8e --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/GetSupplierEndpoint.cs @@ -0,0 +1,43 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class GetSupplierRequest +{ + public int Id { get; set; } +} + +public class GetSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(GetSupplierRequest req, CancellationToken ct) + { + var supplier = await database.Suppliers + .SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs b/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs new file mode 100644 index 0000000..ea31903 --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/PatchSupplierDeleveryDelayEndpoint.cs @@ -0,0 +1,36 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class PatchSupplierDeleveryDelayEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Get("/api/supplier/{@Id}/DeleveryDalay", x => new {x.Id}); + } + + public override async Task HandleAsync(PatchSupplierDeliveryDelayDto req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + supplier.DeliveryDelay = req.DeliveryDelay; + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + DeliveryDelay = supplier.DeliveryDelay, + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs new file mode 100644 index 0000000..ab68ebb --- /dev/null +++ b/PyroFetes/Endpoints/Supplier/UpdateSupplierEndpoint.cs @@ -0,0 +1,48 @@ +using FastEndpoints; +using Microsoft.EntityFrameworkCore; +using PyroFetes.DTO.Supplier.Request; +using PyroFetes.DTO.Supplier.Response; + +namespace PyroFetes.Endpoints.Supplier; + +public class UpdateSupplierEndpoint(PyroFetesDbContext database) : Endpoint +{ + public override void Configure() + { + Put("/api/suppliers/{@Id}", x => new {x.Id}); + } + + public override async Task HandleAsync(UpdateSupplierDto req, CancellationToken ct) + { + var supplier = await database.Suppliers.SingleOrDefaultAsync(x => x.Id == req.Id, ct); + + if (supplier == null) + { + await Send.NotFoundAsync(ct); + return; + } + + supplier.Name = req.Name; + supplier.Email = req.Email; + supplier.Phone = req.Phone; + supplier.Address = req.Address; + supplier.City = req.City; + supplier.ZipCode = req.ZipCode; + supplier.DeliveryDelay = req.DeliveryDelay; + await database.SaveChangesAsync(ct); + + GetSupplierDto responseDto = new() + { + Id = supplier.Id, + Name = supplier.Name, + Email = supplier.Email, + Phone = supplier.Phone, + Address = supplier.Address, + City = supplier.City, + ZipCode = supplier.ZipCode, + DeliveryDelay = supplier.DeliveryDelay + }; + + await Send.OkAsync(responseDto, ct); + } +} \ No newline at end of file