diff options
-rw-r--r-- | RhSolutions.Api/Controllers/ProductsController.cs | 153 | ||||
-rw-r--r-- | RhSolutions.Api/Controllers/SearchController.cs | 5 | ||||
-rw-r--r-- | RhSolutions.Api/Program.cs | 22 | ||||
-rw-r--r-- | RhSolutions.Api/RhSolutions.Api.csproj | 2 |
4 files changed, 113 insertions, 69 deletions
diff --git a/RhSolutions.Api/Controllers/ProductsController.cs b/RhSolutions.Api/Controllers/ProductsController.cs index 550ea9d..99c0af0 100644 --- a/RhSolutions.Api/Controllers/ProductsController.cs +++ b/RhSolutions.Api/Controllers/ProductsController.cs @@ -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"); + } + } }
\ No newline at end of file diff --git a/RhSolutions.Api/Controllers/SearchController.cs b/RhSolutions.Api/Controllers/SearchController.cs index cf35209..f0402ec 100644 --- a/RhSolutions.Api/Controllers/SearchController.cs +++ b/RhSolutions.Api/Controllers/SearchController.cs @@ -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) { diff --git a/RhSolutions.Api/Program.cs b/RhSolutions.Api/Program.cs index c4ac88d..eb6d7f3 100644 --- a/RhSolutions.Api/Program.cs +++ b/RhSolutions.Api/Program.cs @@ -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(); diff --git a/RhSolutions.Api/RhSolutions.Api.csproj b/RhSolutions.Api/RhSolutions.Api.csproj index fac265e..f32d9e4 100644 --- a/RhSolutions.Api/RhSolutions.Api.csproj +++ b/RhSolutions.Api/RhSolutions.Api.csproj @@ -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> |