mirror of
https://github.com/BarkProductions/barkman.git
synced 2026-06-12 22:11:54 +00:00
Add CRUD endpoints for inventory management
Implemented GET, POST, PUT, and DELETE endpoints for the `/inventory` resource. These endpoints provide full CRUD functionality, handling operations such as retrieval, creation, update, and deletion of inventory items in the database. Adds appropriate error handling for cases where inventory items are not found.
This commit is contained in:
+51
-9
@@ -1,7 +1,6 @@
|
|||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using Microsoft.OpenApi.Models;
|
using Microsoft.OpenApi.Models;
|
||||||
|
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
builder.Services.AddDbContext<BarkContext>();
|
builder.Services.AddDbContext<BarkContext>();
|
||||||
@@ -24,14 +23,57 @@ if (app.Environment.IsDevelopment())
|
|||||||
|
|
||||||
app.MapGet("/", () => "Hello World!");
|
app.MapGet("/", () => "Hello World!");
|
||||||
|
|
||||||
// app.MapGet("/inventory", async (InventoryItems dbContext) =>
|
app.MapGet("/inventory", async (BarkContext db) =>
|
||||||
// {
|
await db.Inventory.ToListAsync());
|
||||||
// var inventoryItems = await dbContext.Set<InventoryItems>().ToListAsync();
|
|
||||||
// return Results.Ok(inventoryItems);
|
app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
|
||||||
// });
|
{
|
||||||
|
var item = await db.Inventory.FindAsync(id);
|
||||||
|
if (item == null)
|
||||||
|
{
|
||||||
|
return Results.NotFound(new { Message = "Inventory item not found" });
|
||||||
|
}
|
||||||
|
return Results.Ok(item);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkContext db) =>
|
||||||
|
{
|
||||||
|
var existingItem = await db.Inventory.FindAsync(id);
|
||||||
|
if (existingItem == null)
|
||||||
|
{
|
||||||
|
return Results.NotFound(new { Message = "Inventory item not found" });
|
||||||
|
}
|
||||||
|
|
||||||
|
existingItem.Name = updatedItem.Name;
|
||||||
|
existingItem.Brand = updatedItem.Brand;
|
||||||
|
existingItem.SerialNumber = updatedItem.SerialNumber;
|
||||||
|
existingItem.Status = updatedItem.Status;
|
||||||
|
existingItem.RentalPrice = updatedItem.RentalPrice;
|
||||||
|
existingItem.ReplacementCost = updatedItem.ReplacementCost;
|
||||||
|
existingItem.Notes = updatedItem.Notes;
|
||||||
|
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return Results.Ok(existingItem);
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
app.MapPost("/inventory", async (InventoryItems newItem, BarkContext db) =>
|
||||||
|
{
|
||||||
|
db.Inventory.Add(newItem);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return Results.Created($"/inventory/{newItem.Id}", newItem);
|
||||||
|
});
|
||||||
|
|
||||||
|
app.MapDelete("/inventory/{id}", async (int id, BarkContext db) =>
|
||||||
|
{
|
||||||
|
var item = await db.Inventory.FindAsync(id);
|
||||||
|
if (item == null) {
|
||||||
|
return Results.NotFound(new { Message = "Inventory item not found" });
|
||||||
|
}
|
||||||
|
db.Inventory.Remove(item);
|
||||||
|
await db.SaveChangesAsync();
|
||||||
|
return Results.Ok(new { Message = "Inventory item deleted successfully" });
|
||||||
|
});
|
||||||
|
|
||||||
//app.MapGet("/inventory/{id}", (int id) => InventoryDb.GetInventory(id));
|
|
||||||
//app.MapPut("/inventory", (InventoryItem item) => InventoryDb.UpdateItem(item));
|
|
||||||
//app.MapDelete("/inventory/{id}", (int id) => InventoryDb.RemoveItem(id));
|
|
||||||
|
|
||||||
app.Run();
|
app.Run();
|
||||||
Reference in New Issue
Block a user