forked from sanchezvem/PyroFetes
09/10
This commit is contained in:
36
PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs
Normal file
36
PyroFetes/Endpoints/Material/CreateMaterialEndpoint.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using PyroFetes.DTO.Material.Request;
|
||||
using PyroFetes.DTO.Material.Response;
|
||||
using FastEndpoints;
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class CreateMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<CreateMaterialDto, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Post("/api/material/create");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CreateMaterialDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Material material = new()
|
||||
{
|
||||
Label = req.Label,
|
||||
Quantity = req.Quantity,
|
||||
WarehouseId = req.WarehouseId,
|
||||
};
|
||||
|
||||
appDbContext.Materials.Add(material);
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
Console.WriteLine("Material added");
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
{
|
||||
Id = material.Id,
|
||||
WarehouseId = material.WarehouseId,
|
||||
Label = req.Label,
|
||||
};
|
||||
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
35
PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs
Normal file
35
PyroFetes/Endpoints/Material/DeleteMaterialEndpoint.cs
Normal file
@@ -0,0 +1,35 @@
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
public class DeleteMaterialRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
public class DeleteMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<DeleteMaterialRequest>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Delete("/api/materials/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(DeleteMaterialRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Material? materialToDelete = await appDbContext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(a => a.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (materialToDelete == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun matériel avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
appDbContext.Materials.Remove(materialToDelete);
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
|
||||
await Send.NoContentAsync(ct);
|
||||
}
|
||||
}
|
||||
28
PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs
Normal file
28
PyroFetes/Endpoints/Material/GetAllMaterialsEndpoint.cs
Normal file
@@ -0,0 +1,28 @@
|
||||
using PyroFetes.DTO.Material.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class GetAllMaterialsEndpoint(PyroFetesDbContext appDbContext) : EndpointWithoutRequest<List<GetMaterialDto>>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/material");
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(CancellationToken ct)
|
||||
{
|
||||
List<GetMaterialDto> responseDto = await appDbContext.Materials
|
||||
.Select(a => new GetMaterialDto
|
||||
{
|
||||
Id = a.Id,
|
||||
Label = a.Label,
|
||||
Quantity = a.Quantity,
|
||||
WarehouseId = a.WarehouseId,
|
||||
}
|
||||
).ToListAsync(ct);
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
}
|
||||
43
PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs
Normal file
43
PyroFetes/Endpoints/Material/GetMaterialEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using PyroFetes.DTO.Material.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.AspNetCore.Authentication;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class GetMaterialRequest
|
||||
{
|
||||
public int Id { get; set; }
|
||||
}
|
||||
|
||||
public class GetMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<GetMaterialRequest, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Get("/api/material/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(GetMaterialRequest req, CancellationToken ct)
|
||||
{
|
||||
Models.Material? material = await appDbContext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (material == null)
|
||||
{
|
||||
Console.WriteLine("Aucun matériel avec l'ID {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
}
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
{
|
||||
Id = material.Id,
|
||||
Label = material.Label,
|
||||
WarehouseId = material.WarehouseId,
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
|
||||
}
|
||||
}
|
||||
43
PyroFetes/Endpoints/Material/UpdateMaterialEndpoint.cs
Normal file
43
PyroFetes/Endpoints/Material/UpdateMaterialEndpoint.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using PyroFetes.DTO.Material.Request;
|
||||
using PyroFetes.DTO.Material.Response;
|
||||
using FastEndpoints;
|
||||
using Microsoft.AspNetCore.Server.Kestrel;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace PyroFetes.Endpoints.Material;
|
||||
|
||||
public class UpdateMaterialEndpoint(PyroFetesDbContext appDbContext) : Endpoint<UpdateMaterialDto, GetMaterialDto>
|
||||
{
|
||||
public override void Configure()
|
||||
{
|
||||
Put("/api/material/{@id}", x => new { x.Id });
|
||||
AllowAnonymous();
|
||||
}
|
||||
|
||||
public override async Task HandleAsync(UpdateMaterialDto req, CancellationToken ct)
|
||||
{
|
||||
Models.Material? materialToEdit = await appDbContext
|
||||
.Materials
|
||||
.SingleOrDefaultAsync(x => x.Id == req.Id, cancellationToken: ct);
|
||||
|
||||
if (materialToEdit == null)
|
||||
{
|
||||
Console.WriteLine($"Aucun matériel avec l'id {req.Id} trouvé.");
|
||||
await Send.NotFoundAsync(ct);
|
||||
return;
|
||||
|
||||
}
|
||||
materialToEdit.Label = req.Label;
|
||||
materialToEdit.WarehouseId = req.WarehouseId;
|
||||
await appDbContext.SaveChangesAsync(ct);
|
||||
|
||||
GetMaterialDto responseDto = new()
|
||||
{
|
||||
Id = req.Id,
|
||||
Label = req.Label,
|
||||
WarehouseId = req.WarehouseId,
|
||||
};
|
||||
await Send.OkAsync(responseDto, ct);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user