forked from sanchezvem/PyroFetes
		
	
		
			
				
	
	
		
			54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			54 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using API.DTO.Warehouse.Request;
 | |
| using API.DTO.Warehouse.Response;
 | |
| using FastEndpoints;
 | |
| using Microsoft.EntityFrameworkCore;
 | |
| 
 | |
| namespace PyroFetes.Endpoints.Warehouse;
 | |
| 
 | |
| public class UpdateWarehouseEndpoint(PyroFetesDbContext pyrofetesdbcontext) : Endpoint<UpdateWarehouseDto, GetWarehouseDto>
 | |
| {
 | |
|     public override void Configure()
 | |
|     {
 | |
|         Put("/api/warehouses/{@id}", x => new { x.Id });
 | |
|         AllowAnonymous();
 | |
|     }
 | |
| 
 | |
|     public override async Task HandleAsync(UpdateWarehouseDto req, CancellationToken ct)
 | |
|     {
 | |
|         Models.Warehouse? warehouseToEdit = await pyrofetesdbcontext
 | |
|             .Warehouses
 | |
|             .SingleOrDefaultAsync(w => w.Id == req.Id, cancellationToken: ct);
 | |
| 
 | |
|         if (warehouseToEdit == null)
 | |
|         {
 | |
|             Console.WriteLine($"Aucun entrepôt avec l'ID {req.Id} trouvé.");
 | |
|             await Send.NotFoundAsync(ct);
 | |
|             return;
 | |
|         }
 | |
| 
 | |
|         // Mise à jour des champs
 | |
|         warehouseToEdit.Name = req.Name;
 | |
|         warehouseToEdit.MaxWeight = req.MaxWeight;
 | |
|         warehouseToEdit.Current = req.Current;
 | |
|         warehouseToEdit.MinWeight = req.MinWeight;
 | |
|         warehouseToEdit.Address = req.Adress;
 | |
|         warehouseToEdit.ZipCode = req.ZipCode;
 | |
|         warehouseToEdit.City = req.City;
 | |
| 
 | |
|         await pyrofetesdbcontext.SaveChangesAsync(ct);
 | |
| 
 | |
|         GetWarehouseDto responseDto = new()
 | |
|         {
 | |
|             Id = warehouseToEdit.Id,
 | |
|             Name = warehouseToEdit.Name,
 | |
|             MaxWeight = warehouseToEdit.MaxWeight,
 | |
|             Current = warehouseToEdit.Current,
 | |
|             MinWeight = warehouseToEdit.MinWeight,
 | |
|             Adress = warehouseToEdit.Address,
 | |
|             ZipCode = warehouseToEdit.ZipCode,
 | |
|             City = warehouseToEdit.City
 | |
|         };
 | |
| 
 | |
|         await Send.OkAsync(responseDto, ct);
 | |
|     }
 | |
| } |