forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
57
PyroFetes/Endpoints/Product/CreateProductEndpoint.cs
Normal file
57
PyroFetes/Endpoints/Product/CreateProductEndpoint.cs
Normal file
@@ -0,0 +1,57 @@
|
||||
using API.DTO.Product.Request;
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class CreateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductDto, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/products");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateProductDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Product product = new ()
|
||||
{
|
||||
References = req.References,
|
||||
Name = req.Name!,
|
||||
Duration = req.Duration,
|
||||
Caliber = req.Caliber,
|
||||
ApprovalNumber = req.ApprovalNumber,
|
||||
Weight = req.Weight,
|
||||
Nec = req.Nec,
|
||||
SellingPrice = req.SellingPrice,
|
||||
Image = req.Image!,
|
||||
Link = req.Link!,
|
||||
ProductCategoryId = req.ProductCategoryId,
|
||||
ClassificationId = req.ClassificationId
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.Products.Add(product);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
Console.WriteLine("Product créé avec succès !");
|
||||
|
||||
GetProductDto responseDto = new ()
|
||||
{
|
||||
Id = product.Id,
|
||||
Reference = req.References,
|
||||
Name = req.Name,
|
||||
Duration = req.Duration,
|
||||
Caliber = req.Caliber,
|
||||
ApprovalNumber = req.ApprovalNumber,
|
||||
Weight = req.Weight,
|
||||
Nec = req.Nec,
|
||||
SellingPrice = req.SellingPrice,
|
||||
Image = req.Image,
|
||||
Link = req.Link,
|
||||
ProductCategoryId = req.ProductCategoryId,
|
||||
ClassificationId = req.ClassificationId
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
37
PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs
Normal file
37
PyroFetes/Endpoints/Product/DeleteProductEndpoint.cs
Normal file
@@ -0,0 +1,37 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class DeleteProductRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteProductRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Product? productToDelete = await pyrofetesdbcontext
|
||||
.Products
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productToDelete == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.Products.Remove(productToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
40
PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs
Normal file
40
PyroFetes/Endpoints/Product/GetAllProductsEndpoint.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class GetAllProductsEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/products");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetProductDto> responseDto = await pyrofetesdbcontext.Products
|
||||
.Select(p => new GetProductDto()
|
||||
{
|
||||
Id = p.Id,
|
||||
Reference = p.References,
|
||||
Name = p.Name,
|
||||
Duration = p.Duration,
|
||||
Caliber = p.Caliber,
|
||||
ApprovalNumber = p.ApprovalNumber,
|
||||
Weight = p.Weight,
|
||||
Nec = p.Nec,
|
||||
SellingPrice = p.SellingPrice,
|
||||
Image = p.Image,
|
||||
Link = p.Link,
|
||||
ClassificationId = p.ClassificationId,
|
||||
ClassificationLabel = p.Classification!.Label,
|
||||
ProductCategoryId = p.ProductCategoryId,
|
||||
ProductCategoryLabel = p.ProductCategory!.Label,
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
55
PyroFetes/Endpoints/Product/GetProductEndpoint.cs
Normal file
55
PyroFetes/Endpoints/Product/GetProductEndpoint.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class GetProductRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class GetProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetProductRequest, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/product/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Product? product = await pyrofetesdbcontext
|
||||
.Products.Include(product => product.Classification).Include(product => product.ProductCategory)
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (product == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetProductDto responseDto = new()
|
||||
{
|
||||
Id = product.Id,
|
||||
Reference = product.References,
|
||||
Name = product.Name,
|
||||
Duration = product.Duration,
|
||||
Caliber = product.Caliber,
|
||||
ApprovalNumber = product.ApprovalNumber,
|
||||
Weight = product.Weight,
|
||||
Nec = product.Nec,
|
||||
SellingPrice = product.SellingPrice,
|
||||
Image = product.Image,
|
||||
Link = product.Link,
|
||||
ClassificationId = product.ClassificationId,
|
||||
ClassificationLabel = product.Classification!.Label,
|
||||
ProductCategoryId = product.ProductCategoryId,
|
||||
ProductCategoryLabel = product.ProductCategory!.Label,
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
58
PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs
Normal file
58
PyroFetes/Endpoints/Product/UpdateProductEndpoint.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using API.DTO.Product.Request;
|
||||
using API.DTO.Product.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Product;
|
||||
|
||||
public class UpdateProductEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<UpdateProductDto, GetProductDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/products/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Product? productToEdit = await pyrofetesdbcontext
|
||||
.Products
|
||||
.SingleOrDefaultAsync(p => p.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productToEdit == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun produit avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
productToEdit.References = req.Reference;
|
||||
productToEdit.Name = req.Name;
|
||||
productToEdit.Duration = req.Duration;
|
||||
productToEdit.Caliber = req.Caliber;
|
||||
productToEdit.ApprovalNumber = req.ApprovalNumber;
|
||||
productToEdit.Weight = req.Weight;
|
||||
productToEdit.Nec = req.Nec;
|
||||
productToEdit.SellingPrice = req.SellingPrice;
|
||||
productToEdit.Image = req.Image;
|
||||
productToEdit.Link = req.Link;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetProductDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Reference = req.Reference,
|
||||
Name = req.Name,
|
||||
Duration = req.Duration,
|
||||
Caliber = req.Caliber,
|
||||
ApprovalNumber = req.ApprovalNumber,
|
||||
Weight = req.Weight,
|
||||
Nec = req.Nec,
|
||||
SellingPrice = req.SellingPrice,
|
||||
Image = req.Image,
|
||||
Link = req.Link
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user