Compare commits

2 Commits

Author SHA1 Message Date
3ada21adae Creating all setting's endpoints 2025-10-16 23:50:00 +01:00
4f12911263 creating endpoints from PurchaseOrder 2025-10-16 23:28:27 +01:00
11 changed files with 334 additions and 17 deletions

View File

@@ -1,7 +1,10 @@
namespace PyroFetes.DTO.PurchaseOrder.Response; using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.DTO.PurchaseOrder.Response;
public class GetPurchaseOrderDto public class GetPurchaseOrderDto
{ {
public int Id { get; set; } public int Id { get; set; }
public string? PurchaseConditions { get; set; } public string? PurchaseConditions { get; set; }
public List<GetPurchaseProductDto>? GetPurchaseProductDto { get; set; }
} }

View File

@@ -0,0 +1,41 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class DeletePurchaseOrderRequest
{
public int Id { get; set; }
}
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseOrderRequest>
{
public override void Configure()
{
Delete("/api/purchaseOrders/{Id}", x => new {x.Id});
AllowAnonymous();
}
public override async Task HandleAsync(DeletePurchaseOrderRequest req, CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.Include(po => po.PurchaseProducts)
.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
if (purchaseOrder == null)
{
await Send.NotFoundAsync(ct);
return;
}
if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any())
{
database.PurchaseProducts.RemoveRange(purchaseOrder.PurchaseProducts);
}
database.PurchaseOrders.Remove(purchaseOrder);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,45 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class GetAllPurchaseOrderEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetPurchaseOrderDto>>
{
public override void Configure()
{
Get("/api/purchaseOrders");
}
public override async Task HandleAsync(CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.Include(p => p.PurchaseProducts)
.Select(purchaseOrder => new GetPurchaseOrderDto()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
})
.ToListAsync(ct);
await Send.OkAsync(purchaseOrder, ct);
}
}

View File

