diff --git a/barkman/DB.cs b/barkman/DB.cs new file mode 100644 index 0000000..231fad2 --- /dev/null +++ b/barkman/DB.cs @@ -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 _inventory = new List() + { + 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 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(); + } +} \ No newline at end of file diff --git a/barkman/Program.cs b/barkman/Program.cs index d5e0ef3..5a8f98b 100644 --- a/barkman/Program.cs +++ b/barkman/Program.cs @@ -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("/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.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(); - -record WeatherForecast(DateOnly Date, int TemperatureC, string? Summary) -{ - public int TemperatureF => 32 + (int)(TemperatureC / 0.5556); -} \ No newline at end of file +app.Run(); \ No newline at end of file diff --git a/barkman/barkman.csproj b/barkman/barkman.csproj index c741fda..2805cec 100644 --- a/barkman/barkman.csproj +++ b/barkman/barkman.csproj @@ -7,7 +7,8 @@ - + +