From 0a6b99d4d0531240e7a2c5cc070c4f4d358afa83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9D=D0=B8=D0=BA=D0=BE=D0=BB=D0=B0=D0=B9?= Date: Sat, 1 Apr 2023 18:09:16 +0400 Subject: [PATCH] =?UTF-8?q?=D0=B3=D0=BE=D1=82=D0=BE=D0=B2=D1=8B=20=D1=85?= =?UTF-8?q?=D1=80=D0=B0=D0=BD=D0=B8=D0=BB=D0=B8=D1=89=D0=B0=20=D0=B4=D0=BB?= =?UTF-8?q?=D1=8F=20=D0=A0=D0=B0=D0=B1=D0=BE=D1=82=D0=BD=D0=B8=D0=BA=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HardwareShopBusinessLogic.csproj | 5 + .../SearchModels/BuildSearchModel.cs | 2 + .../SearchModels/CommentSearchModel.cs | 2 + .../SearchModels/PurchaseSearchModel.cs | 2 + .../SearchModels/UserSearchModel.cs | 2 + .../StoragesContracts/IBuildStorage.cs | 2 +- .../Implements/Worker/BuildStorage.cs | 4 +- .../Implements/Worker/CommentStorage.cs | 65 ++++++++++-- .../Implements/Worker/PurchaseStorage.cs | 98 +++++++++++++++++-- 9 files changed, 167 insertions(+), 15 deletions(-) diff --git a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj index 132c02c..957eaa4 100644 --- a/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj +++ b/HardwareShop/HardwareShopBusinessLogic/HardwareShopBusinessLogic.csproj @@ -6,4 +6,9 @@ enable + + + + + diff --git a/HardwareShop/HardwareShopContracts/SearchModels/BuildSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/BuildSearchModel.cs index be06933..3d5caee 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/BuildSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/BuildSearchModel.cs @@ -6,5 +6,7 @@ namespace HardwareShopContracts.SearchModels public int? Id { get; set; } public string? BuildName { get; set; } = string.Empty; + + public int? UserId { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs index 78c5893..78b05be 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/CommentSearchModel.cs @@ -6,5 +6,7 @@ namespace HardwareShopContracts.SearchModels public class CommentSearchModel { public int? Id { get; set; } + + public int? BuildId { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/SearchModels/PurchaseSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/PurchaseSearchModel.cs index 097858e..1a62d47 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/PurchaseSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/PurchaseSearchModel.cs @@ -5,5 +5,7 @@ namespace HardwareShopContracts.SearchModels public class PurchaseSearchModel { public int? Id { get; set; } + + public int? UserId { get; set; } } } diff --git a/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs index 8733ba5..be190e7 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs @@ -10,5 +10,7 @@ namespace HardwareShopContracts.SearchModels public string? Login { get; set; } = string.Empty; public string? Email { get; set; } = string.Empty; + + public int? UserId { get; set; } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopContracts/StoragesContracts/IBuildStorage.cs b/HardwareShop/HardwareShopContracts/StoragesContracts/IBuildStorage.cs index bca831f..8771e49 100644 --- a/HardwareShop/HardwareShopContracts/StoragesContracts/IBuildStorage.cs +++ b/HardwareShop/HardwareShopContracts/StoragesContracts/IBuildStorage.cs @@ -7,7 +7,7 @@ namespace HardwareShopContracts.StoragesContracts public interface IBuildStorage { List GetFullList(); - List GetFilteredList(UserSearchModel model); + List GetFilteredList(BuildSearchModel model); BuildViewModel? GetElement(BuildSearchModel model); BuildViewModel? Insert(BuildBindingModel model); BuildViewModel? Update(BuildBindingModel model); diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs index cc8e3a1..10e0f11 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/BuildStorage.cs @@ -22,7 +22,7 @@ namespace HardwareShopContracts.StoragesContracts .ToList(); } - public List GetFilteredList(UserSearchModel model) + public List GetFilteredList(BuildSearchModel model) { if (model.Id.HasValue) { @@ -34,7 +34,7 @@ namespace HardwareShopContracts.StoragesContracts .ThenInclude(x => x.Component) .Include(x => x.Comments) .Include(x => x.User) - .Where(x => x.UserId == model.Id) + .Where(x => x.UserId == model.UserId) .ToList() .Select(x => x.GetViewModel) .ToList(); diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs index 50b307e..2f92b6f 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/CommentStorage.cs @@ -1,6 +1,9 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement; +using HardwareShopDatabaseImplement.Models.Worker; +using Microsoft.EntityFrameworkCore; namespace HardwareShopContracts.StoragesContracts { @@ -8,32 +11,82 @@ namespace HardwareShopContracts.StoragesContracts { public List GetFullList() { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + return context.Comments + .Include(x => x.Build) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(CommentSearchModel model) { - throw new NotImplementedException(); + if (model.BuildId.HasValue) + { + return new(); + } + using var context = new HardwareShopDatabase(); + return context.Comments + .Include(x => x.Build) + .Where(x => x.BuildId == model.BuildId) + .Select(x => x.GetViewModel) + .ToList(); } public CommentViewModel? GetElement(CommentSearchModel model) { - throw new NotImplementedException(); + if (model.Id.HasValue) + { + return null; + } + using var context = new HardwareShopDatabase(); + return context.Comments + .Include(x => x.Build) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public CommentViewModel? Insert(CommentBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + var newComment = Comment.Create(model); + if (newComment == null) + { + return null; + } + context.Comments.Add(newComment); + context.SaveChanges(); + return newComment.GetViewModel; } public CommentViewModel? Update(CommentBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + var comment = context.Comments.FirstOrDefault(x => x.Id == model.Id); + if (comment == null) + { + return null; + } + comment.Update(model); + context.SaveChanges(); + return context.Comments + .Include(x => x.Build) + .FirstOrDefault(x => x.Id == comment.Id) + ?.GetViewModel; } public CommentViewModel? Delete(CommentBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + var element = context.Comments + .Include(x => x.Build) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Comments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs index bf40285..562c24e 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Worker/PurchaseStorage.cs @@ -1,6 +1,10 @@ using HardwareShopContracts.BindingModels; using HardwareShopContracts.SearchModels; using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement; +using HardwareShopDatabaseImplement.Models.Storekeeper; +using HardwareShopDatabaseImplement.Models.Worker; +using Microsoft.EntityFrameworkCore; namespace HardwareShopContracts.StoragesContracts { @@ -8,32 +12,114 @@ namespace HardwareShopContracts.StoragesContracts { public List GetFullList() { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + return context.Purchases + .Include(x => x.Builds) + .Include(x => x.Goods) + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); } public List GetFilteredList(PurchaseSearchModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + 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 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(); } public PurchaseViewModel? GetElement(PurchaseSearchModel model) { - throw new NotImplementedException(); + if (!model.Id.HasValue) + { + return null; + } + using var context = new HardwareShopDatabase(); + return context.Purchases + .Include(x => x.Builds) + .Include(x => x.Goods) + .Include(x => x.User) + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel; } public PurchaseViewModel? Insert(PurchaseBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + var newPurchase = Purchase.Create(context, model); + if (newPurchase == null) + { + return null; + } + context.Purchases.Add(newPurchase); + context.SaveChanges(); + return context.Purchases + .Include(x => x.Builds) + .Include(x => x.Goods) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == newPurchase.Id) + ?.GetViewModel; } public PurchaseViewModel? Update(PurchaseBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var purchase = context.Purchases + .Include(x => x.Builds) + .Include(x => x.Goods) + .Include(x => x.User) + .FirstOrDefault(x => x.Id == model.Id); + if (purchase == null) + { + return null; + } + purchase.Update(model); + context.SaveChanges(); + purchase.UpdateGoods(context, model); + purchase.UpdateBuilds(context, model); + transaction.Commit(); + return purchase.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } } public PurchaseViewModel? Delete(PurchaseBindingModel model) { - throw new NotImplementedException(); + using var context = new HardwareShopDatabase(); + var element = context.Purchases + .Include(x => x.Builds) + .Include(x => x.Goods) + .Include(x => x.User) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Purchases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; } } }