mirror of
https://github.com/BarkProductions/barkman.git
synced 2026-06-13 06:11:55 +00:00
v1 API
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
namespace BarkMan.DB;
|
||||
|
||||
public record InventoryItem
|
||||
{
|
||||
public int Id {get; set;}
|
||||
public string ? Name { get; set; }
|
||||
public int Quantity { get; set; }
|
||||
}
|
||||
|
||||
public class InventoryDB
|
||||
{
|
||||
private static List<InventoryItem> _inventory = new List<InventoryItem>()
|
||||
{
|
||||
new InventoryItem(){Id = 1, Name = "Dac70", Quantity = 1},
|
||||
new InventoryItem(){Id = 2, Name = "Ethercon Barrel", Quantity = 4},
|
||||
new InventoryItem(){Id = 3, Name = "Powercon Barrel", Quantity = 4},
|
||||
new InventoryItem(){Id = 4, Name = "2018 MacBook Pro", Quantity = 1},
|
||||
};
|
||||
|
||||
public static List<InventoryItem> GetInventory()
|
||||
{
|
||||
return _inventory;
|
||||
}
|
||||
|
||||
public static InventoryItem ? GetInventory(int id)
|
||||
{
|
||||
return _inventory.SingleOrDefault(item => item.Id == id);
|
||||
}
|
||||
|
||||
public static InventoryItem CreateInventoryItem(InventoryItem item)
|
||||
{
|
||||
_inventory.Add(item);
|
||||
return item;
|
||||
}
|
||||
|
||||
public static InventoryItem UpdateItem(InventoryItem update)
|
||||
{
|
||||
_inventory = _inventory.Select(pizza =>
|
||||
{
|
||||
if (pizza.Id == update.Id)
|
||||
{
|
||||
pizza.Name = update.Name;
|
||||
}
|
||||
return pizza;
|
||||
}).ToList();
|
||||
return update;
|
||||
}
|
||||
|
||||
public static void RemoveItem(int id)
|
||||
{
|
||||
_inventory = _inventory.FindAll(item => item.Id != id).ToList();
|
||||
}
|
||||
}
|
||||
+19
-29
@@ -1,41 +1,31 @@
|
||||
using Microsoft.OpenApi.Models;
|
||||
using BarkMan.DB;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen(c =>
|
||||
{
|
||||
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c =>
|
||||
{
|
||||
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Bark Inventory API V1");
|
||||
});
|
||||
}
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
app.MapGet("/", () => "Hello World!");
|
||||
|
||||
var summaries = new[]
|
||||
{
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
};
|
||||
|
||||
app.MapGet("/weatherforecast", () =>
|
||||
{
|
||||
var forecast = Enumerable.Range(1, 5).Select(index =>
|
||||
new WeatherForecast
|
||||
(
|
||||
DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
Random.Shared.Next(-20, 55),
|
||||
summaries[Random.Shared.Next(summaries.Length)]
|
||||
))
|
||||
.ToArray();
|
||||
return forecast;
|
||||
})
|
||||
.WithName("GetWeatherForecast");
|
||||
app.MapGet("/inventory/{id}", (int id) => InventoryDB.GetInventory(id));
|
||||
app.MapGet("/inventory", () => InventoryDB.GetInventory());
|
||||
app.MapPost("/inventory", (InventoryItem item) => InventoryDB.CreateInventoryItem(item));
|
||||
app.MapPut("/inventory", (InventoryItem item) => InventoryDB.UpdateItem(item));
|
||||
app.MapDelete("/inventory/{id}", (int id) => InventoryDB.RemoveItem(id));
|
||||
|
||||
app.Run();
|
||||
|
||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
||||
{
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
}
|
||||
@@ -7,7 +7,8 @@
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0"/>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="9.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.1.4" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
Reference in New Issue
Block a user