Creating supplier's endpoints

This commit is contained in:
2025-11-06 17:30:03 +01:00
parent 1c898f9144
commit be0a6a4004
6 changed files with 235 additions and 0 deletions

View File

@@ -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<CreateSupplierDto, GetSupplierDto>
{
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);
}
}

View File

@@ -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<DeleteSupplierRequest>
{
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);
}
}

View File

@@ -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<List<GetSupplierDto>>
{
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);
}
}

View File

@@ -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<GetSupplierRequest, GetSupplierDto>
{
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);
}
}

View File

@@ -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<PatchSupplierDeliveryDelayDto, GetSupplierDto>
{
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);
}
}

View File

@@ -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<UpdateSupplierDto, GetSupplierDto>
{
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);
}
}