Created API Controllers (#1)
Removed strongly typed inventory objects Added EAV inventory types Added EAV object handling philosophy Added controllers Added EF Core migration integration Reviewed-on: #1
This commit was merged in pull request #1.
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using WyvernInventory.Core.Interfaces.Services;
|
||||
using WyvernInventory.Core.Models;
|
||||
|
||||
namespace WyvernInventory.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class InventoryController(IDataObjectService<GenericInventoryItem> genericItemService)
|
||||
: ControllerBase
|
||||
{
|
||||
private readonly IDataObjectService<GenericInventoryItem> _genericItemService = genericItemService;
|
||||
|
||||
[HttpPost]
|
||||
public List<GenericInventoryItem> Get([FromBody] List<GenericInventoryItem> items)
|
||||
{
|
||||
return _genericItemService.GetAsync(_ => _).Result;
|
||||
}
|
||||
|
||||
[HttpPost("upsert")]
|
||||
public IResult Post([FromBody] List<GenericInventoryItem> items)
|
||||
{
|
||||
(int, int) results = _genericItemService.UpsertAsync(items).Result;
|
||||
|
||||
if (results.Item1 > 0 || results.Item2 > 0)
|
||||
{
|
||||
return Results.StatusCode(201);
|
||||
}
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IResult Delete([FromRoute] int id)
|
||||
{
|
||||
_genericItemService.DeleteAsync(new() { new() { Id = id } }).Wait();
|
||||
|
||||
return Results.Ok();
|
||||
}
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace WyvernInventory.API.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("[controller]")]
|
||||
public class WeatherForecastController : ControllerBase
|
||||
{
|
||||
private static readonly string[] Summaries =
|
||||
[
|
||||
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
|
||||
];
|
||||
|
||||
[HttpGet(Name = "GetWeatherForecast")]
|
||||
public IEnumerable<WeatherForecast> GetBwah()
|
||||
{
|
||||
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
|
||||
[HttpGet("bwah", Name = "GetWeatherForecastBwah")]
|
||||
public IEnumerable<WeatherForecast> Get()
|
||||
{
|
||||
return Enumerable.Range(1, 2).Select(index => new WeatherForecast
|
||||
{
|
||||
Date = DateOnly.FromDateTime(DateTime.Now.AddDays(index)),
|
||||
TemperatureC = Random.Shared.Next(-20, 55),
|
||||
Summary = Summaries[Random.Shared.Next(Summaries.Length)]
|
||||
})
|
||||
.ToArray();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serilog.Events;
|
||||
using WyvernInventory.Core.Interfaces.Services;
|
||||
using WyvernInventory.Core.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace WyvernInventory.API.Controllers.api.v1;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class InventoryItemController(IInventoryItemService inventoryItemService, ILogger logger) : ControllerBase
|
||||
{
|
||||
private readonly IInventoryItemService _inventoryItemService = inventoryItemService;
|
||||
private ILogger _logger = logger;
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult<List<InventoryItemDto>> Get([FromBody] List<InventoryItemRequest> items)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _inventoryItemService.GetAsync(items).Result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("upsert")]
|
||||
public ActionResult Post([FromBody] List<InventoryItemRequest> items)
|
||||
{
|
||||
try
|
||||
{
|
||||
(int, int) results = _inventoryItemService.UpsertAsync(items).Result;
|
||||
|
||||
if (results.Item1 > 0 || results.Item2 > 0)
|
||||
{
|
||||
return Ok(new { Created = results.Item1, Updated = results.Item2 });
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public ActionResult Delete([FromRoute] int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
int results = _inventoryItemService.DeleteAsync([new() { Id = id }]).Result;
|
||||
|
||||
return Ok(results);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
using Microsoft.AspNetCore.HttpLogging;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Serilog.Events;
|
||||
using WyvernInventory.Core.Interfaces.Services;
|
||||
using WyvernInventory.Core.Models;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
namespace WyvernInventory.API.Controllers.api.v1;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/v1/[controller]")]
|
||||
public class InventoryTypeController(IDataObjectService<InventoryType, InventoryTypeDto> inventoryTypeService, ILogger logger) : ControllerBase
|
||||
{
|
||||
private readonly IDataObjectService<InventoryType, InventoryTypeDto> _inventoryTypeService = inventoryTypeService;
|
||||
private ILogger _logger = logger;
|
||||
|
||||
[HttpPost]
|
||||
public ActionResult<List<InventoryTypeDto>> Get([FromBody] List<InventoryType> items)
|
||||
{
|
||||
try
|
||||
{
|
||||
return _inventoryTypeService.GetAsync(items).Result;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpPost("upsert")]
|
||||
public IActionResult Post([FromBody] List<InventoryType> items)
|
||||
{
|
||||
try
|
||||
{
|
||||
(int, int) results = _inventoryTypeService.UpsertAsync(items).Result;
|
||||
|
||||
if (results.Item1 > 0 || results.Item2 > 0)
|
||||
{
|
||||
return Ok(new { Created = results.Item1, Updated = results.Item2 });
|
||||
}
|
||||
|
||||
return Ok();
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
public IActionResult Delete([FromRoute] int id)
|
||||
{
|
||||
try
|
||||
{
|
||||
int results = _inventoryTypeService.DeleteAsync([new() { Id = id }]).Result;
|
||||
|
||||
return Ok(results);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
_logger.Write(LogEventLevel.Error, e, "An error occured");
|
||||
return StatusCode(500, "Internal server error");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,27 +1,83 @@
|
||||
using System.Diagnostics;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.IdentityModel.Tokens;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
using WyvernInventory.Core.Interfaces.Repos;
|
||||
using WyvernInventory.Core.Interfaces.Services;
|
||||
using WyvernInventory.Core.Models;
|
||||
using WyvernInventory.Infrastructure.Data;
|
||||
using WyvernInventory.Infrastructure.Repos;
|
||||
using WyvernInventory.Infrastructure.Services;
|
||||
using ILogger = Serilog.ILogger;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
// Add services to the container.
|
||||
|
||||
builder.Services.AddControllers();
|
||||
|
||||
builder.Services.AddScoped<IDataObjectService<InventoryType, InventoryTypeDto>, InventoryTypeService>();
|
||||
builder.Services.AddScoped<IDbObjectRepo<InventoryType, InventoryTypeDto>, InventoryTypeRepo>();
|
||||
builder.Services.AddScoped<IInventoryItemService, InventoryItemService>();
|
||||
builder.Services.AddScoped<IInventoryItemRepo, InventoryItemRepo>();
|
||||
|
||||
// Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
|
||||
builder.Services.AddOpenApi();
|
||||
builder.Services.AddSingleton<IDbObjectRepo<GenericInventoryItem>, GenericInventoryItemRepo>();
|
||||
builder.Services.AddSingleton<IDataObjectService<GenericInventoryItem>, GenericInventoryItemService>();
|
||||
builder.Services.AddSwaggerGen();
|
||||
builder.Host.UseSerilog((context, services, configuration) =>
|
||||
configuration
|
||||
.ReadFrom.Configuration(context.Configuration)
|
||||
.ReadFrom.Services(services)
|
||||
);
|
||||
|
||||
if (builder.Configuration["Database:ConnectionString"].IsNullOrEmpty())
|
||||
{
|
||||
ILogger logger = new LoggerConfiguration().ReadFrom.Configuration(builder.Configuration).CreateLogger();
|
||||
|
||||
logger.Fatal("Database environment variables not found");
|
||||
|
||||
Environment.Exit(1);
|
||||
}
|
||||
else
|
||||
{
|
||||
builder.Services.AddDbContext<DBContext>(options =>
|
||||
{
|
||||
options
|
||||
.UseSqlServer(builder.Configuration["Database:ConnectionString"]).EnableSensitiveDataLogging();
|
||||
|
||||
if (builder.Environment.IsDevelopment())
|
||||
{
|
||||
options
|
||||
.EnableSensitiveDataLogging()
|
||||
.EnableDetailedErrors()
|
||||
.LogTo(msg => { Debug.WriteLine(msg); }, [DbLoggerCategory.Database.Command.Name], LogLevel.Information);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
using (var scope = app.Services.CreateScope())
|
||||
{
|
||||
DBContext db = scope.ServiceProvider.GetRequiredService<DBContext>();
|
||||
var logger = scope.ServiceProvider.GetRequiredService<ILogger>();
|
||||
|
||||
if (db.Database.GetPendingMigrations().Any())
|
||||
{
|
||||
await db.Database.MigrateAsync().ConfigureAwait(false);
|
||||
|
||||
logger.Write(LogEventLevel.Information, "Migrated database");
|
||||
}
|
||||
}
|
||||
|
||||
// Configure the HTTP request pipeline.
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.MapOpenApi();
|
||||
}
|
||||
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI();
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.UseAuthorization();
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": false,
|
||||
"applicationUrl": "https://localhost:7048;http://localhost:5244",
|
||||
"applicationUrl": "https://0.0.0.0:7048;http://0.0.0.0:5244",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
|
||||
@@ -1,12 +0,0 @@
|
||||
namespace WyvernInventory.API;
|
||||
|
||||
public class WeatherForecast
|
||||
{
|
||||
public DateOnly Date { get; set; }
|
||||
|
||||
public int TemperatureC { get; set; }
|
||||
|
||||
public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);
|
||||
|
||||
public string? Summary { get; set; }
|
||||
}
|
||||
@@ -9,6 +9,17 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.6"/>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="10.0.7" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.7">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Serilog" Version="4.3.1" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Settings.Configuration" Version="10.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.1.1" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="7.0.0" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="10.1.7" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@@ -5,21 +5,42 @@ Content-Type: application/json
|
||||
|
||||
[
|
||||
{
|
||||
"name": "RTX 4070"
|
||||
"name": "RTX 4070 Ti Super"
|
||||
}
|
||||
]
|
||||
###
|
||||
|
||||
POST http://localhost:5244/inventory/upsert
|
||||
POST http://localhost:5244/api/v1/InventoryType
|
||||
Content-Type: application/json
|
||||
|
||||
[
|
||||
]
|
||||
###
|
||||
|
||||
POST http://localhost:5244/api/v1/inventorytype/upsert
|
||||
Content-Type: application/json
|
||||
|
||||
[
|
||||
{
|
||||
"name": "RTX 4070 Ti Super",
|
||||
"description": "Desktop GPU"
|
||||
},
|
||||
{
|
||||
"name": "Ryzen 9 7950X3D",
|
||||
"description": "Desktop CPU 16c/32t"
|
||||
"name": "GPU",
|
||||
"attributedefinitions": [
|
||||
{
|
||||
"name": "Brand",
|
||||
"dataType": 0
|
||||
},
|
||||
{
|
||||
"name": "VRAM_GB",
|
||||
"dataType": 1
|
||||
},
|
||||
{
|
||||
"name": "Chipset",
|
||||
"dataType": 0
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
]
|
||||
###
|
||||
|
||||
DELETE http://localhost:5244/api/v1/InventoryType/6
|
||||
|
||||
###
|
||||
@@ -5,5 +5,33 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"Serilog": {
|
||||
"Using": [
|
||||
"Serilog.Sinks.Console"
|
||||
],
|
||||
"MinimumLevel": {
|
||||
"Default": "Information",
|
||||
"Override": {
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information",
|
||||
"System": "Warning",
|
||||
"Microsoft.EntityFrameworkCore": "Warning"
|
||||
}
|
||||
},
|
||||
"Enrich": [
|
||||
"FromLogContext"
|
||||
],
|
||||
"WriteTo": [
|
||||
{
|
||||
"Name": "Console",
|
||||
"Args": {
|
||||
"outputTemplate": "[{Timestamp:MM-dd-yyyy HH:mm:ss} {Level:u3}] {Message:lj}{NewLine}{Exception}"
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
"Database": {
|
||||
"ConnectionString": ""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user