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) { +