mirror of
https://github.com/BarkProductions/barkman.git
synced 2026-06-13 06:11:55 +00:00
grouped api endpoints in swagger
This commit is contained in:
+25
-21
@@ -8,11 +8,15 @@ var allowSpecificOrigins = "_AllowSpecificOrigins";
|
||||
|
||||
builder.Services.AddDbContext<BarkContext>();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
builder.Services.AddOpenApi("document", c =>
|
||||
{
|
||||
c.SwaggerDoc("v1",
|
||||
new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
|
||||
|
||||
c.AddDocumentTransformer((doc, _, _) =>
|
||||
{
|
||||
doc.Info.Version = "v1";
|
||||
doc.Info.Title = "BarkMan API";
|
||||
doc.Info.Description = "BARK BARK WOOF WOOF ARF";
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
});
|
||||
|
||||
builder.Services.AddCors(options =>
|
||||
@@ -32,17 +36,21 @@ var app = builder.Build();
|
||||
if (!app.Environment.IsProduction())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bark Productions API V1"); });
|
||||
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/openapi/document.json", "Bark Productions API V1"); });
|
||||
}
|
||||
|
||||
app.MapGet("/", () => "Hello World!");
|
||||
var inventoryGroup = app.MapGroup(prefix: "/inventory")
|
||||
.WithTags("Inventory")
|
||||
.WithDescription("Endpoints for managing inventory items");
|
||||
|
||||
var itemStatusGroup = app.MapGroup(prefix: "/itemstatus")
|
||||
.WithTags("Item Status")
|
||||
.WithDescription("Endpoints for managing item status");
|
||||
|
||||
|
||||
app.MapGet("/inventory", async (BarkContext db) =>
|
||||
inventoryGroup.MapGet("/inventory", async (BarkContext db) =>
|
||||
await db.Inventory.OrderBy(item => item.Id).ToListAsync());
|
||||
|
||||
app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
|
||||
inventoryGroup.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
|
||||
{
|
||||
var item = await db.Inventory.FindAsync(id);
|
||||
if (item == null)
|
||||
@@ -53,7 +61,7 @@ app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
|
||||
return Results.Ok(item);
|
||||
});
|
||||
|
||||
app.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkContext db) =>
|
||||
inventoryGroup.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkContext db) =>
|
||||
{
|
||||
var existingItem = await db.Inventory.FindAsync(id);
|
||||
if (existingItem == null)
|
||||
@@ -74,14 +82,14 @@ app.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkCon
|
||||
});
|
||||
|
||||
|
||||
app.MapPost("/inventory", async (InventoryItems newItem, BarkContext db) =>
|
||||
inventoryGroup.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) =>
|
||||
inventoryGroup.MapDelete("/inventory/{id}", async (int id, BarkContext db) =>
|
||||
{
|
||||
var item = await db.Inventory.FindAsync(id);
|
||||
if (item == null)
|
||||
@@ -94,17 +102,17 @@ app.MapDelete("/inventory/{id}", async (int id, BarkContext db) =>
|
||||
return Results.Ok(new { Message = "Inventory item deleted successfully" });
|
||||
});
|
||||
|
||||
app.MapGet("/itemstatus", async (BarkContext db) =>
|
||||
itemStatusGroup.MapGet("/itemstatus", async (BarkContext db) =>
|
||||
await db.ItemStatus.ToListAsync());
|
||||
|
||||
app.MapPost("/itemstatus", async (ItemStatus newItemStatus, BarkContext db) =>
|
||||
itemStatusGroup.MapPost("/itemstatus", async (ItemStatus newItemStatus, BarkContext db) =>
|
||||
{
|
||||
db.ItemStatus.Add(newItemStatus);
|
||||
await db.SaveChangesAsync();
|
||||
return Results.Created($"/itemstatus/{newItemStatus.Id}", newItemStatus);
|
||||
});
|
||||
|
||||
app.MapGet("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
itemStatusGroup.MapGet("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
{
|
||||
var itemStatus = await db.ItemStatus.FindAsync(id);
|
||||
if (itemStatus == null)
|
||||
@@ -115,7 +123,7 @@ app.MapGet("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
return Results.Ok(itemStatus);
|
||||
});
|
||||
|
||||
app.MapPut("/itemstatus/{id}", async (string id, ItemStatus updatedStatus, BarkContext db) =>
|
||||
itemStatusGroup.MapPut("/itemstatus/{id}", async (string id, ItemStatus updatedStatus, BarkContext db) =>
|
||||
{
|
||||
var existingStatus = await db.ItemStatus.FindAsync(id);
|
||||
if (existingStatus == null)
|
||||
@@ -130,7 +138,7 @@ app.MapPut("/itemstatus/{id}", async (string id, ItemStatus updatedStatus, BarkC
|
||||
return Results.Ok(existingStatus);
|
||||
});
|
||||
|
||||
app.MapDelete("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
itemStatusGroup.MapDelete("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
{
|
||||
var itemStatus = await db.ItemStatus.FindAsync(id);
|
||||
if (itemStatus == null)
|
||||
@@ -143,10 +151,6 @@ app.MapDelete("/itemstatus/{id}", async (string id, BarkContext db) =>
|
||||
return Results.Ok(new { Message = "Item status deleted successfully" });
|
||||
});
|
||||
|
||||
var inventoryGroup = app.MapGroup(prefix: "/inventory")
|
||||
.WithTags("Inventory")
|
||||
.WithDescription("Endpoints for managing inventory items");
|
||||
|
||||
using (var serviceScope = app.Services.CreateScope())
|
||||
{
|
||||
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
|
||||
|
||||
Reference in New Issue
Block a user