0
0

Add API documentation

This commit is contained in:
Serghei Cebotari 2024-01-14 23:03:52 +03:00
parent 0452f379c6
commit 27dd2af627
4 changed files with 113 additions and 69 deletions

View File

@ -5,78 +5,95 @@ using System.Linq;
namespace RhSolutions.Api.Controllers
{
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private RhSolutionsContext dbContext;
private IPricelistParser parser;
[Route("api/[controller]")]
public class ProductsController : ControllerBase
{
private RhSolutionsContext dbContext;
private IPricelistParser parser;
public ProductsController(RhSolutionsContext dbContext, IPricelistParser parser)
{
this.dbContext = dbContext;
this.parser = parser;
}
public ProductsController(RhSolutionsContext dbContext, IPricelistParser parser)
{
this.dbContext = dbContext;
this.parser = parser;
}
/// <summary>
/// Возвращает все продукты в базе данных
/// </summary>
/// <returns></returns>
[HttpGet]
public IAsyncEnumerable<Product> GetProducts()
{
return dbContext.Products
.AsAsyncEnumerable();
}
[HttpGet]
public IAsyncEnumerable<Product> GetProducts()
{
return dbContext.Products
.AsAsyncEnumerable();
}
/// <summary>
/// Возвращает продукт по номеру артикула
/// </summary>
/// <param name="id">Номер артикула</param>
/// <returns></returns>
[HttpGet("{id}")]
public IEnumerable<Product> GetProduct(string id)
{
return dbContext.Products
.Where(p => p.Id.Equals(id));
}
[HttpGet("{id}")]
public IEnumerable<Product> GetProduct(string id)
{
return dbContext.Products
.Where(p => p.Id.Equals(id));
}
/// <summary>
/// Загрузка прайс-листа в формате xlsx в базу данных
/// </summary>
/// <returns></returns>
[HttpPost]
public IActionResult PostProductsFromXls()
{
try
{
var products = parser.GetProducts(HttpContext).GroupBy(p => p.ProductSku)
.Select(g => new Product(g.Key)
{
Name = g.First().Name,
DeprecatedSkus = g.SelectMany(p => p.DeprecatedSkus).Distinct().ToList(),
ProductLines = g.SelectMany(p => p.ProductLines).Distinct().ToList(),
IsOnWarehouse = g.Any(p => p.IsOnWarehouse == true),
ProductMeasure = g.First().ProductMeasure,
DeliveryMakeUp = g.First().DeliveryMakeUp,
Price = g.First().Price
});
[HttpPost]
public IActionResult PostProductsFromXls()
{
try
{
var products = parser.GetProducts(HttpContext).GroupBy(p => p.ProductSku)
.Select(g => new Product(g.Key)
{
Name = g.First().Name,
DeprecatedSkus = g.SelectMany(p => p.DeprecatedSkus).Distinct().ToList(),
ProductLines = g.SelectMany(p => p.ProductLines).Distinct().ToList(),
IsOnWarehouse = g.Any(p => p.IsOnWarehouse == true),
ProductMeasure = g.First().ProductMeasure,
DeliveryMakeUp = g.First().DeliveryMakeUp,
Price = g.First().Price
});
foreach (var p in products)
{
dbContext.Add<Product>(p);
}
foreach (var p in products)
{
dbContext.Add<Product>(p);
}
dbContext.SaveChanges();
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
dbContext.SaveChanges();
return Ok();
}
catch (Exception ex)
{
return BadRequest(ex.Message);
}
}
[HttpDelete]
public IActionResult DeleteAllProducts()
{
List<Product> deleted = new();
if (dbContext.Products.Count() > 0)
{
foreach (Product p in dbContext.Products)
{
deleted.Add(p);
dbContext.Remove(p);
}
dbContext.SaveChanges();
return Ok(deleted);
}
else return Ok("Empty db");
}
}
/// <summary>
/// Удаление всех продуктов из базы данных
/// </summary>
/// <returns></returns>
[HttpDelete]
public IActionResult DeleteAllProducts()
{
List<Product> deleted = new();
if (dbContext.Products.Count() > 0)
{
foreach (Product p in dbContext.Products)
{
deleted.Add(p);
dbContext.Remove(p);
}
dbContext.SaveChanges();
return Ok(deleted);
}
else return Ok("Empty db");
}
}
}

View File

@ -14,6 +14,11 @@ namespace RhSolutions.Api.Controllers
this.context = context;
}
/// <summary>
/// Поиск артикула в базе данных через предварительную мультиклассовую классификацию с применением ML-модели артикулов
/// </summary>
/// <param name="query">Запрос в свободной форме</param>
/// <returns></returns>
[HttpGet]
public IAsyncEnumerable<Product> SearchProducts([FromQuery] string query)
{

View File

@ -5,6 +5,8 @@ using RhSolutions.Api.Middleware;
using RhSolutions.QueryModifiers;
using RhSolutions.QueryModifiers.DrinkingWaterHeatingFittings;
using RhSolutions.QueryModifiers.DrinkingWaterHeatingPipes;
using Microsoft.OpenApi.Models;
using System.Reflection;
var builder = WebApplication.CreateBuilder(args);
@ -60,7 +62,25 @@ builder.Services.AddKeyedTransient<IProductQueryModifier, Sleeve>("Монтаж
.AddKeyedTransient<IProductQueryModifier, StabilPipe>("Stabil")
.AddKeyedTransient<IProductQueryModifier, BlackPipe>("Black");
builder.Services.AddSwaggerGen();
builder.Services.AddSwaggerGen(options =>
{
options.SwaggerDoc("v1", new OpenApiInfo
{
Version = "v1",
Title = "RhSolutions API",
Description = "API к базе данных артикулов РЕХАУ для поиска с помощью ML.NET и полнотестового поиска PostgreSQL",
Contact = new OpenApiContact
{
Name = "Serghei Cebotari",
Url = new Uri("https://cebotari.ru/"),
Email = @"serghei@cebotari.ru"
}
});
var xmlFilename = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
options.IncludeXmlComments(Path.Combine(AppContext.BaseDirectory, xmlFilename));
});
var app = builder.Build();

View File

@ -5,6 +5,8 @@
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UserSecretsId>1c307973-55cf-4d5c-a4f8-1def6b58ee3c</UserSecretsId>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);1591</NoWarn>
</PropertyGroup>
<ItemGroup>