mirror of
https://github.com/BarkProductions/barkman.git
synced 2026-06-12 22:11:54 +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.AddDbContext<BarkContext>();
|
||||||
builder.Services.AddEndpointsApiExplorer();
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
builder.Services.AddSwaggerGen(c =>
|
builder.Services.AddOpenApi("document", c =>
|
||||||
{
|
{
|
||||||
c.SwaggerDoc("v1",
|
c.AddDocumentTransformer((doc, _, _) =>
|
||||||
new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
|
{
|
||||||
|
doc.Info.Version = "v1";
|
||||||
|
doc.Info.Title = "BarkMan API";
|
||||||
|
doc.Info.Description = "BARK BARK WOOF WOOF ARF";
|
||||||
|
return Task.CompletedTask;
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
builder.Services.AddCors(options =>
|
builder.Services.AddCors(options =>
|
||||||
@@ -32,17 +36,21 @@ var app = builder.Build();
|
|||||||
if (!app.Environment.IsProduction())
|
if (!app.Environment.IsProduction())
|
||||||
{
|
{
|
||||||
app.MapOpenApi();
|
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");
|
||||||
|
|
||||||
|
inventoryGroup.MapGet("/inventory", async (BarkContext db) =>
|
||||||
app.MapGet("/inventory", async (BarkContext db) =>
|
|
||||||
await db.Inventory.OrderBy(item => item.Id).ToListAsync());
|
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);
|
var item = await db.Inventory.FindAsync(id);
|
||||||
if (item == null)
|
if (item == null)
|
||||||
@@ -53,7 +61,7 @@ app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
|
|||||||
return Results.Ok(item);
|
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);
|
var existingItem = await db.Inventory.FindAsync(id);
|
||||||
if (existingItem == null)
|
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);
|
db.Inventory.Add(newItem);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
return Results.Created($"/inventory/{newItem.Id}", newItem);
|
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);
|
var item = await db.Inventory.FindAsync(id);
|
||||||
if (item == null)
|
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" });
|
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());
|
await db.ItemStatus.ToListAsync());
|
||||||
|
|
||||||
app.MapPost("/itemstatus", async (ItemStatus newItemStatus, BarkContext db) =>
|
itemStatusGroup.MapPost("/itemstatus", async (ItemStatus newItemStatus, BarkContext db) =>
|
||||||
{
|
{
|
||||||
db.ItemStatus.Add(newItemStatus);
|
db.ItemStatus.Add(newItemStatus);
|
||||||
await db.SaveChangesAsync();
|
await db.SaveChangesAsync();
|
||||||
return Results.Created($"/itemstatus/{newItemStatus.Id}", newItemStatus);
|
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);
|
var itemStatus = await db.ItemStatus.FindAsync(id);
|
||||||
if (itemStatus == null)
|
if (itemStatus == null)
|
||||||
@@ -115,7 +123,7 @@ app.MapGet("/itemstatus/{id}", async (string id, BarkContext db) =>
|
|||||||
return Results.Ok(itemStatus);
|
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);
|
var existingStatus = await db.ItemStatus.FindAsync(id);
|
||||||
if (existingStatus == null)
|
if (existingStatus == null)
|
||||||
@@ -130,7 +138,7 @@ app.MapPut("/itemstatus/{id}", async (string id, ItemStatus updatedStatus, BarkC
|
|||||||
return Results.Ok(existingStatus);
|
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);
|
var itemStatus = await db.ItemStatus.FindAsync(id);
|
||||||
if (itemStatus == null)
|
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" });
|
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())
|
using (var serviceScope = app.Services.CreateScope())
|
||||||
{
|
{
|
||||||
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
|
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
|
||||||
|
|||||||
Reference in New Issue
Block a user