mirror of
https://github.com/BarkProductions/barkman.git
synced 2026-06-13 06:11:55 +00:00
105 lines
3.0 KiB
C#
105 lines
3.0 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.AddSwaggerGen(c =>
|
|
{
|
|
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.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.UseSwagger();
|
|
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());
|
|
|
|
app.MapGet("/inventory/{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" });
|
|
}
|
|
|
|
return Results.Ok(item);
|
|
});
|
|
|
|
app.MapPut("/inventory/{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.Brand = updatedItem.Brand;
|
|
existingItem.SerialNumber = updatedItem.SerialNumber;
|
|
existingItem.Status = updatedItem.Status;
|
|
existingItem.RentalPrice = updatedItem.RentalPrice;
|
|
existingItem.ReplacementCost = updatedItem.ReplacementCost;
|
|
existingItem.Notes = updatedItem.Notes;
|
|
|
|
await db.SaveChangesAsync();
|
|
return Results.Ok(existingItem);
|
|
});
|
|
|
|
|
|
app.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) =>
|
|
{
|
|
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" });
|
|
});
|
|
|
|
app.MapGet("/itemstatus", async (BarkContext db) =>
|
|
await db.ItemStatus.ToListAsync());
|
|
|
|
using (var serviceScope = app.Services.CreateScope())
|
|
{
|
|
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
|
|
dbContext.Database.Migrate();
|
|
}
|
|
|
|
app.UseCors(allowSpecificOrigins);
|
|
|
|
app.Run(); |