From cfb0caf0f7ea87862a9a8af389f870a3aad7af70 Mon Sep 17 00:00:00 2001 From: the Date: Wed, 17 May 2023 12:19:00 +0400 Subject: [PATCH] Add and delete for assembly --- .../BusinessLogics/AssemblyLogic.cs | 8 +- .../OfficePackage/AbstractSaveToExcel.cs | 143 +++++++++--------- .../Controllers/HomeController.cs | 46 ++++-- .../Views/Home/Assembly.cshtml | 9 ++ .../Views/Home/CreateAssembly.cshtml | 65 +------- .../Views/Home/DeleteAssembly.cshtml | 16 ++ .../SearchModels/AssemblySearchModel.cs | 1 + .../SearchModels/ComponentSearchModel.cs | 1 + .../Implements/AssemblyStorage.cs | 27 +++- .../Controllers/AssemblyController.cs | 21 ++- 10 files changed, 180 insertions(+), 157 deletions(-) create mode 100644 ComputerShopProvider/ComputerShopClientApp/Views/Home/DeleteAssembly.cshtml diff --git a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs index f6d776a..1f7455d 100644 --- a/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs +++ b/ComputerShopProvider/ComputerShopBusinessLogic/BusinessLogics/AssemblyLogic.cs @@ -101,10 +101,10 @@ namespace ComputerShopBusinessLogic.BusinessLogics { throw new ArgumentNullException("Нет названия сборки", nameof(model.AssemblyName)); } - if (model.Price <= 0) - { - throw new ArgumentNullException("Стоимость сборки должна быть больше 0", nameof(model.Price)); - } + //if (model.Price <= 0) + //{ + // throw new ArgumentNullException("Стоимость сборки должна быть больше 0", nameof(model.Price)); + //} _logger.LogInformation("Assembly. ComputerName:{AssemblyName}.Price:{ Price}. Id: { Id}", model.AssemblyName, model.Price, model.Id); var element = _assemblyStorage.GetElement(new AssemblySearchModel { 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..ff12442 100644 --- a/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs +++ b/ComputerShopProvider/ComputerShopClientApp/Controllers/HomeController.cs @@ -38,7 +38,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] @@ -208,25 +208,45 @@ namespace ComputerShopClientApp.Controllers } [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"); } 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/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/ComputerShopRestApi/Controllers/AssemblyController.cs b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs index f3d37ef..154fcdf 100644 --- a/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs +++ b/ComputerShopProvider/ComputerShopRestApi/Controllers/AssemblyController.cs @@ -1,5 +1,6 @@ using ComputerShopContracts.BindingModels; using ComputerShopContracts.BusinessLogicContracts; +using ComputerShopContracts.SearchModels; using ComputerShopContracts.ViewModels; using Microsoft.AspNetCore.Mvc; @@ -19,11 +20,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 +61,18 @@ namespace ComputerShopRestApi.Controllers throw; } } + [HttpPost] + public void DeleteAssembly(AssemblyBindingModel model) + { + try + { + _assembly.Delete(model); + } + catch (Exception ex) + { + _logger.LogError(ex, "Ошибка удаления сборки"); + throw; + } + } } }