From 27dd2af627553ee782a7f3524a91fc1e40292a51 Mon Sep 17 00:00:00 2001 From: Serghei Cebotari Date: Sun, 14 Jan 2024 23:03:52 +0300 Subject: [PATCH] Add API documentation --- .../Controllers/ProductsController.cs | 153 ++++++++++-------- .../Controllers/SearchController.cs | 5 + RhSolutions.Api/Program.cs | 22 ++- 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; + } + + /// + /// Возвращает все продукты в базе данных + /// + /// + [HttpGet] + public IAsyncEnumerable GetProducts() + { + return dbContext.Products + .AsAsyncEnumerable(); + } - [HttpGet] - public IAsyncEnumerable GetProducts() - { - return dbContext.Products - .AsAsyncEnumerable(); - } + /// + /// Возвращает продукт по номеру артикула + /// + /// Номер артикула + /// + [HttpGet("{id}")] + public IEnumerable GetProduct(string id) + { + return dbContext.Products + .Where(p => p.Id.Equals(id)); + } - [HttpGet("{id}")] - public IEnumerable GetProduct(string id) - { - return dbContext.Products - .Where(p => p.Id.Equals(id)); - } + /// + /// Загрузка прайс-листа в формате xlsx в базу данных + /// + /// + [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(p); + } - foreach (var p in products) - { - dbContext.Add(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 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"); - } - } + /// + /// Удаление всех продуктов из базы данных + /// + /// + [HttpDelete] + public IActionResult DeleteAllProducts() + { + List 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; } + /// + /// Поиск артикула в базе данных через предварительную мультиклассовую классификацию с применением ML-модели артикулов + /// + /// Запрос в свободной форме + /// [HttpGet] public IAsyncEnumerable 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("Монтаж .AddKeyedTransient("Stabil") .AddKeyedTransient("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 @@ enable enable 1c307973-55cf-4d5c-a4f8-1def6b58ee3c + true + $(NoWarn);1591