diff --git a/barkmanAPI/Program.cs b/barkmanAPI/Program.cs index f137c77..82e2ac6 100644 --- a/barkmanAPI/Program.cs +++ b/barkmanAPI/Program.cs @@ -346,6 +346,52 @@ jobStatusGroup.MapDelete("/{id}", async (string id, BarkContext db) => return Results.Ok(new { Message = "Job status deleted successfully" }); }); +jobItemGroup.MapGet("", async (BarkContext db) => + await db.JobItems.OrderBy(items => items.Id).ToListAsync()); + +jobItemGroup.MapGet("/{id}", async (int id, BarkContext db) => +{ + var jobItems = await db.JobItems + .FirstOrDefaultAsync(i => i.Id == id); + + if (jobItems == null) + { + return Results.NotFound(new { Message = "Job Items not found" }); + } + + return Results.Ok(jobItems); +}); + +jobItemGroup.MapPut("/{id}", async (int id, JobItems updatedJobItems, BarkContext db) => +{ + var existingJobItems = await db.JobItems.FindAsync(id); + if (existingJobItems == null) + { + return Results.NotFound(new { Message = "Job Items not found" }); + } + + existingJobItems.OrderId = updatedJobItems.OrderId; + existingJobItems.ItemId = updatedJobItems.ItemId; + existingJobItems.Quantity = updatedJobItems.Quantity; + + await db.SaveChangesAsync(); + return Results.Ok(existingJobItems); +}); + +jobItemGroup.MapPost("", async (JobItems newJobItemsInput, BarkContext db) => +{ + var newJobItem = new JobItems() + { + OrderId = newJobItemsInput.OrderId, + ItemId = newJobItemsInput.ItemId, + Quantity = newJobItemsInput.Quantity, + }; + + db.JobItems.Add(newJobItem); + await db.SaveChangesAsync(); + return Results.Created($"/jobItems/{newJobItem.Id}", newJobItem); +}); + using (var serviceScope = app.Services.CreateScope()) { var dbContext = serviceScope.ServiceProvider.GetRequiredService();