created item status db

This commit is contained in:
2025-01-23 13:02:54 -06:00
parent 47d2918a0b
commit 58e19bd32a
5 changed files with 166 additions and 13 deletions
+18 -13
View File
@@ -10,18 +10,20 @@ builder.Services.AddDbContext<BarkContext>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
c.SwaggerDoc("v1",
new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
});
builder.Services.AddCors(options =>
{
options.AddPolicy(name: allowSpecificOrigins,
policy =>
policy =>
{
policy.WithOrigins("https://barkdev.ts.drewr.io", "http://localhost:5173").AllowAnyMethod().AllowAnyHeader();
policy.WithOrigins("https://barkdev.ts.drewr.io", "http://localhost:5173").AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddDbContext<BarkContext>(opt =>
builder.Services.AddDbContext<BarkContext>(opt =>
opt.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")).UseSnakeCaseNamingConvention());
var app = builder.Build();
@@ -29,16 +31,13 @@ var app = builder.Build();
if (!app.Environment.IsProduction())
{
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bark Inventory API V1");
});
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bark Productions API V1"); });
}
app.MapGet("/", () => "Hello World!");
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) =>
{
@@ -47,6 +46,7 @@ app.MapGet("/inventory/{id}", async (int id, BarkContext db) =>
{
return Results.NotFound(new { Message = "Inventory item not found" });
}
return Results.Ok(item);
});
@@ -57,7 +57,7 @@ app.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkCon
{
return Results.NotFound(new { Message = "Inventory item not found" });
}
existingItem.Name = updatedItem.Name;
existingItem.Brand = updatedItem.Brand;
existingItem.SerialNumber = updatedItem.SerialNumber;
@@ -68,7 +68,7 @@ app.MapPut("/inventory/{id}", async (int id, InventoryItems updatedItem, BarkCon
await db.SaveChangesAsync();
return Results.Ok(existingItem);
});
});
app.MapPost("/inventory", async (InventoryItems newItem, BarkContext db) =>
@@ -81,14 +81,19 @@ app.MapPost("/inventory", async (InventoryItems newItem, BarkContext db) =>
app.MapDelete("/inventory/{id}", async (int id, BarkContext db) =>
{
var item = await db.Inventory.FindAsync(id);
if (item == null) {
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("/itemstatus", async (BarkContext db) =>
await db.ItemStatus.ToListAsync());
using (var serviceScope = app.Services.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
@@ -97,4 +102,4 @@ using (var serviceScope = app.Services.CreateScope())
app.UseCors(allowSpecificOrigins);
app.Run();
app.Run();