created product's endpoints

This commit is contained in:
2025-11-07 17:41:50 +01:00
parent f8b3c51435
commit 97a7c6811c
7 changed files with 187 additions and 3 deletions

View File

@@ -2,7 +2,7 @@ namespace PyroFetes.DTO.Product.Request;
public class CreateProductDto
{
public int References { get; set; }
public string? References { get; set; }
public string? Name { get; set; }
public decimal Duration {get; set;}
public decimal Caliber { get; set; }

View File

@@ -3,7 +3,7 @@ namespace PyroFetes.DTO.Product.Request;
public class UpdateProductDto
{
public int Id { get; set; }
public int References { get; set; }
public string? References { get; set; }
public string? Name { get; set; }
public decimal Duration {get; set;}
public decimal Caliber { get; set; }

View File

@@ -3,7 +3,7 @@ namespace PyroFetes.DTO.Product.Response;
public class GetProductDto
{
public int Id { get; set; }
public int References { get; set; }
public string? References { get; set; }
public string? Name { get; set; }
public decimal Duration {get; set;}
public decimal Caliber { get; set; }

View File

@@ -0,0 +1,36 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.Product;
public class GetAllProductsEndpoint(PyroFetesDbContext database) : EndpointWithoutRequest<List<GetProductDto>>
{
public override void Configure()
{
Get("/api/products");
}
public override async Task HandleAsync(CancellationToken ct)
{
var product = await database.Products
.Select(product => new GetProductDto()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
})
.ToListAsync(ct);
await Send.OkAsync(product, ct);
}
}

View File

@@ -0,0 +1,48 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.Product;
public class GetProductRequest
{
public int Id { get; set; }
}
public class GetProductEndpoint(PyroFetesDbContext database) : Endpoint<GetProductRequest, GetProductDto>
{
public override void Configure()
{
Get("/api/products/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
{
var product = await database.Products
.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
return;
}
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,46 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
using PyroFetes.DTO.PurchaseProduct.Response;
namespace PyroFetes.Endpoints.Product;
public class PatchProductMinimalStockEndpoint(PyroFetesDbContext database)
: Endpoint<PatchProductMinimalStockDto, GetProductDto>
{
public override void Configure()
{
Patch("/api/products/{@Id}/MinimalStock", x => new { x.Id });
AllowAnonymous();
}
public override async Task HandleAsync(PatchProductMinimalStockDto req, CancellationToken ct)
{
var product = await database.Products.SingleOrDefaultAsync(po => po.Id == req.Id, ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
return;
}
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
}
}

View File

@@ -0,0 +1,54 @@
using FastEndpoints;
using Microsoft.EntityFrameworkCore;
using PyroFetes.DTO.Product.Request;
using PyroFetes.DTO.Product.Response;
namespace PyroFetes.Endpoints.Product;
public class UpdateProductEndpoint(PyroFetesDbContext database) : Endpoint<UpdateProductDto, GetProductDto>
{
public override void Configure()
{
Put("/api/products/{@Id}", x => new {x.Id});
}
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
{
var product = await database.Products.SingleOrDefaultAsync(x => x.Id == req.Id, ct);
if (product == null)
{
await Send.NotFoundAsync(ct);
return;
}
product.Reference = req.References;
product.Name = req.Name;
product.Duration = req.Duration;
product.Caliber = req.Caliber;
product.ApprovalNumber = req.ApprovalNumber;
product.Weight = req.Weight;
product.Nec = req.Nec;
product.Image = req.Image;
product.Link = req.Link;
product.MinimalQuantity = req.MinimalQuantity;
await database.SaveChangesAsync(ct);
GetProductDto responseDto = new()
{
Id = product.Id,
References = product.Reference,
Name = product.Name,
Duration = product.Duration,
Caliber = product.Caliber,
ApprovalNumber = product.ApprovalNumber,
Weight = product.Weight,
Nec = product.Nec,
Image = product.Image,
Link = product.Link,
MinimalQuantity = product.MinimalQuantity,
};
await Send.OkAsync(responseDto, ct);
}
}