diff --git a/JewelryStore/JewelryStore/FormComponent.Designer.cs b/JewelryStore/JewelryStore/FormComponent.Designer.cs index 182fb3a..953f3df 100644 --- a/JewelryStore/JewelryStore/FormComponent.Designer.cs +++ b/JewelryStore/JewelryStore/FormComponent.Designer.cs @@ -101,6 +101,7 @@ this.Controls.Add(this.labelName); this.Name = "FormComponent"; this.Text = "Компонент"; + this.Load += new System.EventHandler(this.FormComponent_Load); this.ResumeLayout(false); this.PerformLayout(); diff --git a/JewelryStore/JewelryStore/FormComponents.Designer.cs b/JewelryStore/JewelryStore/FormComponents.Designer.cs index e35263a..dfa26c5 100644 --- a/JewelryStore/JewelryStore/FormComponents.Designer.cs +++ b/JewelryStore/JewelryStore/FormComponents.Designer.cs @@ -106,6 +106,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormComponents"; this.Text = "Компоненты"; + this.Load += new System.EventHandler(this.FormComponents_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/JewelryStore/JewelryStore/FormJewel.Designer.cs b/JewelryStore/JewelryStore/FormJewel.Designer.cs index 78d04a6..54fa842 100644 --- a/JewelryStore/JewelryStore/FormJewel.Designer.cs +++ b/JewelryStore/JewelryStore/FormJewel.Designer.cs @@ -207,6 +207,7 @@ this.Controls.Add(this.groupBox); this.Name = "FormJewel"; this.Text = "Изделие"; + this.Load += new System.EventHandler(this.FormJewel_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.groupBox.ResumeLayout(false); this.ResumeLayout(false); diff --git a/JewelryStore/JewelryStore/FormJewels.Designer.cs b/JewelryStore/JewelryStore/FormJewels.Designer.cs index 3d7d620..911c3cb 100644 --- a/JewelryStore/JewelryStore/FormJewels.Designer.cs +++ b/JewelryStore/JewelryStore/FormJewels.Designer.cs @@ -100,6 +100,7 @@ this.Controls.Add(this.dataGridView); this.Name = "FormJewels"; this.Text = "Изделия"; + this.Load += new System.EventHandler(this.FormJewels_Load); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); this.ResumeLayout(false); diff --git a/JewelryStore/JewelryStore/FormMain.Designer.cs b/JewelryStore/JewelryStore/FormMain.Designer.cs index 631bb24..5deb703 100644 --- a/JewelryStore/JewelryStore/FormMain.Designer.cs +++ b/JewelryStore/JewelryStore/FormMain.Designer.cs @@ -156,6 +156,7 @@ this.MainMenuStrip = this.menuStrip; this.Name = "FormMain"; this.Text = "Ювелирная лавка"; + this.Load += new System.EventHandler(this.FormMain_Load); this.menuStrip.ResumeLayout(false); this.menuStrip.PerformLayout(); ((System.ComponentModel.ISupportInitialize)(this.dataGridView)).EndInit(); diff --git a/JewelryStore/JewelryStore/Program.cs b/JewelryStore/JewelryStore/Program.cs index 26ae8eb..8d5a0fc 100644 --- a/JewelryStore/JewelryStore/Program.cs +++ b/JewelryStore/JewelryStore/Program.cs @@ -1,6 +1,6 @@ using JewelryStoreContracts.BusinessLogicsContracts; using JewelryStoreContracts.StoragesContracts; -using JewelryStoreFileImplement.Implements; +using JewelryStoreDatabaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging; diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs b/JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..a77bfb3 --- /dev/null +++ b/JewelryStore/JewelryStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,80 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Components.Select(x => x.GetViewModel).ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Components.Where(x => x.ComponentName.Contains(model.ComponentName)).Select(x => x.GetViewModel).ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new JewelryStoreDatabase(); + 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) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new JewelryStoreDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var component = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Components.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs b/JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs new file mode 100644 index 0000000..edfbd8a --- /dev/null +++ b/JewelryStore/JewelryStoreDatabaseImplement/Implements/JewelStorage.cs @@ -0,0 +1,94 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class JewelStorage : IJewelStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).ToList().Select(x => x.GetViewModel).ToList(); + } + public List GetFilteredList(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName)) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component) + .Where(x => x.JewelName.Contains(model.JewelName)).ToList().Select(x => x.GetViewModel).ToList(); + } + public JewelViewModel? GetElement(JewelSearchModel model) + { + if (string.IsNullOrEmpty(model.JewelName) && !model.Id.HasValue) + { + return null; + } + using var context = new JewelryStoreDatabase(); + return context.Jewels.Include(x => x.Components).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.JewelName) && + x.JewelName == model.JewelName) ||(model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + public JewelViewModel? Insert(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var newProduct = Jewel.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Jewels.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public JewelViewModel? Update(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Jewels.FirstOrDefault(rec => + rec.Id == model.Id); + if (product == null) + { + return null; + } + product.Update(model); + context.SaveChanges(); + product.UpdateComponents(context, model); + transaction.Commit(); + return product.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + public JewelViewModel? Delete(JewelBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Jewels + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Jewels.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs b/JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..0a159f4 --- /dev/null +++ b/JewelryStore/JewelryStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,90 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDatabaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new JewelryStoreDatabase(); + return context.Orders + .Select(x => AccessJewelStorage(x.GetViewModel, context)) + .ToList(); + } + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return context.Orders + .Where(x => x.Id == model.Id) + .Select(x => AccessJewelStorage(x.GetViewModel, context)) + .ToList(); + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + using var context = new JewelryStoreDatabase(); + return AccessJewelStorage(context.Orders + .FirstOrDefault(x => model.Id.HasValue && x.Id == model.Id) + ?.GetViewModel, context); + } + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + using var context = new JewelryStoreDatabase(); + context.Orders.Add(newOrder); + context.SaveChanges(); + return AccessJewelStorage(newOrder.GetViewModel, context); + } + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + return AccessJewelStorage(order.GetViewModel, context); + } + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new JewelryStoreDatabase(); + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return AccessJewelStorage(element.GetViewModel, context); + } + return null; + } + static OrderViewModel AccessJewelStorage(OrderViewModel model, JewelryStoreDatabase context) + { + if (model == null) return model; + string? jewelName = context.Jewels.FirstOrDefault(x => x.Id == model.JewelId)?.JewelName; + if (jewelName != null) model.JewelName = jewelName; + return model; + } + } +} diff --git a/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs b/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs index f99818b..fb91643 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/JewelryStoreDatabase.cs @@ -15,7 +15,7 @@ namespace JewelryStoreDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=JewelryStoreDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } diff --git a/JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs b/JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs index 20f0a09..af1be1e 100644 --- a/JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs +++ b/JewelryStore/JewelryStoreDatabaseImplement/Models/Order.cs @@ -5,6 +5,7 @@ using JewelryStoreDataModels.Models; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; +using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; @@ -15,19 +16,17 @@ namespace JewelryStoreDatabaseImplement.Models { public int Id { get; private set; } [Required] - public int JewelId { get; private set; } + public int JewelId { get; set; } [Required] - public int Count { get; private set; } + public int Count { get; set; } [Required] - public double Sum { get; private set; } + public double Sum { get; set; } [Required] - public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + public OrderStatus Status { get; set; } [Required] - public DateTime DateCreate { get; private set; } = DateTime.Now; - - public DateTime? DateImplement { get; private set; } - - public virtual Jewel Jewel { get; set; } + public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } + public Jewel Jewel { get; set; } public static Order? Create(OrderBindingModel? model) { @@ -46,36 +45,19 @@ namespace JewelryStoreDatabaseImplement.Models DateImplement = model.DateImplement }; } - - public static Order Create(OrderViewModel model) - { - return new Order - { - Id = model.Id, - JewelId = model.JewelId, - Count = model.Count, - Sum = model.Sum, - Status = model.Status, - DateCreate = model.DateCreate, - DateImplement = model.DateImplement - }; - } - - public void Update(OrderBindingModel model) + public void Update(OrderBindingModel? model) { if (model == null) { return; } - Status = model.Status; - DateImplement = model.DateImplement; + if (model.DateImplement.HasValue) DateImplement = model.DateImplement; } public OrderViewModel GetViewModel => new() { Id = Id, JewelId = JewelId, - JewelName = Jewel.JewelName, Count = Count, Sum = Sum, Status = Status,