From 7938a755b7b0e5617d74a2d71db34bfc134f1e70 Mon Sep 17 00:00:00 2001 From: devil_1nc Date: Mon, 24 Jun 2024 16:04:51 +0400 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B0=20=D1=83=D0=B6=20=D0=B4=D1=83?= =?UTF-8?q?=D1=85=D0=BE=D1=82=D0=B8=D1=89=D0=B0=20=D0=BD=D0=BE=20=D1=8F=20?= =?UTF-8?q?=D0=BD=D0=B0=D1=87=D0=B0=D0=BB=20=D1=85=D0=BE=D1=82=D1=8F=20?= =?UTF-8?q?=D0=B1=D1=8B=20=D0=BA=D0=BE=D0=BD=D1=82=D1=80=D0=BE=D0=BB=D0=BB?= =?UTF-8?q?=D0=B5=D1=80=D1=8B=20=D0=B8=20=D0=B2=D1=8C=D1=8E=D1=88=D0=BA?= =?UTF-8?q?=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DatabaseImplement/Models/Sell.cs | 18 +++++++ RestAPI/Controllers/MainController.cs | 78 +++++++++++++++++++++++++++ WebApp/Pages/Catalog.cshtml | 52 ++++++++++++++++++ WebApp/Pages/Shared/_Layout.cshtml | 12 ++++- 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 RestAPI/Controllers/MainController.cs diff --git a/DatabaseImplement/Models/Sell.cs b/DatabaseImplement/Models/Sell.cs index 9bd3754..14624b2 100644 --- a/DatabaseImplement/Models/Sell.cs +++ b/DatabaseImplement/Models/Sell.cs @@ -80,5 +80,23 @@ namespace DatabaseImplement.Models DateSell = model.DateSell; UserId = model.UserId; } + public void UpdateProducts(Database context, SellBindingModel model) + { + var sellProducts = context.SellProducts.Where(rec => + rec.Id == model.Id).ToList(); + if (sellProducts != null && sellProducts.Count > 0) + { // удалили те, которых нет в модели + context.SellProducts.RemoveRange(sellProducts.Where(rec + => !model.SellProducts.ContainsKey(rec.ProductId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateProduct in sellProducts) + { + updateProduct.Count = model.SellProducts[updateProduct.ProductId].Item2; + model.SellProducts.Remove(updateProduct.ProductId); + } + context.SaveChanges(); + } + } } } diff --git a/RestAPI/Controllers/MainController.cs b/RestAPI/Controllers/MainController.cs new file mode 100644 index 0000000..6afdc21 --- /dev/null +++ b/RestAPI/Controllers/MainController.cs @@ -0,0 +1,78 @@ +using BusinessLogic.BusinessLogic; +using Contracts.BindingModels; +using Contracts.BusinessLogicContracts; +using Contracts.Exceptions; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Microsoft.AspNetCore.Http.HttpResults; +using Microsoft.AspNetCore.Mvc; + +namespace RestAPI.Controllers +{ + [Route("[controller]/[action]")] + [ApiController] + public class MainController + { + private readonly ILogger _logger; + private readonly IProductLogic _product; + + public MainController(ILogger logger, IProductLogic productLogic) + { + _logger = logger; + _product = productLogic; + } + [HttpGet] + public List? GetProductList() + { + try + { + return _product.ReadList(null); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка получения списка пакетов"); + throw; + } + } + + [HttpPatch] + public IResult Update([FromBody] ProductBindingModel model) + { + try + { + var res = _product.Update(model); + return Results.Ok(res); + } + catch (AccountException ex) + { + _logger.LogWarning(ex, "Wrong update data"); + return Results.BadRequest(ex.Message); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error update user"); + return Results.Problem(ex.Message); + } + } + + [HttpDelete] + public IResult Delete(Guid id) + { + try + { + var res = _product.Delete(new() { Id = id }); + return Results.Ok(res); + } + catch (ElementNotFoundException ex) + { + _logger.LogInformation(ex, "User not found"); + return Results.NoContent(); + } + catch (Exception ex) + { + _logger.LogError(ex, "Error delete user"); + return Results.Problem(ex.Message); + } + } + } +} diff --git a/WebApp/Pages/Catalog.cshtml b/WebApp/Pages/Catalog.cshtml index 7aac31b..e548507 100644 --- a/WebApp/Pages/Catalog.cshtml +++ b/WebApp/Pages/Catalog.cshtml @@ -1,4 +1,56 @@ @page @model WebApp.Pages.CatalogModel @{ + ViewData["Title"] = "Каталог оружия"; } + + + +@section Scripts { + +} \ No newline at end of file diff --git a/WebApp/Pages/Shared/_Layout.cshtml b/WebApp/Pages/Shared/_Layout.cshtml index e7560e9..24afbbf 100644 --- a/WebApp/Pages/Shared/_Layout.cshtml +++ b/WebApp/Pages/Shared/_Layout.cshtml @@ -7,6 +7,10 @@ + + + +
@@ -23,10 +27,16 @@ Home + @* *@ @if (User.Identity.IsAuthenticated) { +