@@ -0,0 +1,56 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.PurchaseOrder.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.PurchaseOrder;
public class GetPurchaseOrderRequest
{
public int Id { get; set; }
}
public class GetPurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<GetPurchaseOrderRequest, GetPurchaseOrderDto>
{
public override void Configure()
{
Get("/api/purchaseOrders/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(GetPurchaseOrderRequest req, CancellationToken ct)
{
var purchaseOrder = await database.PurchaseOrders
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (purchaseOrder == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetPurchaseOrderDto responseDto = new()
{
Id = purchaseOrder.Id,
PurchaseConditions = purchaseOrder.PurchaseConditions,
GetPurchaseProductDto = purchaseOrder.PurchaseProducts
.Select(p => new GetPurchaseProductDto
{
ProductId = p.ProductId,
ProductReferences = p.Product.Reference,
ProductName = p.Product.Name,
ProductDuration = p.Product.Duration,
ProductCaliber = p.Product.Caliber,
ProductApprovalNumber = p.Product.ApprovalNumber,
ProductWeight = p.Product.Weight,
ProductNec = p.Product.Nec,
ProductImage = p.Product.Image,
ProductLink = p.Product.Link,
ProductMinimalQuantity = p.Product.MinimalQuantity,
PurchaseOrderId = p.PurchaseOrderId,
Quantity = p.Quantity,
}).ToList()
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -3,37 +3,32 @@ using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.PurchaseProduct; namespace PyroFetes.Endpoints.PurchaseProduct;
public class DeletePurchaseOrderRequest public class DeletePurchaseProductRequest
{ {
public int Id { get; set; } public int ProductId { get; set; }
public int PurchaseOrderId { get; set; }
} }
public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseOrderRequest> public class DeletePurchaseOrderEndpoint(PyroFetesDbContext database) : Endpoint<DeletePurchaseProductRequest>
{ {
public override void Configure() public override void Configure()
{ {
Delete("/api/purchaseOrders/{Id}", x => new {x.Id}); Delete("/api/purchaseProducts/{ProductId}/{PurchaseOrderId}", x => new {x.ProductId, x.PurchaseOrderId});
AllowAnonymous(); AllowAnonymous();
} }
public override async Task HandleAsync(DeletePurchaseOrderRequest req, CancellationToken ct) public override async Task HandleAsync(DeletePurchaseProductRequest req, CancellationToken ct)
{ {
var purchaseOrder = await database.PurchaseOrders var purchaseProduct = await database.PurchaseProducts
.Include(po => po.PurchaseProducts) .SingleOrDefaultAsync(po => po.ProductId == req.ProductId && po.PurchaseOrderId == req.PurchaseOrderId, ct);
.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
if (purchaseOrder == null) if (purchaseProduct == null)
{ {
await Send.NotFoundAsync(ct); await Send.NotFoundAsync(ct);
return; return;
} }
if (purchaseOrder.PurchaseProducts != null && purchaseOrder.PurchaseProducts.Any()) database.PurchaseProducts.Remove(purchaseProduct);
{
database.PurchaseProducts.RemoveRange(purchaseOrder.PurchaseProducts);
}
database.PurchaseOrders.Remove(purchaseOrder);
await database.SaveChangesAsync(ct); await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct); await Send.NoContentAsync(ct);

View File

@@ -9,7 +9,7 @@ public class PatchPurchaseProductQuantityEndpoint(PyroFetesDbContext database) :
{ {
public override void Configure() public override void Configure()
{ {
Patch("/api/purchaseOrders/{ProductId}/{PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId }); Patch("/api/purchaseProducts/{ProductId}/{PurchaseOrderId}/Quantity", x => new { x.ProductId, x.PurchaseOrderId });
AllowAnonymous(); AllowAnonymous();
} }

View File

@@ -0,0 +1,32 @@
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
using FastEndpoints;
namespace PyroFetes.Endpoints.Setting;
public class CreateSettingEndpoint(PyroFetesDbContext database) : Endpoint<CreateSettingDto, GetSettingDto>
{
public override void Configure()
{
Post("/api/setting");
}
public override async Task HandleAsync(CreateSettingDto req, CancellationToken ct)
{
var setting = new Models.Setting()
{
ElectronicSignature = req.ElectronicSignature,
Logo = req.Logo
};
database.Settings.Add(setting);
await database.SaveChangesAsync(ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,33 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
namespace PyroFetes.Endpoints.Setting;
public class DeleteSettingRequest
{
public int Id { get; set; }
}
public class DeleteSettingEndpoint(PyroFetesDbContext database) : Endpoint<DeleteSettingRequest>
{
public override void Configure()
{
Delete("/api/setting/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(DeleteSettingRequest req, CancellationToken ct)
{
var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (setting == null)
{
await Send.NotFoundAsync(ct);
return;
}
database.Settings.Remove(setting);
await database.SaveChangesAsync(ct);
await Send.NoContentAsync(ct);
}
}

View File

@@ -0,0 +1,38 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Response;
namespace PyroFetes.Endpoints.Setting;
public class GetSettingRequest
{
public int Id { get; set; }
}
public class GetSettingEndpoint(PyroFetesDbContext database) : Endpoint<GetSettingRequest, GetSettingDto>
{
public override void Configure()
{
Get("/api/setting/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(GetSettingRequest req, CancellationToken ct)
{
var setting = await database.Settings
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (setting == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,37 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
namespace PyroFetes.Endpoints.Setting;
public class PatchSettingElectronicSignatureEndpoint(PyroFetesDbContext database) : Endpoint<PatchSettingElectronicSignatureDto, GetSettingDto>
{
public override void Configure()
{
Get("/api/setting/{@Id}/ElectronicSignature", x => new {x.Id});
}
public override async Task HandleAsync(PatchSettingElectronicSignatureDto req, CancellationToken ct)
{
var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (setting == null)
{
await Send.NotFoundAsync(ct);
return;
}
setting.ElectronicSignature = req.ElectronicSignature;
await database.SaveChangesAsync(ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,37 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.SettingDTO.Request;
using PyroFetes.DTO.SettingDTO.Response;
namespace PyroFetes.Endpoints.Setting;
public class PatchSettingLogoEndpoint(PyroFetesDbContext database) : Endpoint<PatchSettingLogoDto, GetSettingDto>
{
public override void Configure()
{
Get("/api/setting/{@Id}/Logo", x => new {x.Id});
}
public override async Task HandleAsync(PatchSettingLogoDto req, CancellationToken ct)
{
var setting = await database.Settings.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (setting == null)
{
await Send.NotFoundAsync(ct);
return;
}
setting.Logo = req.Logo;
await database.SaveChangesAsync(ct);
GetSettingDto responseDto = new()
{
Id = setting.Id,
ElectronicSignature = setting.ElectronicSignature,
Logo = setting.Logo
};
await Send.OkAsync(responseDto, ct);
}
}