diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs index 49a2435..60ad2c5 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/BuildLogic.cs @@ -10,10 +10,12 @@ namespace HardwareShopContracts.BusinessLogicsContracts { private readonly ILogger _logger; private readonly IBuildStorage _buildStorage; - public BuildLogic(ILogger logger, IBuildStorage buildStorage) + private readonly IComponentStorage _componentStorage; + public BuildLogic(ILogger logger, IBuildStorage buildStorage, IComponentStorage componentStorage) { _logger = logger; _buildStorage = buildStorage; + _componentStorage = componentStorage; } public List? ReadList(BuildSearchModel? model) @@ -106,7 +108,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts { throw new ArgumentNullException("Некорректный идентификатор у сборки", nameof(model.Id)); } - _logger.LogInformation("Build. BuildName:{BuildName}. Price:{Price}. Id:{Id}", model.BuildName, model.Price, model.Id); + _logger.LogInformation("Build. UserId:{UserId}. BuildName:{BuildName}. Price:{Price}. Id:{Id}", model.UserId, model.BuildName, model.Price, model.Id); var element = _buildStorage.GetElement(new BuildSearchModel { BuildName = model.BuildName diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs index 4d4a130..23ef980 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/CommentLogic.cs @@ -100,8 +100,12 @@ namespace HardwareShopContracts.BusinessLogicsContracts { throw new ArgumentNullException("Некорректный идентификатор у комментария", nameof(model.Id)); } + if (model.UserId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор у клиента", nameof(model.UserId)); + } - _logger.LogInformation("Comment. BuildId:{BuildId}. Id:{Id}", model.BuildId, model.Id); + _logger.LogInformation("Comment. UserId:{UserId}. BuildId:{BuildId}. Id:{Id}", model.UserId, model.BuildId, model.Id); } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs index 7aa764f..8563aac 100644 --- a/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs +++ b/HardwareShop/HardwareShopBusinessLogic/BusinessLogics/Worker/PurchaseLogic.cs @@ -121,7 +121,7 @@ namespace HardwareShopContracts.BusinessLogicsContracts { throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); } - _logger.LogInformation("Purchase. PurchaseID:{Id}. Sum:{ Sum}", model.Id, model.Sum); + _logger.LogInformation("Purchase. UserId:{UserId}. PurchaseID:{Id}. Sum:{ Sum}", model.UserId, model.Id, model.Sum); } } } diff --git a/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs b/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs index efefdc9..7bdafe2 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/HomeController.cs @@ -82,6 +82,7 @@ namespace HardwareShopClientApp.Controllers } if ((int)APIClient.User.Role == 1) { + return RedirectToAction("MainWorker", "Worker"); return RedirectToAction("WorkerReport", "Worker"); } else diff --git a/HardwareShop/HardwareShopClientApp/Controllers/WorkerController.cs b/HardwareShop/HardwareShopClientApp/Controllers/WorkerController.cs index e43cbaf..9155dc8 100644 --- a/HardwareShop/HardwareShopClientApp/Controllers/WorkerController.cs +++ b/HardwareShop/HardwareShopClientApp/Controllers/WorkerController.cs @@ -17,11 +17,6 @@ namespace HardwareShopClientApp.Controllers _logger = logger; } - public IActionResult Builds() - { - return View(); - } - public IActionResult Comments() { return View(); @@ -40,18 +35,12 @@ namespace HardwareShopClientApp.Controllers [HttpGet] public IActionResult Purchases() { - //return View(); - //string login = (string)TempData["UserId"]; - return View(APIClient.GetRequest>($"api/client/getpurchases?UserId={APIClient.User.Id}")); } [HttpPost] public void Purchases(int id) { - //return View(); - //string login = (string)TempData["UserId"]; - Response.Redirect("Purchase"); } @@ -59,9 +48,6 @@ namespace HardwareShopClientApp.Controllers [HttpGet] public IActionResult Purchase() { - //return View(); - //string login = (string)TempData["UserId"]; - return View(); } @@ -69,5 +55,30 @@ namespace HardwareShopClientApp.Controllers { return View(); } + + [HttpGet] + public IActionResult Builds() + { + return View(); + } + + [HttpPost] + public void Builds(int id) + { + Response.Redirect("Build"); + } + + [HttpGet] + public IActionResult Build() + { + return View(); + } + + [HttpGet] + public IActionResult linkingBuild() + { + return View(); + } + } -} \ No newline at end of file +} diff --git a/HardwareShop/HardwareShopClientApp/Views/Worker/Build.cshtml b/HardwareShop/HardwareShopClientApp/Views/Worker/Build.cshtml new file mode 100644 index 0000000..107df0b --- /dev/null +++ b/HardwareShop/HardwareShopClientApp/Views/Worker/Build.cshtml @@ -0,0 +1,78 @@ + + +@using HardwareShopContracts.ViewModels +@{ + ViewData["Title"] = "Build"; +} + +@model List + +@section Header { +
+ +
+} +
+
+
+

Комплектующие

+
+
+ + +
+ +
+ + + + + + + + + + + +
+ Номер + + Комплектующее + + Цена + + Логин пользователя +
+
+ + + diff --git a/HardwareShop/HardwareShopClientApp/Views/Worker/Builds.cshtml b/HardwareShop/HardwareShopClientApp/Views/Worker/Builds.cshtml index 5bfc6f8..c32d47c 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Worker/Builds.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Worker/Builds.cshtml @@ -1,62 +1,81 @@ -@{ +@using HardwareShopContracts.ViewModels +@{ ViewData["Title"] = "Builds"; } +@model List + @section Header {
- -
+ + } -
-

Регистрация

-
-
-
- - -
-
- - -
-
- - -
-
- - -
- - -
- +
+
+
+

Сборки

+
+
+ + + + + + + + + + + + +
+ Номер + + Дата оплаты + + Сумма + + Статус + + Логин пользователя +
+
+
+
+ + + + + +
+
+ \ No newline at end of file diff --git a/HardwareShop/HardwareShopClientApp/Views/Worker/Comments.cshtml b/HardwareShop/HardwareShopClientApp/Views/Worker/Comments.cshtml index cebb316..bb67b2a 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Worker/Comments.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Worker/Comments.cshtml @@ -1,62 +1,105 @@ -@{ +@using HardwareShopContracts.ViewModels +@{ ViewData["Title"] = "Comments"; } +@model List + @section Header {
- -
+ + } -
-

Регистрация

-
-
-
- - -
-
- - -
-
- - -
-
- - -
- - -
+
+
+
+

Сборки

+
+
+ + + + + + + + + + + +
+ Номер + + Текст + + Название сборки + + Логин пользователя +
+
+
+
+ + + + +
+ + +
diff --git a/HardwareShop/HardwareShopClientApp/Views/Worker/MainWorker.cshtml b/HardwareShop/HardwareShopClientApp/Views/Worker/MainWorker.cshtml index e60f995..a2643be 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Worker/MainWorker.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Worker/MainWorker.cshtml @@ -27,6 +27,9 @@ +
diff --git a/HardwareShop/HardwareShopClientApp/Views/Worker/Purchase.cshtml b/HardwareShop/HardwareShopClientApp/Views/Worker/Purchase.cshtml index 1318430..cb2f1e0 100644 --- a/HardwareShop/HardwareShopClientApp/Views/Worker/Purchase.cshtml +++ b/HardwareShop/HardwareShopClientApp/Views/Worker/Purchase.cshtml @@ -30,6 +30,9 @@ + @@ -55,7 +58,7 @@ Цена - Пользователь + Логин пользователя Количество @@ -79,25 +82,25 @@ + + + } -
-

Регистрация

-
-
-
- - + +
+
+

Покупки

+
+
+ + + + + + + + + + + + +
+ Номер + + Дата оплаты + + Сумма + + Статус + + Логин пользователя +
+
-
- - -
-
- - -
-
- - -
- +
+ + + + + +
+ + - diff --git a/HardwareShop/HardwareShopContracts/BindingModels/BuildBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/BuildBindingModel.cs index 9caaf5f..d373725 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/BuildBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/BuildBindingModel.cs @@ -6,12 +6,12 @@ namespace HardwareShopContracts.BindingModels { public int Id { get; set; } - public decimal Price { get; set; } + public double Price { get; set; } public string BuildName { get; set; } = string.Empty; public int UserId { get; set; } - public Dictionary? BuildComponents { get; set; } + public Dictionary BuildPurchases { get; set; } = new(); } } diff --git a/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs index 098d088..49f4107 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/GoodBindingModel.cs @@ -12,7 +12,7 @@ namespace HardwareShopContracts.BindingModels public int UserId { get; set; } - public Dictionary GoodComponents + public Dictionary GoodsComponents { get; set; diff --git a/HardwareShop/HardwareShopContracts/BindingModels/PurchaseBindingModel.cs b/HardwareShop/HardwareShopContracts/BindingModels/PurchaseBindingModel.cs index 654d33b..ccaaf61 100644 --- a/HardwareShop/HardwareShopContracts/BindingModels/PurchaseBindingModel.cs +++ b/HardwareShop/HardwareShopContracts/BindingModels/PurchaseBindingModel.cs @@ -7,7 +7,7 @@ namespace HardwareShopContracts.BindingModels { public int Id { get; set; } - public decimal Sum { get; set; } + public double Sum { get; set; } public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен; @@ -15,8 +15,6 @@ namespace HardwareShopContracts.BindingModels public int UserId { get; set; } - public Dictionary? PurchaseBuilds { get; set; } - public Dictionary PurchaseGoods { get; set; } = new(); } } diff --git a/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs b/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs index 7a60b86..50a1ad4 100644 --- a/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs +++ b/HardwareShop/HardwareShopContracts/StoragesContracts/IPurchaseStorage.cs @@ -1,6 +1,7 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; +using HardwareShopDataModels.Models; namespace HardwareShopContracts.StoragesContracts { diff --git a/HardwareShop/HardwareShopContracts/ViewModels/BuildViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/BuildViewModel.cs index 68f0c74..4d80ce4 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/BuildViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/BuildViewModel.cs @@ -7,16 +7,17 @@ namespace HardwareShopContracts.ViewModels public int Id { get; set; } [DisplayName("Цена")] - public decimal Price { get; set; } + public double Price { get; set; } [DisplayName("Название компонента")] public string BuildName { get; set; } = string.Empty; - [DisplayName("Логин работника")] + [DisplayName("Логин пользователя")] public string UserLogin { get; set; } = string.Empty; public int UserId { get; set; } - public Dictionary? BuildComponents { get; set; } + public Dictionary BuildPurchases { get; set; } = new(); + } } diff --git a/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs index 15343a3..6620922 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/CommentViewModel.cs @@ -17,7 +17,7 @@ namespace HardwareShopContracts.ViewModels public int UserId { get; set; } - [DisplayName("Логин работника")] + [DisplayName("Логин пользователя")] public string UserLogin { get; set; } = string.Empty; } } diff --git a/HardwareShop/HardwareShopContracts/ViewModels/PurchaseViewModel.cs b/HardwareShop/HardwareShopContracts/ViewModels/PurchaseViewModel.cs index 2a6967a..ee1be3b 100644 --- a/HardwareShop/HardwareShopContracts/ViewModels/PurchaseViewModel.cs +++ b/HardwareShop/HardwareShopContracts/ViewModels/PurchaseViewModel.cs @@ -8,7 +8,7 @@ namespace HardwareShopContracts.ViewModels public int Id { get; set; } [DisplayName("Цена")] - public decimal Sum { get; set; } + public double Sum { get; set; } [DisplayName("Статус покупки")] public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен; @@ -18,11 +18,9 @@ namespace HardwareShopContracts.ViewModels public int UserId { get; set; } - [DisplayName("Логин работника")] + [DisplayName("Логин пользователя")] public string UserLogin { get; set; } = string.Empty; - public Dictionary? PurchaseBuilds { get; set; } - public Dictionary PurchaseGoods { get; set; } = new(); } } diff --git a/HardwareShop/HardwareShopDataModels/Models/IBuildModel.cs b/HardwareShop/HardwareShopDataModels/Models/IBuildModel.cs index 0540675..3e57d64 100644 --- a/HardwareShop/HardwareShopDataModels/Models/IBuildModel.cs +++ b/HardwareShop/HardwareShopDataModels/Models/IBuildModel.cs @@ -9,12 +9,12 @@ namespace HardwareShopDataModels.Models { public interface IBuildModel : IId { - decimal Price { get; } + double Price { get; } string BuildName { get; } int UserId { get; } - Dictionary? BuildComponents { get; } + Dictionary BuildPurchases { get; } } } diff --git a/HardwareShop/HardwareShopDataModels/Models/IPurchaseModel.cs b/HardwareShop/HardwareShopDataModels/Models/IPurchaseModel.cs index 69d4ef4..91420cf 100644 --- a/HardwareShop/HardwareShopDataModels/Models/IPurchaseModel.cs +++ b/HardwareShop/HardwareShopDataModels/Models/IPurchaseModel.cs @@ -4,17 +4,14 @@ namespace HardwareShopDataModels.Models { public interface IPurchaseModel : IId { - decimal Sum { get; } + double Sum { get; } PurchaseStatus PurchaseStatus { get; } - //через "?" обозначается что поле может быть null DateTime? DatePurchase { get; } int UserId { get; } - Dictionary? PurchaseBuilds { get; } - Dictionary PurchaseGoods { get; } } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs index 30bd71e..cd6992f 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs @@ -13,9 +13,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker { using var context = new HardwareShopDatabase(); return context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) .Include(x => x.User) .ToList() .Select(x => x.GetViewModel) @@ -24,17 +23,25 @@ namespace HardwareShopDatabaseImplement.Implements.Worker public List GetFilteredList(BuildSearchModel model) { - if (!model.UserId.HasValue) + if (string.IsNullOrEmpty(model.BuildName) && !model.UserId.HasValue) { return new(); } using var context = new HardwareShopDatabase(); + if (!string.IsNullOrEmpty(model.BuildName)) + { + return context.Builds + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) + .Include(x => x.BuildName.Contains(model.BuildName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } return context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) - .Include(x => x.User) - .Where(x => x.UserId == model.UserId) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) + .Include(x => x.UserId == model.UserId) .ToList() .Select(x => x.GetViewModel) .ToList(); @@ -48,9 +55,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker } using var context = new HardwareShopDatabase(); return context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) .Include(x => x.User) .Where(x => x.Id == model.Id) .FirstOrDefault(x => (!string.IsNullOrEmpty(model.BuildName) && x.BuildName == model.BuildName) || @@ -61,7 +67,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker public BuildViewModel? Insert(BuildBindingModel model) { using var context = new HardwareShopDatabase(); - var newBuild = Build.Create(context, model); + var newBuild = Build.Create(model); if (newBuild == null) { return null; @@ -69,9 +75,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker context.Builds.Add(newBuild); context.SaveChanges(); return context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) .Include(x => x.User) .Where(x => x.UserId == model.Id) .FirstOrDefault(x => x.Id == newBuild.Id) @@ -85,9 +90,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker try { var build = context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) .Include(x => x.User) .Where(x => x.UserId == model.UserId) .FirstOrDefault(x => x.Id == model.Id); @@ -97,7 +101,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker } build.Update(model); context.SaveChanges(); - build.UpdateComponents(context, model); + build.UpdatePurchases(context, model); transaction.Commit(); return build?.GetViewModel; } @@ -112,9 +116,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker { using var context = new HardwareShopDatabase(); var element = context.Builds - .Include(x => x.Components) - .ThenInclude(x => x.Component) - .Include(x => x.Comments) + .Include(x => x.Purchases) + .ThenInclude(x => x.Purchase) .Include(x => x.User) .Where(x => x.UserId == model.Id) .FirstOrDefault(rec => rec.Id == model.Id); diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs index 63dcaa6..9812184 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs @@ -21,7 +21,7 @@ namespace HardwareShopDatabaseImplement.Implements.Worker public List GetFilteredList(CommentSearchModel model) { - if (!model.Id.HasValue) + if (!model.BuildId.HasValue && !model.UserId.HasValue) { return new(); } @@ -35,13 +35,12 @@ namespace HardwareShopDatabaseImplement.Implements.Worker .Select(x => x.GetViewModel) .ToList(); } - return context.Comments - .Include(x => x.Build) - .Include(x => x.User) - .Where(x => x.BuildId == model.BuildId) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Build) + .Include(x => x.User) + .Where(x => x.BuildId == model.BuildId) + .Select(x => x.GetViewModel) + .ToList(); } public CommentViewModel? GetElement(CommentSearchModel model) diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs index 428a796..8013365 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs @@ -2,7 +2,9 @@ using HardwareShopContracts.SearchModels; using HardwareShopContracts.StoragesContracts; using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.ManyToMany; using HardwareShopDatabaseImplement.Models.Worker; +using HardwareShopDataModels.Models; using Microsoft.EntityFrameworkCore; namespace HardwareShopDatabaseImplement.Implements.Worker @@ -13,8 +15,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker { using var context = new HardwareShopDatabase(); return context.Purchases - .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .Select(x => x.GetViewModel) .ToList(); @@ -23,23 +25,17 @@ namespace HardwareShopDatabaseImplement.Implements.Worker public List GetFilteredList(PurchaseSearchModel model) { using var context = new HardwareShopDatabase(); - if (model.UserId.HasValue) + if (!model.UserId.HasValue) { - return context.Purchases - .Include(x => x.Builds) - .Include(x => x.Goods) - .Include(x => x.User) - .Where(x => x.UserId == model.UserId) - .Select(x => x.GetViewModel) - .ToList(); + return new(); } return context.Purchases - .Include(x => x.Builds) - .Include(x => x.Goods) - .Include(x => x.User) - .Where(x => x.Id == model.Id) - .Select(x => x.GetViewModel) - .ToList(); + .Include(x => x.Goods) + .ThenInclude(x => x.Good) + .Include(x => x.User) + .Where(x => x.UserId == model.UserId) + .Select(x => x.GetViewModel) + .ToList(); } public PurchaseViewModel? GetElement(PurchaseSearchModel model) @@ -50,8 +46,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker } using var context = new HardwareShopDatabase(); return context.Purchases - .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) ?.GetViewModel; @@ -68,8 +64,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker context.Purchases.Add(newPurchase); context.SaveChanges(); return context.Purchases - .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => x.Id == newPurchase.Id) ?.GetViewModel; @@ -82,8 +78,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker try { var purchase = context.Purchases - .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(x => x.Id == model.Id); if (purchase == null) @@ -93,7 +89,6 @@ namespace HardwareShopDatabaseImplement.Implements.Worker purchase.Update(model); context.SaveChanges(); purchase.UpdateGoods(context, model); - purchase.UpdateBuilds(context, model); transaction.Commit(); return purchase.GetViewModel; } @@ -108,8 +103,8 @@ namespace HardwareShopDatabaseImplement.Implements.Worker { using var context = new HardwareShopDatabase(); var element = context.Purchases - .Include(x => x.Builds) .Include(x => x.Goods) + .ThenInclude(x => x.Good) .Include(x => x.User) .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs index a870057..6c3d753 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs @@ -55,7 +55,7 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper GoodName = model.GoodName, Price = model.Price, UserId = model.UserId, - Components = model.GoodComponents.Select(x => new GoodComponent + Components = model.GoodsComponents.Select(x => new GoodComponent { Component = context.Components.First(y => y.Id == x.Key), Count = x.Value.Item2 @@ -80,7 +80,7 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper Price = Price, UserId = UserId, UserLogin = User.Login, - GoodComponents = GoodsComponents + GoodsComponents = GoodsComponents }; public void UpdateComponents(HardwareShopDatabase context, GoodBindingModel model) @@ -91,18 +91,18 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper { // удалили те, которых нет в модели context.GoodsComponents .RemoveRange(goodComponents - .Where(rec => !model.GoodComponents.ContainsKey(rec.ComponentId))); + .Where(rec => !model.GoodsComponents.ContainsKey(rec.ComponentId))); context.SaveChanges(); // обновили количество у существующих записей foreach (var updateComponent in goodComponents) { - updateComponent.Count = model.GoodComponents[updateComponent.ComponentId].Item2; - model.GoodComponents.Remove(updateComponent.ComponentId); + updateComponent.Count = model.GoodsComponents[updateComponent.ComponentId].Item2; + model.GoodsComponents.Remove(updateComponent.ComponentId); } context.SaveChanges(); } var good = context.Goods.First(x => x.Id == Id); - foreach (var gc in model.GoodComponents) + foreach (var gc in model.GoodsComponents) { context.GoodsComponents.Add(new GoodComponent { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Build.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Build.cs index d23d8f5..ea70d0b 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Build.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Build.cs @@ -4,6 +4,7 @@ using HardwareShopDatabaseImplement.Models.ManyToMany; using HardwareShopDataModels.Models; using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; namespace HardwareShopDatabaseImplement.Models.Worker { @@ -12,7 +13,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker public int Id { get; set; } [Required] - public decimal Price { get; set; } + public double Price { get; set; } [Required] public string BuildName { get; set; } = string.Empty; @@ -23,44 +24,37 @@ namespace HardwareShopDatabaseImplement.Models.Worker public virtual User User { get; set; } [ForeignKey("BuildId")] - public virtual List? Comments { get; set; } + public virtual List Comments { get; set; } = new(); [ForeignKey("BuildId")] public virtual List? Components { get; set; } [ForeignKey("BuildId")] - public virtual List? Purchases { get; set; } + public virtual List Purchases { get; set; } = new(); - - private Dictionary? _buildComponents = null; + public Dictionary? _buildPurchases = null; [NotMapped] - public Dictionary BuildComponents + public Dictionary BuildPurchases { get { - if (_buildComponents == null) + if (_buildPurchases == null) { - _buildComponents = Components.ToDictionary(recBC => recBC.ComponentId, recBC => (recBC.Component as IComponentModel, recBC.Count)); + _buildPurchases = Purchases.ToDictionary(recBP => recBP.PurchaseId, recBP => (recBP.Purchase as IPurchaseModel, recBP.Count)); } - return _buildComponents; + return _buildPurchases; } } - public static Build Create(HardwareShopDatabase context, BuildBindingModel model) + public static Build Create(BuildBindingModel model) { return new Build() { Id = model.Id, - Price = model.Price, BuildName = model.BuildName, UserId = model.UserId, - Components = model.BuildComponents.Select(x => new ComponentBuild - { - Component = context.Components.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList() }; } @@ -77,38 +71,52 @@ namespace HardwareShopDatabaseImplement.Models.Worker Price = Price, UserLogin = User.Login, UserId = UserId, - BuildComponents = BuildComponents + BuildPurchases = BuildPurchases, }; - public void UpdateComponents(HardwareShopDatabase context, BuildBindingModel model) + public void UpdatePurchases(HardwareShopDatabase context, BuildBindingModel model) { - var buildComponents = context.ComponentsBuilds.Where(rec => rec.BuildId == model.Id).ToList(); - if (buildComponents != null && buildComponents.Count > 0) + var buildPurchases = context.PurchasesBuilds.Where(rec => rec.BuildId == model.Id).ToList(); + if (buildPurchases != null && buildPurchases.Count > 0) { // удалили те в бд, которых нет в модели - context.ComponentsBuilds.RemoveRange(buildComponents.Where(rec => !model.BuildComponents.ContainsKey(rec.ComponentId))); + context.PurchasesBuilds.RemoveRange(buildPurchases.Where(rec => !model.BuildPurchases.ContainsKey(rec.PurchaseId))); context.SaveChanges(); // обновили количество у существующих записей - foreach (var updateComponent in buildComponents) + foreach (var updateComponent in buildPurchases) { - updateComponent.Count = model.BuildComponents[updateComponent.ComponentId].Item2; - model.BuildComponents.Remove(updateComponent.ComponentId); + updateComponent.Count = model.BuildPurchases[updateComponent.PurchaseId].Item2; + model.BuildPurchases.Remove(updateComponent.PurchaseId); } context.SaveChanges(); } var build = context.Builds.First(x => x.Id == Id); //добавляем в бд блюда которые есть в моделе, но ещё нет в бд - foreach (var dc in model.BuildComponents) + foreach (var bp in model.BuildPurchases) { - context.ComponentsBuilds.Add(new ComponentBuild + context.PurchasesBuilds.Add(new PurchaseBuild { Build = build, - Component = context.Components.First(x => x.Id == dc.Key), - Count = dc.Value.Item2 + Purchase = context.Purchases.First(x => x.Id == bp.Key), + Count = bp.Value.Item2 }); + var purchase = context.Purchases.First(x => x.Id == bp.Key); + if (purchase != null) + { + purchase.Sum += bp.Value.Item2* build.Price; + } context.SaveChanges(); + } - _buildComponents = null; + _buildPurchases = null; } + + public void UpdateSumPurchase(HardwareShopDatabase context, BuildBindingModel model) + { + var buildPurchases = context.PurchasesBuilds.Where(rec => rec.BuildId == model.Id).ToList(); + + } + + } } diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Purchase.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Purchase.cs index 4c573fc..35a163f 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Purchase.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Worker/Purchase.cs @@ -14,7 +14,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker public int Id { get; set; } [Required] - public decimal Sum { get; set; } + public double Sum { get; set; } [Required] public PurchaseStatus PurchaseStatus { get; set; } = PurchaseStatus.Неизвестен; @@ -29,27 +29,13 @@ namespace HardwareShopDatabaseImplement.Models.Worker [ForeignKey("PurchaseId")] public virtual List? Builds { get; set; } - public Dictionary? _purchaseBuilds = null; - - [NotMapped] - public Dictionary? PurchaseBuilds - { - get - { - if (_purchaseBuilds == null) - { - _purchaseBuilds = Builds.ToDictionary(recPB => recPB.BuildId, recPB => (recPB.Build as IBuildModel, recPB.Count)); - } - return _purchaseBuilds; - } - } - [ForeignKey("PurchaseId")] public virtual List Goods { get; set; } = new(); public Dictionary? _purchaseGoods = null; + [NotMapped] - public Dictionary? PurchaseGoods + public Dictionary PurchaseGoods { get { @@ -70,16 +56,11 @@ namespace HardwareShopDatabaseImplement.Models.Worker PurchaseStatus = model.PurchaseStatus, DatePurchase = model.DatePurchase, UserId = model.UserId, - Builds = model.PurchaseBuilds.Select(x => new PurchaseBuild - { - Build = context.Builds.First(y => y.Id == x.Key), - Count = x.Value.Item2 - }).ToList(), Goods = model.PurchaseGoods.Select(x => new PurchaseGood { Good = context.Goods.First(y => y.Id == x.Key), Count = x.Value.Item2 - }).ToList() + }).ToList(), }; } @@ -87,6 +68,7 @@ namespace HardwareShopDatabaseImplement.Models.Worker { PurchaseStatus = model.PurchaseStatus; DatePurchase = model.DatePurchase; + Sum = model.Sum; } public PurchaseViewModel GetViewModel => new() @@ -97,38 +79,9 @@ namespace HardwareShopDatabaseImplement.Models.Worker DatePurchase = DatePurchase, UserId = UserId, UserLogin = User.Login, - PurchaseBuilds = PurchaseBuilds, PurchaseGoods = PurchaseGoods }; - public void UpdateBuilds(HardwareShopDatabase context, PurchaseBindingModel model) - { - var purchaseBuilds = context.PurchasesBuilds.Where(rec => rec.PurchaseId == model.Id).ToList(); - if (purchaseBuilds != null && purchaseBuilds.Count > 0) - { - context.PurchasesBuilds.RemoveRange(purchaseBuilds.Where(rec => !model.PurchaseBuilds.ContainsKey(rec.BuildId))); - context.SaveChanges(); - foreach (var updateBuild in purchaseBuilds) - { - updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2; - model.PurchaseBuilds.Remove(updateBuild.BuildId); - } - context.SaveChanges(); - } - var purchase = context.Purchases.First(x => x.Id == Id); - foreach (var dc in model.PurchaseBuilds) - { - context.PurchasesBuilds.Add(new PurchaseBuild - { - Purchase = purchase, - Build = context.Builds.First(x => x.Id == dc.Key), - Count = dc.Value.Item2 - }); - context.SaveChanges(); - } - _purchaseBuilds = null; - } - public void UpdateGoods(HardwareShopDatabase context, PurchaseBindingModel model) { var purchaseGoods = context.PurchasesGoods.Where(rec => rec.PurchaseId == model.Id).ToList();