diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs index f6d776a..5568228 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs @@ -3,6 +3,7 @@ using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.SearchModels; using ComputerShopContracts.StorageContracts; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; @@ -16,11 +17,13 @@ namespace ComputerShopBusinessLogic.BusinessLogics { private readonly ILogger _logger; private readonly IAssemblyStorage _assemblyStorage; + private readonly IComponentStorage _componentStorage; - public AssemblyLogic(ILogger logger, IAssemblyStorage assemblyStorage) + public AssemblyLogic(ILogger logger, IAssemblyStorage assemblyStorage, IComponentStorage componentStorage) { _logger = logger; _assemblyStorage = assemblyStorage; + _componentStorage = componentStorage; } public bool Create(AssemblyBindingModel model) @@ -87,6 +90,38 @@ namespace ComputerShopBusinessLogic.BusinessLogics return true; } + public bool AddComponentToAssembly(AssemblySearchModel model, ComponentSearchModel componentmodel, int amount) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("AddComponentToAssembly. AssemblyName:{AssemblyName}.Id:{ Id}", model.AssemblyName, model.Id); + var element = _assemblyStorage.GetElement(model); + var component = _componentStorage.GetElement(componentmodel); + + if (element == null || component == null) + { + return false; + } + + _logger.LogInformation("AddComponentToAssembly find. Id:{Id}", element.Id); + + element.AssemblyComponents[component.Id] = (component, amount); + + _assemblyStorage.Update(new() + { + Id = element.Id, + AssemblyName = element.AssemblyName, + Price = element.Price + component.Cost * amount, + ClientId = element.ClientId, + AssemblyComponents = element.AssemblyComponents, + }); + + return true; + } + private void CheckModel(AssemblyBindingModel model, bool withParams = true) { if (model == null) diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs index 664144e..8228476 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -24,8 +24,7 @@ namespace ComputerShopBusinessLogic.BusinessLogics public List? ReadList(ComponentSearchModel? model) { _logger.LogInformation("ReadList. ComponentName:{ComponentName}. Id:{ Id}", model?.ComponentName, model?.Id); - var list = model == null ? _componentStorage.GetFullList() : - _componentStorage.GetFilteredList(model); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); if (list == null) { _logger.LogWarning("ReadList return null list"); diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs index 866ff89..3ef20ce 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/OfficePackage/AbstractSaveToExcel.cs @@ -7,78 +7,77 @@ using System.Threading.Tasks; namespace ComputerShopBusinessLogic.OfficePackage { - public class AbstractSaveToExcel + public abstract class AbstractSaveToExcel { - public void CreateReport(ExcelInfo info) - { - CreateExcel(info); - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "A", - RowIndex = 1, - Text = info.Title, - StyleInfo = ExcelStyleInfoType.Title - }); - MergeCells(new ExcelMergeParameters - { - CellFromName = "A1", - CellToName = "C1" - }); - uint rowIndex = 2; - foreach (var pc in info.DocumentBlanks) - { - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "A", - RowIndex = rowIndex, - Text = pc.DocumentName, - StyleInfo = ExcelStyleInfoType.Text - }); - rowIndex++; - foreach (var (Blank, Count) in pc.Blanks) - { - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "B", - RowIndex = rowIndex, - Text = Blank, - StyleInfo = ExcelStyleInfoType.TextWithBorder - }); - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "C", - RowIndex = rowIndex, - Text = Count.ToString(), - StyleInfo = ExcelStyleInfoType.TextWithBorder - }); - rowIndex++; - } - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "A", - RowIndex = rowIndex, - Text = "Итого", - StyleInfo = ExcelStyleInfoType.Text - }); - InsertCellInWorksheet(new ExcelCellParameters - { - ColumnName = "C", - RowIndex = rowIndex, - Text = pc.TotalCount.ToString(), - StyleInfo = ExcelStyleInfoType.Text - }); - rowIndex++; - } - SaveExcel(info); - } - /// Создание excel-файла - protected abstract void CreateExcel(ExcelInfo info); - /// Добавляем новую ячейку в лист - protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); - /// Объединение ячеек - protected abstract void MergeCells(ExcelMergeParameters excelParams); - /// Сохранение файла - protected abstract void SaveExcel(ExcelInfo info); + //public void CreateReport(ExcelInfo info) + //{ + // CreateExcel(info); + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "A", + // RowIndex = 1, + // Text = info.Title, + // StyleInfo = ExcelStyleInfoType.Title + // }); + // MergeCells(new ExcelMergeParameters + // { + // CellFromName = "A1", + // CellToName = "C1" + // }); + // uint rowIndex = 2; + // foreach (var pc in info.DocumentBlanks) + // { + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "A", + // RowIndex = rowIndex, + // Text = pc.DocumentName, + // StyleInfo = ExcelStyleInfoType.Text + // }); + // rowIndex++; + // foreach (var (Blank, Count) in pc.Blanks) + // { + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "B", + // RowIndex = rowIndex, + // Text = Blank, + // StyleInfo = ExcelStyleInfoType.TextWithBorder + // }); + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "C", + // RowIndex = rowIndex, + // Text = Count.ToString(), + // StyleInfo = ExcelStyleInfoType.TextWithBorder + // }); + // rowIndex++; + // } + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "A", + // RowIndex = rowIndex, + // Text = "Итого", + // StyleInfo = ExcelStyleInfoType.Text + // }); + // InsertCellInWorksheet(new ExcelCellParameters + // { + // ColumnName = "C", + // RowIndex = rowIndex, + // Text = pc.TotalCount.ToString(), + // StyleInfo = ExcelStyleInfoType.Text + // }); + // rowIndex++; + // } + // SaveExcel(info); + //} + ///// Создание excel-файла + //protected abstract void CreateExcel(ExcelInfo info); + ///// Добавляем новую ячейку в лист + //protected abstract void InsertCellInWorksheet(ExcelCellParameters excelParams); + ///// Объединение ячеек + //protected abstract void MergeCells(ExcelMergeParameters excelParams); + ///// Сохранение файла + //protected abstract void SaveExcel(ExcelInfo info); } } -} diff --git a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs index aae16f4..96c8fda 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs +++ b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs @@ -1,5 +1,6 @@ using ComputerShopClientApp.Models; using ComputerShopContracts.BindingModels; +using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; using System.Diagnostics; @@ -30,7 +31,7 @@ namespace ComputerShopClientApp.Controllers { return Redirect("~/Home/Enter"); } - return View(APIClient.GetRequest>($"api/component/getcomponentlist")); + return View(APIClient.GetRequest>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}")); } public IActionResult Assembly() { @@ -38,7 +39,7 @@ namespace ComputerShopClientApp.Controllers { return Redirect("~/Home/Enter"); } - return View(APIClient.GetRequest>($"api/assembly/getassemblylist")); + return View(APIClient.GetRequest>($"api/Assembly/getassemblylist?clientId={APIClient.Client.Id}")); } [HttpGet] @@ -129,7 +130,7 @@ namespace ComputerShopClientApp.Controllers [HttpGet] public IActionResult CreatePurchase() { - ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentlist"); + ViewBag.Components = APIClient.GetRequest>($"api/main/getcomponentlist?clientId={APIClient.Client.Id}"); return View(); } @@ -182,7 +183,7 @@ namespace ComputerShopClientApp.Controllers [HttpGet] public IActionResult DeleteComponent() { - ViewBag.Components = APIClient.GetRequest>("api/main/getcomponentlist"); + ViewBag.Components = APIClient.GetRequest>($"api/main/getcomponentlist?clientId={APIClient.Client.Id}"); return View(); } [HttpDelete] @@ -202,31 +203,77 @@ namespace ComputerShopClientApp.Controllers [HttpGet] public IActionResult CreateAssembly() { - ViewBag.Components = APIClient.GetRequest>("api/component/getcomponentlist"); + ViewBag.Components = APIClient.GetRequest>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}"); ViewBag.CurrentComponents = new List(); return View(); } [HttpPost] - public void CreateAssembly(string name, int sum) + public void CreateAssembly(string name, int cost) { if (APIClient.Client == null) { throw new Exception("Вы как суда попали? Суда вход только авторизованным"); } - if (sum <= 0) + APIClient.PostRequest("api/assembly/createassembly", new AssemblyBindingModel { - throw new Exception("Сумма должна быть больше 0"); - } - //APIClient.PostRequest("api/assembly/createassembly", new AssemblyBindingModel - //{ - // ClientId = APIClient.Client.Id, - // AssemblyName = name, - // Price = 0, - // AssemblyComponents = new() - //}); + ClientId = APIClient.Client.Id, + AssemblyName = name, + Price = cost, + }); System.Diagnostics.Debug.WriteLine("it might work"); - Response.Redirect("Index"); + Response.Redirect("Assembly"); + } + + [HttpGet] + public IActionResult DeleteAssembly() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Assemblies = APIClient.GetRequest>($"api/assembly/GetAssemblyList?clientId={APIClient.Client.Id}"); + return View(); + } + + [HttpPost] + public void DeleteAssembly(int assembly) + { + if (APIClient.Client == null) + { + throw new Exception("Необходима авторизация"); + } + APIClient.PostRequest("api/assembly/deleteassembly", new AssemblyBindingModel + { + Id = assembly + }); + Response.Redirect("Assembly"); + } + + public IActionResult AddComponentToAssembly() + { + if (APIClient.Client == null) + { + return Redirect("~/Home/Enter"); + } + ViewBag.Assemblies = APIClient.GetRequest>($"api/assembly/getassemblylist?clientId={APIClient.Client.Id}"); + ViewBag.Components = APIClient.GetRequest>($"api/component/getcomponentlist?clientId={APIClient.Client.Id}"); + return View(); + } + + [HttpPost] + public void AddComponentToAssembly(int assembly, int component, int amount) + { + if (APIClient.Client == null) + { + throw new Exception("Необходима авторизация"); + } + APIClient.PostRequest("api/assembly/AddComponentToAssembly", Tuple.Create( + new AssemblySearchModel() { Id = assembly }, + new ComponentSearchModel() { Id = component }, + amount + )); + Response.Redirect("Assembly"); } diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/AddComponentToAssembly.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/AddComponentToAssembly.cshtml new file mode 100644 index 0000000..cd5ad58 --- /dev/null +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/AddComponentToAssembly.cshtml @@ -0,0 +1,31 @@ +@using ComputerShopContracts.ViewModels; +@using ComputerShopDataModels.Models; + +@{ + ViewData["Title"] = "AddComponentToAssembly"; +} + +@model Dictionary + +
+
+ +
+ +
+
+
+ +
+ +
+
+
+
Количество:
+
+
+
+
+
+
+
\ No newline at end of file diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/Assembly.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/Assembly.cshtml index cb31371..6d45e79 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Views/Home/Assembly.cshtml +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/Assembly.cshtml @@ -22,6 +22,15 @@

