Files
barkman/barkmanAPI/Program.cs
T

174 lines
5.2 KiB
C#

using barkmanapi;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
var builder = WebApplication.CreateBuilder(args);
var allowSpecificOrigins = "_AllowSpecificOrigins";
builder.Services.AddDbContext<BarkContext>();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddOpenApi("document", c =>
{
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 =>
{
options.AddPolicy(name: allowSpecificOrigins,
policy =>
{
policy.WithOrigins("https://barkdev.ts.drewr.io", "http://localhost:5173").AllowAnyMethod()
.AllowAnyHeader();
});
});
builder.Services.AddDbContext<BarkContext>(opt =>
opt.UseNpgsql(builder.Configuration.GetConnectionString("DefaultConnection")).UseSnakeCaseNamingConvention());
var app = builder.Build();
if (!app.Environment.IsProduction())
{
app.MapOpenApi();
app.UseSwaggerUI(c => { c.SwaggerEndpoint("/openapi/document.json", "Bark Productions API V1"); });
}
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("", async (BarkContext db) =>
await db.Inventory.OrderBy(item => item.Barcode).ToListAsync());
inventoryGroup.MapGet("/{id}", async (int id, BarkContext db) =>
{
var item = await db.Inventory
.Include(item => item.Status)
.FirstOrDefaultAsync(i => i.Id == id);
if (item == null)
{
return Results.NotFound(new { Message = "Inventory item not found" });
}
return Results.Ok(item);
});
inventoryGroup.MapPut("/{id}", async (int id, InventoryItems updatedItem, BarkContext db) =>
{
var existingItem = await db.Inventory.FindAsync(id);
if (existingItem == null)
{
return Results.NotFound(new { Message = "Inventory item not found" });
}
existingItem.Name = updatedItem.Name;
existingItem.Barcode = updatedItem.Barcode;
existingItem.Brand = updatedItem.Brand;
existingItem.SerialNumber = updatedItem.SerialNumber;
existingItem.StatusId = updatedItem.StatusId;
existingItem.RentalPrice = updatedItem.RentalPrice;
existingItem.ReplacementCost = updatedItem.ReplacementCost;
existingItem.Notes = updatedItem.Notes;
await db.SaveChangesAsync();
return Results.Ok(existingItem);
});
inventoryGroup.MapPost("", async (InventoryItems newItemInput, BarkContext db) =>
{
var newItem = new InventoryItems();
newItem.Name = newItemInput.Name;
newItem.Barcode = newItemInput.Barcode;
newItem.Brand = newItemInput.Brand;
newItem.SerialNumber = newItemInput.SerialNumber;
newItem.RentalPrice = newItemInput.RentalPrice;
newItem.ReplacementCost = newItemInput.ReplacementCost;
newItem.StatusId = "ready";
db.Inventory.Add(newItem);
await db.SaveChangesAsync();
return Results.Created($"/inventory/{newItem.Id}", newItem);
});
inventoryGroup.MapDelete("/{id}", async (int id, BarkContext db) =>
{
var item = await db.Inventory.FindAsync(id);
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" });
});
itemStatusGroup.MapGet("/", async (BarkContext db) =>
await db.ItemStatus.ToListAsync());
itemStatusGroup.MapPost("/", async (ItemStatus newItemStatus, BarkContext db) =>
{
db.ItemStatus.Add(newItemStatus);
await db.SaveChangesAsync();
return Results.Created($"/itemstatus/{newItemStatus.Id}", newItemStatus);
});
itemStatusGroup.MapGet("/{id}", async (string id, BarkContext db) =>
{
var itemStatus = await db.ItemStatus.FindAsync(id);
if (itemStatus == null)
{
return Results.NotFound(new { Message = "Item Status not found" });
}
return Results.Ok(itemStatus);
});
itemStatusGroup.MapPut("/{id}", async (string id, ItemStatus updatedStatus, BarkContext db) =>
{
var existingStatus = await db.ItemStatus.FindAsync(id);
if (existingStatus == null)
{
return Results.NotFound(new { Message = "Inventory item not found" });
}
existingStatus.Id = updatedStatus.Id;
existingStatus.Name = updatedStatus.Name;
await db.SaveChangesAsync();
return Results.Ok(existingStatus);
});
itemStatusGroup.MapDelete("/{id}", async (string id, BarkContext db) =>
{
var itemStatus = await db.ItemStatus.FindAsync(id);
if (itemStatus == null)
{
return Results.NotFound(new { Message = "Item status not found" });
}
db.ItemStatus.Remove(itemStatus);
await db.SaveChangesAsync();
return Results.Ok(new { Message = "Item status deleted successfully" });
});
using (var serviceScope = app.Services.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
dbContext.Database.Migrate();
}
app.UseCors(allowSpecificOrigins);
app.Run();