Skip to content

Commit

Permalink
sigh
Browse files Browse the repository at this point in the history
  • Loading branch information
Adel-Atzouza committed Feb 7, 2025
1 parent e5e2c67 commit 8722ede
Show file tree
Hide file tree
Showing 34 changed files with 742 additions and 297 deletions.
37 changes: 2 additions & 35 deletions CargoHub/AppDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AppDbContext(DbContextOptions<AppDbContext> options) : DbContext(op
public DbSet<ItemLine> ItemLines { get; set; }
public DbSet<ItemGroup> ItemGroups { get; set; }
public DbSet<Order> Orders { get; set; }
// public DbSet<Shipment> Shipments { get; set; }
public DbSet<Shipment> Shipments { get; set; }
public DbSet<Location> Locations { get; set; }
public DbSet<ItemType> ItemTypes { get; set; }
public DbSet<Item> Items { get; set; }
Expand Down Expand Up @@ -53,40 +53,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);

// modelBuilder.Entity<Shipment>()
// .HasMany(s => s.orders) // Een zending kan meerdere orders bevatten
// .WithOne(o => o.Shipment) // Elke order hoort bij één zending
// .HasForeignKey(o => o.ShipmentId) // Dit is de koppeling via ShipmentId in de Orders-tabel
// .IsRequired(false); // ShipmentId mag leeg zijn, want niet elke order heeft een zending

// modelBuilder.Entity<Order>()
// .HasOne(o => o.Shipment) // Een order kan gekoppeld zijn aan één zending
// .WithMany(s => s.orders) // Een zending kan meerdere orders bevatten
// .HasForeignKey(o => o.ShipmentId) // De koppeling is via ShipmentId in de Orders-tabel
// .IsRequired(false); // Toegestaan dat een order geen zending heeft (nullable)

// modelBuilder.Entity<Order>()
// .HasOne(o => o.ShipToClient) // Een order heeft één ontvanger (ShipTo)
// .WithMany() // Er is geen navigatie terug naar alle orders van de ontvanger
// .HasForeignKey(o => o.ShipTo); // De koppeling gebeurt via ShipTo (foreign key)

// modelBuilder.Entity<Order>()
// .HasOne(o => o.BillToClient) // Een order heeft één klant die de rekening krijgt (BillTo)
// .WithMany() // Geen navigatie terug naar alle orders van de klant
// .HasForeignKey(o => o.BillTo); // De koppeling gebeurt via BillTo (foreign key)

// modelBuilder.Entity<OrderItem>()
// .HasKey(oi => new { oi.OrderId, oi.ItemUid }); // Primaire sleutel: combinatie van OrderId en ItemUid

// modelBuilder.Entity<OrderItem>()
// .HasOne(oi => oi.Order) // Een OrderItem hoort bij één order
// .WithMany(o => o.OrderItems) // Een order kan meerdere OrderItems hebben
// .HasForeignKey(oi => oi.OrderId); // De koppeling is via OrderId

// modelBuilder.Entity<OrderItem>()
// .HasOne(oi => oi.Item) // Een OrderItem hoort bij één specifiek item
// .WithMany(i => i.OrderItems) // Een item kan in meerdere orders voorkomen
// .HasForeignKey(oi => oi.ItemUid); // De koppeling is via ItemUid


// modelBuilder.Entity<Inventory>()
Expand All @@ -103,6 +69,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)


modelBuilder.Entity<Order>().Navigation(c => c.Items).AutoInclude(true);
modelBuilder.Entity<Shipment>().Navigation(c => c.Orders).AutoInclude(true);