Создать сборку

+

+ Изменить сборку +

+

+ Удалить сборку +

+

+ Добавить комплектующее к сборке +

diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/CreateAssembly.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/CreateAssembly.cshtml index 26a977f..0167a66 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Views/Home/CreateAssembly.cshtml +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/CreateAssembly.cshtml @@ -1,6 +1,4 @@ -@using ComputerShopContracts.ViewModels - -@{ +@{ ViewData["Title"] = "CreateAssembly"; }
@@ -8,64 +6,15 @@
-
Компонент:
-
- -
+
Название:
+
-
Количество:
-
-
-
-
Сумма:
-
+
Цена:
+
-
+
-
-
-
-
- - -
-

Список компонентов

-
-
- @{ -
- - - - - - - - - @foreach (var item in (ViewBag.CurrentComponents as IEnumerable)) - { - - - - - - } - -
- Номер - - Компонент - - Цена -
- @Html.DisplayFor(modelItem => item.Id) - - @Html.DisplayFor(modelItem => item.ComponentName) - - @Html.DisplayFor(modelItem => item.Cost) -
- } - \ No newline at end of file + \ No newline at end of file diff --git a/ComputerShopProvider/ComputerShopClientApp/Views/Home/DeleteAssembly.cshtml b/ComputerShopProvider/ComputerShopClientApp/Views/Home/DeleteAssembly.cshtml new file mode 100644 index 0000000..f5f58da --- /dev/null +++ b/ComputerShopProvider/ComputerShopClientApp/Views/Home/DeleteAssembly.cshtml @@ -0,0 +1,16 @@ +@{ + ViewData["Title"] = "DeleteAssembly"; +} + +
+
+ +
+ +
+
+
+
+
+
+
\ No newline at end of file diff --git a/ComputerShopProvider/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs b/ComputerShopProvider/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs index f346b5c..a0c9823 100644 --- a/ComputerShopProvider/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs +++ b/ComputerShopProvider/ComputerShopContracts/BusinessLogicContracts/IAssemblyLogic.cs @@ -1,6 +1,7 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; using System; using System.Collections.Generic; using System.Linq; @@ -16,5 +17,6 @@ namespace ComputerShopContracts.BusinessLogicContracts bool Create(AssemblyBindingModel model); bool Update(AssemblyBindingModel model); bool Delete(AssemblyBindingModel model); + bool AddComponentToAssembly(AssemblySearchModel model, ComponentSearchModel componentmodel, int amount); } } diff --git a/ComputerShopProvider/ComputerShopContracts/SearchModels/AssemblySearchModel.cs b/ComputerShopProvider/ComputerShopContracts/SearchModels/AssemblySearchModel.cs index 120c88d..e7e202a 100644 --- a/ComputerShopProvider/ComputerShopContracts/SearchModels/AssemblySearchModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/SearchModels/AssemblySearchModel.cs @@ -10,5 +10,6 @@ namespace ComputerShopContracts.SearchModels { public int? Id { get; set; } public string? AssemblyName { get; set; } + public int? ClientId { get; set; } } } diff --git a/ComputerShopProvider/ComputerShopContracts/SearchModels/ComponentSearchModel.cs b/ComputerShopProvider/ComputerShopContracts/SearchModels/ComponentSearchModel.cs index 78421a7..c9ef869 100644 --- a/ComputerShopProvider/ComputerShopContracts/SearchModels/ComponentSearchModel.cs +++ b/ComputerShopProvider/ComputerShopContracts/SearchModels/ComponentSearchModel.cs @@ -10,5 +10,6 @@ namespace ComputerShopContracts.SearchModels { public int? Id { get; set; } public string? ComponentName { get; set; } + public int? ClientId { get; set; } } } diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs index 3ddd7a4..3889059 100644 --- a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/AssemblyStorage.cs @@ -27,18 +27,29 @@ namespace ComputerShopDatabaseImplement.Implements } public List GetFilteredList(AssemblySearchModel model) { - if (string.IsNullOrEmpty(model.AssemblyName)) + if (string.IsNullOrEmpty(model.AssemblyName) && model.ClientId == null) { return new(); } using var context = new ComputerShopDatabase(); - return context.Assemblies - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Where(x => x.AssemblyName.Contains(model.AssemblyName)) - .ToList() - .Select(x => x.GetViewModel) - .ToList(); + if (model.ClientId != null) + return context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ClientId == model.ClientId) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + else + { + return context.Assemblies + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.AssemblyName.Contains(model.AssemblyName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } } public AssemblyViewModel? GetElement(AssemblySearchModel model) { diff --git a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs index 13aac14..10c3c3c 100644 --- a/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputerShopProvider/ComputerShopDatabaseImplement/Implements/ComponentStorage.cs @@ -23,15 +23,25 @@ namespace ComputerShopDatabaseImplement.Implements public List GetFilteredList(ComponentSearchModel model) { - if (string.IsNullOrEmpty(model.ComponentName)) + if (string.IsNullOrEmpty(model.ComponentName) && model.ClientId == null) { return new(); } using var context = new ComputerShopDatabase(); - return context.Components - .Where(x => x.ComponentName.Contains(model.ComponentName)) - .Select(x => x.GetViewModel) - .ToList(); + if (!string.IsNullOrEmpty(model.ComponentName)) + { + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + else + { + return context.Components + .Where(x => x.ClientId == model.ClientId) + .Select(x => x.GetViewModel) + .ToList(); + } } public ComponentViewModel? GetElement(ComponentSearchModel model) { diff --git a/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs index f3d37ef..6b67628 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs @@ -1,6 +1,8 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using ComputerShopDataModels.Models; using Microsoft.AspNetCore.Mvc; namespace ComputerShopRestApi.Controllers @@ -19,11 +21,14 @@ namespace ComputerShopRestApi.Controllers _component = component; } [HttpGet] - public List? GetAssemblyList() + public List? GetAssemblyList(int clientId) { try { - return _assembly.ReadList(null); + return _assembly.ReadList(new AssemblySearchModel + { + ClientId = clientId, + }); } catch (Exception ex) { @@ -57,5 +62,32 @@ namespace ComputerShopRestApi.Controllers throw; } } + [HttpPost] + public void DeleteAssembly(AssemblyBindingModel model) + { + try + { + _assembly.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления сборки"); + throw; + } + } + + [HttpPost] + public void AddComponentToAssembly(Tuple model) + { + try + { + _assembly.AddComponentToAssembly(model.Item1, model.Item2, model.Item3); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка добавления компонента в сборку."); + throw; + } + } } } diff --git a/ComputerShopProvider/ComputerShopRestApi/Controllers/ComponentController.cs b/ComputerShopProvider/ComputerShopRestApi/Controllers/ComponentController.cs index 91e20b1..eea994d 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Controllers/ComponentController.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Controllers/ComponentController.cs @@ -3,6 +3,7 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.BusinessLogicContracts; using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; +using System.Reflection; namespace ComputerShopRestApi.Controllers { @@ -18,11 +19,14 @@ namespace ComputerShopRestApi.Controllers _component = component; } [HttpGet] - public List? GetComponentList() + public List? GetComponentList(int clientId) { try { - return _component.ReadList(null); + return _component.ReadList(new ComponentSearchModel + { + ClientId = clientId, + }); } catch (Exception ex) {