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);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
// Add services to the container.
|
builder.Services.AddEndpointsApiExplorer();
|
||||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
builder.Services.AddSwaggerGen(c =>
|
||||||
builder.Services.AddOpenApi();
|
{
|
||||||
|
c.SwaggerDoc("v1", new OpenApiInfo { Title = "BarkMan API", Description = "BARK BARK WOOF WOOF", Version = "v1" });
|
||||||
|
});
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
|
||||||
if (app.Environment.IsDevelopment())
|
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[]
|
app.MapGet("/inventory/{id}", (int id) => InventoryDB.GetInventory(id));
|
||||||
{
|
app.MapGet("/inventory", () => InventoryDB.GetInventory());
|
||||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
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.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.Run();
|
app.Run();
|
||||||
|
|
||||||
record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary)
|
|
||||||
{
|
|
||||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
|
||||||
}
|
|
||||||
@@ -7,7 +7,8 @@
|
|||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<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>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|||||||
Reference in New Issue
Block a user