diff --git a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs index 530c7a3..cbf540d 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/ComputerShopDatabase.cs @@ -24,5 +24,7 @@ namespace ComputersShopDatabaseImplement public virtual DbSet Computers { set; get; } public virtual DbSet ComputerComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet Shops { set; get; } + public virtual DbSet ShopComputers { set; get; } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs index efafb2a..d8afdd4 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComponentStorage.cs @@ -16,17 +16,22 @@ namespace ComputersShopDatabaseImplement.Implements public List GetFullList() { using var context = new ComputersShopDatabase(); - return context.Components.Select(x => x.GetViewModel).ToList(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); } - public List GetFiltredList(ComponentSearchModel model) + public List GetFilteredList(ComponentSearchModel model) { if (string.IsNullOrEmpty(model.ComponentName)) { return new(); } + using var context = new ComputersShopDatabase(); - return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)). - Select(x => x.GetViewModel).ToList(); + return context.Components + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); } public ComponentViewModel? GetElement(ComponentSearchModel model) { @@ -34,9 +39,13 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); - return context.Components.FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) - && x.ComponentName == model.ComponentName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Components + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && + x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public ComponentViewModel? Insert(ComponentBindingModel model) { @@ -45,6 +54,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); context.Components.Add(newComponent); context.SaveChanges(); @@ -58,6 +68,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + component.Update(model); context.SaveChanges(); return component.GetViewModel; @@ -66,13 +77,14 @@ namespace ComputersShopDatabaseImplement.Implements { using var context = new ComputersShopDatabase(); var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); - if (element != null) + if (element == null) { - context.Components.Remove(element); - context.SaveChanges(); - return element.GetViewModel; + return null; } - return null; + + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; } } } diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs index 16e79af..0d56b26 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ComputerStorage.cs @@ -9,6 +9,7 @@ using ComputersShopContracts.BindingModels; using ComputersShopContracts.ViewModels; using ComputersShopDatabaseImplement.Models; using Microsoft.EntityFrameworkCore; +using System.Numerics; namespace ComputersShopDatabaseImplement.Implements { @@ -17,19 +18,28 @@ namespace ComputersShopDatabaseImplement.Implements public List GetFullList() { using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .ToList().Select(x => x.GetViewModel).ToList(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } - public List GetFiltredList(ComputerSearchModel model) + public List GetFilteredList(ComputerSearchModel model) { if (string.IsNullOrEmpty(model.ComputerName)) { return new(); } + using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .Where(x => x.ComputerName.Contains(model.ComputerName)).ToList() - .Select(x => x.GetViewModel).ToList(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ComputerName.Contains(model.ComputerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); } public ComputerViewModel? GetElement(ComputerSearchModel model) { @@ -37,11 +47,15 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); - return context.Computers.Include(x => x.Components).ThenInclude(x => x.Component) - .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && - x.ComputerName == model.ComputerName) || - (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && + x.ComputerName == model.ComputerName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; } public ComputerViewModel? Insert(ComputerBindingModel model) { @@ -51,6 +65,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + context.Computers.Add(newComputer); context.SaveChanges(); return newComputer.GetViewModel; @@ -66,6 +81,7 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + computer.Update(model); context.SaveChanges(); computer.UpdateComponents(context, model); @@ -81,7 +97,9 @@ namespace ComputersShopDatabaseImplement.Implements public ComputerViewModel? Delete(ComputerBindingModel model) { using var context = new ComputersShopDatabase(); - var element = context.Computers.Include(x => x.Components).FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Computers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); if (element != null) { context.Computers.Remove(element); @@ -92,3 +110,4 @@ namespace ComputersShopDatabaseImplement.Implements } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs index 998f763..542447f 100644 --- a/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/OrderStorage.cs @@ -14,29 +14,36 @@ namespace ComputersShopDatabaseImplement.Implements { public class OrderStorage : IOrderStorage { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Orders + .Select(x => GetViewModel(x)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new ComputersShopDatabase(); + return context.Orders + .Where(x => x.Id.Equals(model.Id)) + .Select(x => GetViewModel(x)) + .ToList(); + } public OrderViewModel? GetElement(OrderSearchModel model) { if (!model.Id.HasValue) { return null; } + using var context = new ComputersShopDatabase(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; - } - public List GetFiltredList(OrderSearchModel model) - { - if (!model.Id.HasValue) - { - return new(); - } - using var context = new ComputersShopDatabase(); - return context.Orders.Where(x => x.Id == model.Id).Include(x => x.Computer) - .Select(x => x.GetViewModel).ToList(); - } - public List GetFullList() - { - using var context = new ComputersShopDatabase(); - return context.Orders.Include(x => x.Computer).Select(x => x.GetViewModel).ToList(); + return GetViewModel(context.Orders + .FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); } public OrderViewModel? Insert(OrderBindingModel model) { @@ -45,10 +52,11 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + using var context = new ComputersShopDatabase(); context.Orders.Add(newOrder); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == newOrder.Id)?.GetViewModel; + return GetViewModel(newOrder); } public OrderViewModel? Update(OrderBindingModel model) { @@ -58,21 +66,34 @@ namespace ComputersShopDatabaseImplement.Implements { return null; } + order.Update(model); context.SaveChanges(); - return context.Orders.Include(x => x.Computer).FirstOrDefault(x => x.Id == model.Id)?.GetViewModel; + return GetViewModel(order); } public OrderViewModel? Delete(OrderBindingModel model) { using var context = new ComputersShopDatabase(); - var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + var element = context.Orders.FirstOrDefault(x => x.Id == model.Id); if (element != null) { context.Orders.Remove(element); context.SaveChanges(); - return element.GetViewModel; + return GetViewModel(element); } return null; } + private static OrderViewModel GetViewModel(Order order) + { + using var context = new ComputersShopDatabase(); + var viewModel = order.GetViewModel; + var computer = context.Computers.FirstOrDefault(x => x.Id == order.ComputerId); + if (computer != null) + { + viewModel.ComputerName = computer.ComputerName; + } + return viewModel; + } } } + diff --git a/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..bf472ff --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,168 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public List GetFullList() + { + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + + using var context = new ComputersShopDatabase(); + return context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && + x.ShopName == model.ShopName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var newShop = Shop.Create(context, model); + if (newShop == null) + { + return null; + } + + context.Shops.Add(newShop); + context.SaveChanges(); + return newShop.GetViewModel; + } + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => rec.Id == model.Id); + if (shop == null) + { + return null; + } + + shop.Update(model); + context.SaveChanges(); + shop.UpdateComputers(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ComputersShopDatabase(); + var element = context.Shops + .Include(x => x.Computers) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + public bool SellComputers(IComputerModel model, int count) + { + using var context = new ComputersShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shops = context.Shops + .Include(x => x.Computers) + .ThenInclude(x => x.Computer) + .ToList() + .Where(x => x.ShopComputers.ContainsKey(model.Id)); + + foreach (var shop in shops) + { + int countInCurrentShop = shop.ShopComputers[model.Id].Item2; + if (countInCurrentShop <= count) + { + var elem = context.ShopComputers + .Where(x => x.ComputerId == model.Id) + .FirstOrDefault(x => x.ShopId == shop.Id); + context.ShopComputers.Remove(elem); + shop.ShopComputers.Remove(model.Id); + count -= countInCurrentShop; + } + else + { + shop.ShopComputers[model.Id] = (shop.ShopComputers[model.Id].Item1, countInCurrentShop - count); + count = 0; + shop.UpdateComputers(context, new ShopBindingModel + { + Id = shop.Id, + ShopName = shop.ShopName, + Address = shop.Address, + DateOpening = shop.DateOpening, + ShopComputers = shop.ShopComputers, + MaxComputers = shop.MaxComputers + }); + } + if (count <= 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + public bool CheckCount(IComputerModel model, int count) + { + throw new NotImplementedException(); + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..8719b35 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/Shop.cs @@ -0,0 +1,104 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopDataModels.Models; + +namespace ComputersShopDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string ShopName { get; set; } = string.Empty; + [Required] + public string Address { get; set; } = string.Empty; + [Required] + public DateTime DateOpening { get; set; } + private Dictionary? _shopComputers = null; + + [NotMapped] + public Dictionary ShopComputers + { + get + { + if (_shopComputers == null) + { + _shopComputers = Computers + .ToDictionary(recSP => recSP.ComputerId, recSP => (recSP.Computer as IComputerModel, recSP.Count)); + } + return _shopComputers; + } + } + public int MaxComputers { get; set; } + [ForeignKey("ShopId")] + public List Computers { get; set; } = new(); + public static Shop Create(ComputersShopDatabase context, ShopBindingModel model) + { + return new Shop + { + Id = model.Id, + ShopName = model.ShopName, + Address = model.Address, + DateOpening = model.DateOpening, + MaxComputers = model.MaxComputers, + Computers = model.ShopComputers.Select(x => new ShopComputer + { + Computer = context.Computers.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + Address = model.Address; + DateOpening = model.DateOpening; + MaxComputers = model.MaxComputers; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + DateOpening = DateOpening, + ShopComputers = ShopComputers, + MaxComputers = MaxComputers + }; + public void UpdateComputers(ComputersShopDatabase context, ShopBindingModel model) + { + var shopComputers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList(); + if (shopComputers != null && shopComputers.Count > 0) + { + // Удаление изделий, которых нет в магазине + context.ShopComputers.RemoveRange(shopComputers.Where(rec => !model.ShopComputers.ContainsKey(rec.ComputerId))); + context.SaveChanges(); + // Обновление количества у существующих записей + foreach (var updateComputers in shopComputers) + { + updateComputers.Count = model.ShopComputers[updateComputers.ComputerId].Item2; + model.ShopComputers.Remove(updateComputers.ComputerId); + } + context.SaveChanges(); + } + + var shop = context.Shops.First(x => x.Id == Id); + foreach (var sp in model.ShopComputers) + { + context.ShopComputers.Add(new ShopComputer + { + Shop = shop, + Computer = context.Computers.First(x => x.Id == sp.Key), + Count = sp.Value.Item2 + }); + context.SaveChanges(); + } + _shopComputers = null; + } + } +} diff --git a/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs new file mode 100644 index 0000000..c616c30 --- /dev/null +++ b/ComputersShop/ComputersShopDatabaseImplement/Models/ShopComputer.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Numerics; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDatabaseImplement.Models +{ + public class ShopComputer + { + public int Id { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int ComputerId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Computer Computer { get; set; } = new(); + } +} + diff --git a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs index 9be75ba..ca02429 100644 --- a/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs +++ b/ComputersShop/ComputersShopFileImplement/Implements/OrderStorage.cs @@ -86,10 +86,10 @@ namespace ComputersShopFileImplement.Implements private OrderViewModel GetViewModel(Order order) { var viewModel = order.GetViewModel; - var plane = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId); - if (plane != null) + var computer = _source.Computers.FirstOrDefault(x => x.Id == order.ComputerId); + if (computer != null) { - viewModel.ComputerName = plane.ComputerName; + viewModel.ComputerName = computer.ComputerName; } return viewModel; } diff --git a/ComputersShop/ComputersShopView/FormSell.cs b/ComputersShop/ComputersShopView/FormSell.cs index 5de3f68..954458a 100644 --- a/ComputersShop/ComputersShopView/FormSell.cs +++ b/ComputersShop/ComputersShopView/FormSell.cs @@ -15,21 +15,13 @@ namespace ComputersShopView public partial class FormSell : Form { private readonly ILogger _logger; - - /// - /// Бизнес-логика для изделий - /// private readonly IComputerLogic _logicC; - - /// - /// Бизнес-логика для магазинов - /// private readonly IShopLogic _logicS; - public FormSell(ILogger logger, IComputerLogic planeLogic, IShopLogic shopLogic) + public FormSell(ILogger logger, IComputerLogic computerLogic, IShopLogic shopLogic) { InitializeComponent(); _logger = logger; - _logicC = planeLogic; + _logicC = computerLogic; _logicS = shopLogic; } private void FormSell_Load(object sender, EventArgs e) @@ -52,12 +44,6 @@ namespace ComputersShopView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - /// - /// Кнопка "Сохранить" - /// - /// - /// private void buttonSave_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(textBoxCount.Text)) @@ -92,12 +78,6 @@ namespace ComputersShopView MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } } - - /// - /// Кнопка "Отмена" - /// - /// - /// private void buttonCancel_Click(object sender, EventArgs e) { DialogResult = DialogResult.Cancel;