diff --git a/barkmanAPI/Program.cs b/barkmanAPI/Program.cs index 06b051d..58d9dce 100644 --- a/barkmanAPI/Program.cs +++ b/barkmanAPI/Program.cs @@ -47,6 +47,10 @@ var itemStatusGroup = app.MapGroup(prefix: "/itemstatus") .WithTags("Item Status") .WithDescription("Endpoints for managing item status"); +var customerGroup = app.MapGroup(prefix: "/customers") + .WithTags("Customers") + .WithDescription("Endpoints for managing customers"); + inventoryGroup.MapGet("", async (BarkContext db) => await db.Inventory.OrderBy(item => item.Barcode).ToListAsync()); @@ -163,6 +167,71 @@ itemStatusGroup.MapDelete("/{id}", async (string id, BarkContext db) => return Results.Ok(new { Message = "Item status deleted successfully" }); }); +customerGroup.MapGet("", async (BarkContext db) => + await db.Customers.OrderBy(customer => customer.Id).ToListAsync()); + +customerGroup.MapGet("/{id}", async (int id, BarkContext db) => +{ + var customer = await db.Customers + .FirstOrDefaultAsync(i => i.Id == id); + + if (customer == null) + { + return Results.NotFound(new { Message = "Customer item not found" }); + } + + return Results.Ok(customer); +}); + +customerGroup.MapPut("/{id}", async (int id, Customers updatedCustomer, BarkContext db) => +{ + var existingCustomer = await db.Customers.FindAsync(id); + if (existingCustomer == null) + { + return Results.NotFound(new { Message = "Customer not found" }); + } + + existingCustomer.Name = updatedCustomer.Name; + existingCustomer.Company = updatedCustomer.Company; + existingCustomer.Email = updatedCustomer.Email; + existingCustomer.PhoneNumber = updatedCustomer.PhoneNumber; + existingCustomer.Address = updatedCustomer.Address; + existingCustomer.BillingTerms = updatedCustomer.BillingTerms; + existingCustomer.Notes = updatedCustomer.Notes; + + await db.SaveChangesAsync(); + return Results.Ok(existingCustomer); +}); + +customerGroup.MapPost("", async (Customers newCustomerInput, BarkContext db) => +{ + var newCustomer = new Customers() + { + Name = newCustomerInput.Name, + Email = newCustomerInput.Email, + }; + newCustomer.Company = newCustomerInput.Company; + newCustomer.PhoneNumber = newCustomerInput.PhoneNumber; + newCustomer.Address = newCustomerInput.Address; + + db.Customers.Add(newCustomer); + await db.SaveChangesAsync(); + return Results.Created($"/customers/{newCustomer.Id}", newCustomer); +}); + +customerGroup.MapDelete("/{id}", async (int id, BarkContext db) => +{ + var customer = await db.Customers.FindAsync(id); + if (customer == null) + { + return Results.NotFound(new { Message = "Customer not found" }); + } + + db.Customers.Remove(customer); + await db.SaveChangesAsync(); + return Results.Ok(new { Message = "Customer deleted successfully" }); +}); + using (var serviceScope = app.Services.CreateScope()) { var dbContext = serviceScope.ServiceProvider.GetRequiredService();