diff --git a/PyroFetes/DTO/WareHouse/Response/GetWareHouseDto.cs b/PyroFetes/DTO/WareHouse/Response/GetWareHouseDto.cs new file mode 100644 index 0000000..7b820dc --- /dev/null +++ b/PyroFetes/DTO/WareHouse/Response/GetWareHouseDto.cs @@ -0,0 +1,7 @@ +namespace PyroFetes.DTO.WareHouse.Response; + +public class GetWareHouseDto +{ + public int Id { get; set; } + public string? Name { get; set; } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/GetAllWarehouseEndpoint.cs b/PyroFetes/Endpoints/GetAllWarehouseEndpoint.cs new file mode 100644 index 0000000..ac2c7a2 --- /dev/null +++ b/PyroFetes/Endpoints/GetAllWarehouseEndpoint.cs @@ -0,0 +1,19 @@ +using FastEndpoints; +using PyroFetes.DTO.WareHouse.Response; +using PyroFetes.Repositories; + +namespace PyroFetes.Endpoints; + +public class GetAllWarehouseEndpoint(WareHouseRepository wareHouseRepository, AutoMapper.IMapper mapper) : EndpointWithoutRequest> +{ + public override void Configure() + { + Get("/wareHouses/"); + AllowAnonymous(); + } + + public override async Task HandleAsync(CancellationToken ct) + { + await Send.OkAsync(await wareHouseRepository.ProjectToListAsync(ct), ct); + } +} \ No newline at end of file diff --git a/PyroFetes/Endpoints/WareHouseProducts/PatchWareHouseProductQuantityEndpoint.cs b/PyroFetes/Endpoints/WareHouseProducts/PatchWareHouseProductQuantityEndpoint.cs index e346e9b..2c43f8d 100644 --- a/PyroFetes/Endpoints/WareHouseProducts/PatchWareHouseProductQuantityEndpoint.cs +++ b/PyroFetes/Endpoints/WareHouseProducts/PatchWareHouseProductQuantityEndpoint.cs @@ -3,11 +3,12 @@ using PyroFetes.DTO.WareHouseProduct.Request; using PyroFetes.DTO.WareHouseProduct.Response; using PyroFetes.Models; using PyroFetes.Repositories; +using PyroFetes.Specifications.WareHouse; using PyroFetes.Specifications.WarehouseProducts; namespace PyroFetes.Endpoints.WareHouseProducts; -public class PatchWareHouseProductQuantityEndpoint(WarehouseProductsRepository warehouseProductsRepository) : Endpoint +public class PatchWareHouseProductQuantityEndpoint(WarehouseProductsRepository warehouseProductsRepository, WareHouseRepository wareHouseRepository , AutoMapper.IMapper mapper) : Endpoint { public override void Configure() { @@ -18,16 +19,21 @@ public class PatchWareHouseProductQuantityEndpoint(WarehouseProductsRepository w public override async Task HandleAsync(PatchWareHouseProductQuantityDto req, CancellationToken ct) { WarehouseProduct? wareHouseProduct = await warehouseProductsRepository.FirstOrDefaultAsync(new GetWarehouseProductByProductIdSpec(req.ProductId, req.WareHouseId), ct); + Warehouse? warehouse = await wareHouseRepository.SingleOrDefaultAsync(new GetWareHouseByIdSpec(req.WareHouseId), ct); - if (wareHouseProduct is null) + if (warehouse is null) { await Send.NotFoundAsync(ct); return; } - - wareHouseProduct.Quantity = req.Quantity; - await warehouseProductsRepository.SaveChangesAsync(ct); - + + if (wareHouseProduct is null) await warehouseProductsRepository.AddAsync(mapper.Map(req), ct); + else + { + wareHouseProduct.Quantity = req.Quantity; + await warehouseProductsRepository.SaveChangesAsync(ct); + } + await Send.NoContentAsync(ct); } } \ No newline at end of file diff --git a/PyroFetes/Program.cs b/PyroFetes/Program.cs index ac75892..bf236f3 100644 --- a/PyroFetes/Program.cs +++ b/PyroFetes/Program.cs @@ -49,6 +49,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Ajout des services builder.Services.AddScoped(); diff --git a/PyroFetes/Repositories/WareHouseRepository.cs b/PyroFetes/Repositories/WareHouseRepository.cs new file mode 100644 index 0000000..c393c28 --- /dev/null +++ b/PyroFetes/Repositories/WareHouseRepository.cs @@ -0,0 +1,5 @@ +using PyroFetes.Models; + +namespace PyroFetes.Repositories; + +public class WareHouseRepository(PyroFetesDbContext pyrofetesContext, AutoMapper.IMapper mapper) : PyrofetesRepository(pyrofetesContext, mapper); \ No newline at end of file diff --git a/PyroFetes/Specifications/WareHouse/GetWareHouseByIdSpec.cs b/PyroFetes/Specifications/WareHouse/GetWareHouseByIdSpec.cs new file mode 100644 index 0000000..edf79c7 --- /dev/null +++ b/PyroFetes/Specifications/WareHouse/GetWareHouseByIdSpec.cs @@ -0,0 +1,13 @@ +using Ardalis.Specification; +using PyroFetes.Models; + +namespace PyroFetes.Specifications.WareHouse; + +public class GetWareHouseByIdSpec : SingleResultSpecification +{ + public GetWareHouseByIdSpec(int id) + { + Query + .Where(x => x.Id == id); + } +} \ No newline at end of file