Files
barkman/barkmanAPI/Program.cs
T
drew 5517924259 Fix duplicate changedOrderToJob migration blocking build
Two divergent copies of this branch each independently regenerated an
EF migration named changedOrderToJob, and merging both left two files
with the same C# class name (compile error) plus an orphaned
fixedOrderCustomerID migration referencing the already-renamed orders
table. Removed the dead/unapplied migrations, committed the two
migrations already live on the shared dev DB, and added
FixJobsCustomerIdType to bring jobs.customer_id in line with the model
(text -> integer) using an explicit USING cast.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-25 15:45:09 -05:00

405 lines
12 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://barkui.ts.drewr.io", "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");
var customerGroup = app.MapGroup(prefix: "/customers")
.WithTags("Customers")
.WithDescription("Endpoints for managing customers");
var jobGroup = app.MapGroup(prefix: "/jobs")
.WithTags("Jobs")
.WithDescription("Endpoints for managing jobs");
var jobStatusGroup = app.MapGroup(prefix: "/jobstatus")
.WithTags("Job Status")
.WithDescription("Endpoints for managing job status");
var jobItemGroup = app.MapGroup(prefix: "/jobitems")
.WithTags("Job Items")
.WithDescription("Endpoints for managing job items");
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 = "Item status 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" });
});
customerGroup.MapGet("", async (BarkContext db) =>
await db.Customers.OrderBy(customer => customer.Id).ToListAsync());
customerGroup.MapGet("/{id}", async (int id, BarkContext db) =>
{
var customer = await db.Customers
.FirstOrDefaultAsync(i => i.Id == id);
if (customer == null)
{
return Results.NotFound(new { Message = "Customer not found" });
}
return Results.Ok(customer);
});
customerGroup.MapPut("/{id}", async (int id, Customers updatedCustomer, BarkContext db) =>
{
var existingCustomer = await db.Customers.FindAsync(id);
if (existingCustomer == null)
{
return Results.NotFound(new { Message = "Customer not found" });
}
existingCustomer.Name = updatedCustomer.Name;
existingCustomer.Company = updatedCustomer.Company;
existingCustomer.Email = updatedCustomer.Email;
existingCustomer.PhoneNumber = updatedCustomer.PhoneNumber;
existingCustomer.Address = updatedCustomer.Address;
existingCustomer.BillingTerms = updatedCustomer.BillingTerms;
existingCustomer.Notes = updatedCustomer.Notes;
await db.SaveChangesAsync();
return Results.Ok(existingCustomer);
});
customerGroup.MapPost("", async (Customers newCustomerInput, BarkContext db) =>
{
var newCustomer = new Customers()
{
Name = newCustomerInput.Name,
Email = newCustomerInput.Email,
Company = newCustomerInput.Company,
PhoneNumber = newCustomerInput.PhoneNumber,
Address = newCustomerInput.Address,
};
db.Customers.Add(newCustomer);
await db.SaveChangesAsync();
return Results.Created($"/customers/{newCustomer.Id}", newCustomer);
});
customerGroup.MapDelete("/{id}", async (int id, BarkContext db) =>
{
var customer = await db.Customers.FindAsync(id);
if (customer == null)
{
return Results.NotFound(new { Message = "Customer not found" });
}
db.Customers.Remove(customer);
await db.SaveChangesAsync();
return Results.Ok(new { Message = "Customer deleted successfully" });
});
jobGroup.MapGet("", async (BarkContext db) =>
await db.Jobs.OrderBy(jobs => jobs.Id).ToListAsync());
jobGroup.MapGet("/{id}", async (int id, BarkContext db) =>
{
var job = await db.Jobs
.FirstOrDefaultAsync(i => i.Id == id);
if (job == null)
{
return Results.NotFound(new { Message = "Job not found" });
}
return Results.Ok(job);
});
jobGroup.MapPut("/{id}", async (int id, Jobs updatedJob, BarkContext db) =>
{
var existingJob = await db.Jobs.FindAsync(id);
if (existingJob == null)
{
return Results.NotFound(new { Message = "Job not found" });
}
existingJob.Name = updatedJob.Name;
existingJob.StartDate = updatedJob.StartDate;
existingJob.EndDate = updatedJob.EndDate;
existingJob.TotalPrice = updatedJob.TotalPrice;
existingJob.Venue = updatedJob.Venue;
existingJob.Address = updatedJob.Address;
existingJob.Notes = updatedJob.Notes;
await db.SaveChangesAsync();
return Results.Ok(existingJob);
});
jobGroup.MapPost("", async (Jobs newJobInput, BarkContext db) =>
{
var newJob = new Jobs()
{
CustomerId = newJobInput.CustomerId,
Name = newJobInput.Name,
StartDate = newJobInput.StartDate,
EndDate = newJobInput.EndDate,
Venue = newJobInput.Venue,
Address = newJobInput.Address,
Notes = newJobInput.Notes,
};
db.Jobs.Add(newJob);
await db.SaveChangesAsync();
return Results.Created($"/jobs/{newJob.Id}", newJob);
});
jobStatusGroup.MapGet("/", async (BarkContext db) =>
await db.JobStatus.ToListAsync());
jobStatusGroup.MapPost("/", async (JobStatus newJobStatus, BarkContext db) =>
{
db.JobStatus.Add(newJobStatus);
await db.SaveChangesAsync();
return Results.Created($"/jobstatus/{newJobStatus.Id}", newJobStatus);
});
jobStatusGroup.MapGet("/{id}", async (string id, BarkContext db) =>
{
var jobStatus = await db.JobStatus.FindAsync(id);
if (jobStatus == null)
{
return Results.NotFound(new { Message = "Job Status not found" });
}
return Results.Ok(jobStatus);
});
jobStatusGroup.MapPut("/{id}", async (string id, JobStatus updatedStatus, BarkContext db) =>
{
var existingStatus = await db.JobStatus.FindAsync(id);
if (existingStatus == null)
{
return Results.NotFound(new { Message = "Job status not found" });
}
existingStatus.Id = updatedStatus.Id;
existingStatus.Name = updatedStatus.Name;
await db.SaveChangesAsync();
return Results.Ok(existingStatus);
});
jobStatusGroup.MapDelete("/{id}", async (string id, BarkContext db) =>
{
var jobStatus = await db.JobStatus.FindAsync(id);
if (jobStatus == null)
{
return Results.NotFound(new { Message = "Job status not found" });
}
db.JobStatus.Remove(jobStatus);
await db.SaveChangesAsync();
return Results.Ok(new { Message = "Job status deleted successfully" });
});
jobItemGroup.MapGet("", async (BarkContext db) =>
await db.JobItems.OrderBy(items => items.Id).ToListAsync());
jobItemGroup.MapGet("/{id}", async (int id, BarkContext db) =>
{
var jobItems = await db.JobItems
.FirstOrDefaultAsync(i => i.Id == id);
if (jobItems == null)
{
return Results.NotFound(new { Message = "Job Items not found" });
}
return Results.Ok(jobItems);
});
jobItemGroup.MapPut("/{id}", async (int id, JobItems updatedJobItems, BarkContext db) =>
{
var existingJobItems = await db.JobItems.FindAsync(id);
if (existingJobItems == null)
{
return Results.NotFound(new { Message = "Job Items not found" });
}
existingJobItems.JobId = updatedJobItems.JobId;
existingJobItems.ItemId = updatedJobItems.ItemId;
existingJobItems.Quantity = updatedJobItems.Quantity;
await db.SaveChangesAsync();
return Results.Ok(existingJobItems);
});
jobItemGroup.MapPost("", async (JobItems newJobItemsInput, BarkContext db) =>
{
var newJobItem = new JobItems()
{
JobId = newJobItemsInput.JobId,
ItemId = newJobItemsInput.ItemId,
Quantity = newJobItemsInput.Quantity,
};
db.JobItems.Add(newJobItem);
await db.SaveChangesAsync();
return Results.Created($"/jobItems/{newJobItem.Id}", newJobItem);
});
using (var serviceScope = app.Services.CreateScope())
{
var dbContext = serviceScope.ServiceProvider.GetRequiredService<BarkContext>();
dbContext.Database.Migrate();
}
app.UseCors(allowSpecificOrigins);
app.Run();