Expand Down
2 changes: 1 addition & 1 deletion CargoHub/Controllers/APIKeyController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace CargoHub.Controllers
{
[ApiController]
[Route("api/v1/[controller]")]
[Route("api/v2/[controller]")]
[AuthorizationFilter]
public class APIKeyController : ControllerBase
{
Expand Down
2 changes: 1 addition & 1 deletion CargoHub/Controllers/BaseController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public virtual async Task<IActionResult> PutRow(string id, [FromBody] T row)
if (!updatedRow)
return error.IdNotFound(Name, id);

return Ok(updatedRow);
return Ok($"{Name} with id {_id} updated successfully");
}
return error.ModelInvalid(Name, ModelState);
}
Expand Down
12 changes: 11 additions & 1 deletion CargoHub/Controllers/ClientsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@ namespace CargoHub.Controllers
{
[AuthorizationFilter]
[Route("api/v2/[Controller]")]
public class ClientsController(BaseStorageService<Client> storage, ErrorHandler error) : BaseController<Client>(storage, error) { }
public class ClientsController(ClientStorageService storage, ErrorHandler error) : BaseController<Client>(storage, error)
{
[HttpGet("{id}/orders")]
public async Task<IActionResult> GetOrders(int id)
{
var client = await storage.GetRow(id);
if (client is null) return error.IdNotFound(Name, id.ToString());
var orders = await storage.GetOrders(id);
return orders is null ? NotFound($"Orders not found for client with id {id}") : Ok(orders);
}
}
}
23 changes: 20 additions & 3 deletions CargoHub/Controllers/InventoriesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,33 @@ namespace CargoHub.Controllers
{
[AuthorizationFilter]
[Route("api/v2/[Controller]")]
public class InventoriesController(InventoryStorageService storage, ErrorHandler error) : BaseController<Inventory>(storage, error)
public class InventoriesController(InventoryStorageService storage, ItemStorageService itemStorageService, ErrorHandler error) : BaseController<Inventory>(storage, error)
{

public async Task<IActionResult?> Validate(Inventory row)
{
if (!await itemStorageService.ValidateItem(row.ItemId))
{
return BadRequest("Error: Id for ship to does not exist.");
}


return null;
}

public override async Task<IActionResult> PostRow([FromBody] Inventory row)
{
if (!ModelState.IsValid)
{
return error.ModelInvalid(Name, ModelState);
}

var errorValidate = await Validate(row);
if (errorValidate is not null)
{
return errorValidate;
}

var errorLocations = await storage.LocationsNotExist(row.LocationIds);
if (errorLocations is not null)
{
Expand All @@ -25,7 +42,7 @@ public override async Task<IActionResult> PostRow([FromBody] Inventory row)

var result = await base.PostRow(row);

if (row.Locations.Count != 0)
if (row.LocationIds.Count != 0)
{
var inventory = await storage.GetRow(row.Id);

Expand All @@ -34,7 +51,7 @@ public override async Task<IActionResult> PostRow([FromBody] Inventory row)
var n = await storage.AddLocationsToInventory(row.Id, row.LocationIds);
if (n is false)
{
return BadRequest("Couldnt add location to inventory");
return BadRequest("Couldn't add location to inventory");
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions CargoHub/Controllers/ItemsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ namespace CargoHub.Controllers
[Route("api/v2/[Controller]")]
public class ItemsController(ItemStorageService storage, InventoryStorageService inventoryStorageService, ErrorHandler error) : BaseController<Item>(storage, error)
{


public override async Task<IActionResult> PostRow([FromBody] Item row)
{
if (ModelState.IsValid)
Expand Down
20 changes: 19 additions & 1 deletion CargoHub/Controllers/LocationsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,26 @@ namespace CargoHub.Controllers
{
[AuthorizationFilter]
[Route("api/v2/[Controller]")]
public class LocationsController(LocationStorageService storage, ErrorHandler error) : BaseController<Location>(storage, error)
public class LocationsController(LocationStorageService storage, WarehouseStorageService warehouseStorageService, ErrorHandler error) : BaseController<Location>(storage, error)
{
public override async Task<IActionResult> PostRow([FromBody] Location row)
{
if (!ModelState.IsValid)
{
return error.ModelInvalid(base.Name, ModelState);
}

if (row == null)
{
return error.CantBeNull(base.Name);
}

if (!await warehouseStorageService.ValidateWarehouse((int)row.WarehouseId))
{
return BadRequest("Error: Id for warehouse does not exist.");
}

return await base.PostRow(row);
}
}
}
2 changes: 1 addition & 1 deletion CargoHub/Controllers/MonthlyReportController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// namespace CargoHub.Controllers
// {
// [Route("api/v1/reports")]
// [Route("api/v2/reports")]
// [ApiController]
// [AuthorizationFilter]
// public class ReportsController : ControllerBase
Expand Down
90 changes: 73 additions & 17 deletions CargoHub/Controllers/OrderController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,57 @@

namespace CargoHub.Controllers
{


[AuthorizationFilter]
[Route("api/v2/[Controller]")]
public class OrdersController(OrderStorageService storage, ItemStorageService itemStorageService, InventoryStorageService inventoryStorageService, ErrorHandler error) : BaseController<Order>(storage, error)
public class OrdersController(OrderStorageService storage, WarehouseStorageService warehouseStorageService, ItemStorageService itemStorageService, ClientStorageService clientStorageService, ErrorHandler error) : BaseController<Order>(storage, error)
{

public async Task<IActionResult?> Validate(Order row)
{
if (!await clientStorageService.ValidateClient(row.ShipToId))
{
return BadRequest("Error: Id for ship to does not exist.");
}

if (!await clientStorageService.ValidateClient(row.BillToId))
{
return BadRequest("Error: Id for bill to does not exist.");
}

if (!await warehouseStorageService.ValidateWarehouse(row.WarehouseId))
{
return BadRequest("Error: Id for warehouse does not exist.");
}

return null;
}

public override async Task<IActionResult> PostRow([FromBody] Order row)
{
if (!ModelState.IsValid)
{
return error.ModelInvalid(base.Name, ModelState);
}

var errorValidate = await Validate(row);
if (errorValidate is not null)
{
return errorValidate;
}

string? errorItems = await itemStorageService.ValidateItems(row.Items);

if (errorItems is null)
{
inventoryStorageService.CommitOrder(row);
return await base.PostRow(row);
}

return BadRequest(errorItems);
}

[HttpPut("{id}")]
public override async Task<IActionResult> PutRow(string id, [FromBody] Order row)
{
if (!ModelState.IsValid)
Expand All @@ -39,23 +68,31 @@ public override async Task<IActionResult> PutRow(string id, [FromBody] Order row
var order = await storage.GetRow((int)_id);
if (order is null) return error.IdNotFound(Name, id);

switch (order.OrderStatus)
var errorValidate = await Validate(row);
if (errorValidate is not null)
{
return errorValidate;
}

if (order.OrderStatus == "Pending" && row.OrderStatus != "Pending")
{
case "Pending":
return BadRequest("Can't change status of pending order");
case "Packed":
// todo: handle inventory as well
if (row.OrderStatus != "Pending")
return BadRequest("Can only change status from packed to Pending");

return await base.PutRow(id, row);
case "Shipped":
return BadRequest("Can't change status of shipped order");
case "Delivered":
return BadRequest("Can't change status of delivered order");
default:
return BadRequest("Invalid order status");
return BadRequest("Error: Only shipment can change the order status.");
}

if (order.OrderStatus != "Pending")
{
return BadRequest("Error: Only Pending order can be updated.");
}

string? errorItems = await itemStorageService.ValidateItems(row.Items);

if (errorItems is null)
{
order.Items.Clear();
return await base.PutRow(id, row);
}

return BadRequest(errorItems);
}


Expand All @@ -67,5 +104,24 @@ public async Task<IActionResult> GetItems(int id)
return Ok(order.Items);
}


[HttpPut("{id}/items")]
public async Task<IActionResult> PutItems(int id, [FromBody] List<OrderItem> items)
{
string? errorItems = await itemStorageService.ValidateItems(items);

if (errorItems is not null)
{
return BadRequest(errorItems);
}

Order? order = await storage.GetRow(id);
if (order is null) return error.IdNotFound(base.Name, id.ToString());
order.Items.Clear();
order.Items = items;
var suc = await storage.UpdateRow(id, order);
return suc ? Ok($"{Name} with id {id} updated successfully") : BadRequest("Error: Couldn't update order items.");
}

}
}
Loading

0 comments on commit 8722ede

Please sign in to comment.