forked from sanchezvem/PyroFetes
AJout des DTO et endpoint sur le nouveau git
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
using API.DTO.ProductCategory.Request;
|
||||
using API.DTO.ProductCategory.Response;
|
||||
using FastEndpoints;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class CreateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<CreateProductCategoryDto, GetProductCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/productcategories");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateProductCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory productCategory = new ()
|
||||
{
|
||||
Label = req.Label,
|
||||
|
||||
};
|
||||
|
||||
pyrofetesdbcontext.ProductCategories.Add(productCategory);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
Console.WriteLine("Category créé avec succès !");
|
||||
|
||||
GetProductCategoryDto responseDto = new ()
|
||||
{
|
||||
Id = productCategory.Id,
|
||||
Label = productCategory.Label
|
||||
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
@@ -0,0 +1,36 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class DeleteProductCategoryRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class DeleteProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<DeleteProductCategoryRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/productcategories/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteProductCategoryRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategoryToDelete = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productCategoryToDelete == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
pyrofetesdbcontext.ProductCategories.Remove(productCategoryToDelete);
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
using API.DTO.ProductCategory.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class GetAllProductCategoriesEndpoint(PyroFetesDbContext pyrofetesdbcontext) : EndpointWithoutRequest<List<GetProductCategoryDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/productcategories");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetProductCategoryDto> responseDto = await pyrofetesdbcontext.ProductCategories
|
||||
.Select(pc => new GetProductCategoryDto()
|
||||
{
|
||||
Id = pc.Id,
|
||||
Label = pc.Label
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
@@ -0,0 +1,42 @@
|
||||
using API.DTO.ProductCategory.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class GetProductCategoryRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
|
||||
public class GetProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<GetProductCategoryRequest, GetProductCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetProductCategoryRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategory = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productCategory == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetProductCategoryDto responseDto = new()
|
||||
{
|
||||
Id = productCategory.Id,
|
||||
Label = productCategory.Label
|
||||
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
@@ -0,0 +1,39 @@
|
||||
using API.DTO.ProductCategory.Request;
|
||||
using API.DTO.ProductCategory.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.ProductCategory;
|
||||
|
||||
public class UpdateProductCategoryEndpoint(PyroFetesDbContext pyrofetesdbcontext) :Endpoint<UpdateProductCategoryDto, GetProductCategoryDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/productcategory/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateProductCategoryDto req, CancellationToken ct)
|
||||
{
|
||||
Models.ProductCategory? productCategoryToEdit = await pyrofetesdbcontext
|
||||
.ProductCategories
|
||||
.SingleOrDefaultAsync(pc => pc.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (productCategoryToEdit == null)
|
||||
{
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
productCategoryToEdit.Label = req.Label;
|
||||
await pyrofetesdbcontext.SaveChangesAsync(ct);
|
||||
|
||||
GetProductCategoryDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user