From a4e790d380bdfe6b1926e72fb450a7cc5de86984 Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Tue, 23 Apr 2024 22:34:09 +0400 Subject: [PATCH 1/3] Add ImplementorSoft --- .../BindingModels/ProductBindingModel.cs | 2 +- .../BindingModels/ProductionBindingModel.cs | 2 +- .../Contracts/ViewModels/ProductViewModel.cs | 1 + .../ViewModels/ProductionViewModel.cs | 1 + Course/DatabaseImplement/Models/Detail.cs | 2 + .../Models/DetailProduction.cs | 13 ++- Course/DatabaseImplement/Models/Product.cs | 35 ++++++- Course/DatabaseImplement/Models/Production.cs | 93 +++++++++++++++++-- 8 files changed, 137 insertions(+), 12 deletions(-) diff --git a/Course/Contracts/BindingModels/ProductBindingModel.cs b/Course/Contracts/BindingModels/ProductBindingModel.cs index 763719d..57e6974 100644 --- a/Course/Contracts/BindingModels/ProductBindingModel.cs +++ b/Course/Contracts/BindingModels/ProductBindingModel.cs @@ -8,6 +8,6 @@ namespace Contracts.BindingModels public int UserId { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public Dictionary DetailProducts { get; set; } = new(); + public Dictionary ProductDetails { get; set; } = new(); } } diff --git a/Course/Contracts/BindingModels/ProductionBindingModel.cs b/Course/Contracts/BindingModels/ProductionBindingModel.cs index 5e76313..8fee8ac 100644 --- a/Course/Contracts/BindingModels/ProductionBindingModel.cs +++ b/Course/Contracts/BindingModels/ProductionBindingModel.cs @@ -8,7 +8,7 @@ namespace Contracts.BindingModels public int UserId { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public Dictionary ProductionDetails { get; set; } = new(); + public Dictionary ProductionDetails { get; set; } = new(); } } diff --git a/Course/Contracts/ViewModels/ProductViewModel.cs b/Course/Contracts/ViewModels/ProductViewModel.cs index 186c4e1..1ea4eb4 100644 --- a/Course/Contracts/ViewModels/ProductViewModel.cs +++ b/Course/Contracts/ViewModels/ProductViewModel.cs @@ -16,5 +16,6 @@ namespace Contracts.ViewModels [DisplayName("Цена изделия")] public double Cost { get; set; } public int UserId { get; set; } + public Dictionary DetailProducts { get; set; } = new(); } } diff --git a/Course/Contracts/ViewModels/ProductionViewModel.cs b/Course/Contracts/ViewModels/ProductionViewModel.cs index 7302932..f580fda 100644 --- a/Course/Contracts/ViewModels/ProductionViewModel.cs +++ b/Course/Contracts/ViewModels/ProductionViewModel.cs @@ -16,5 +16,6 @@ namespace Contracts.ViewModels [DisplayName("Цена производства")] public double Cost { get; set; } public int UserId { get; set; } + public Dictionary DetailProductions { get; set; } = new(); } } diff --git a/Course/DatabaseImplement/Models/Detail.cs b/Course/DatabaseImplement/Models/Detail.cs index f62d1ce..d80048d 100644 --- a/Course/DatabaseImplement/Models/Detail.cs +++ b/Course/DatabaseImplement/Models/Detail.cs @@ -17,6 +17,8 @@ namespace DatabaseImplement.Models public int UserId { get; set; } [ForeignKey("DetailId")] public virtual List DetailProducts { get; set; } = new(); + [ForeignKey("DetailId")] + public virtual List DetailProductions { get; set; } = new(); public static Detail? Create(DetailBindingModel model) { if (model == null) diff --git a/Course/DatabaseImplement/Models/DetailProduction.cs b/Course/DatabaseImplement/Models/DetailProduction.cs index ccf5ca7..1c1a260 100644 --- a/Course/DatabaseImplement/Models/DetailProduction.cs +++ b/Course/DatabaseImplement/Models/DetailProduction.cs @@ -1,12 +1,23 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DatabaseImplement.Models { - internal class DetailProduction + public class DetailProduction { + public int Id { get; set; } + [Required] + public int DetailId { get; set; } + [Required] + public int ProductionId { get; set; } + [Required] + public int Count { get; set; } + + public virtual Detail Detail { get; set; } = new(); + public virtual Production Production { get; set; } = new(); } } diff --git a/Course/DatabaseImplement/Models/Product.cs b/Course/DatabaseImplement/Models/Product.cs index b662c02..b79c0a5 100644 --- a/Course/DatabaseImplement/Models/Product.cs +++ b/Course/DatabaseImplement/Models/Product.cs @@ -31,7 +31,7 @@ namespace DatabaseImplement.Models [ForeignKey("ProductId")] public virtual List Details { get; set; } = new(); - public static Product? Create(ProductBindingModel model) + public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel model) { if (model == null) { @@ -45,7 +45,8 @@ namespace DatabaseImplement.Models UserId = model.UserId, Details = model.DetailProducts.Select(x => new DetailProduct { - //Detail = + Detail = context.Details.First(y => y.Id == x.Key), + Count = x.Value.Item2 }).ToList() }; } @@ -71,7 +72,35 @@ namespace DatabaseImplement.Models Id = Id, Name = Name, Cost = Cost, - UserId = UserId + UserId = UserId, + DetailProducts = DetailProducts }; + public void UpdateDetails(FactoryGoWorkDatabase context, ProductBindingModel model) + { + var productDetails = context.DetailProducts.Where(rec => rec.ProductId == model.Id).ToList(); + if (productDetails != null && productDetails.Count > 0) + { + context.DetailProducts.RemoveRange(productDetails.Where(rec => !model.ProductDetails.ContainsKey(rec.DetailId))); + context.SaveChanges(); + foreach (var upDetail in productDetails) + { + upDetail.Count = model.ProductDetails[upDetail.DetailId].Item2; + model.ProductDetails.Remove(upDetail.DetailId); + } + context.SaveChanges(); + } + var product = context.Products.First(x => x.Id == model.Id); + foreach (var dp in model.ProductDetails) + { + context.DetailProducts.Add(new DetailProduct + { + Product = product, + Detail = context.Details.First(x => x.Id == dp.Key), + Count = dp.Value.Item2 + }); + context.SaveChanges(); + } + _detailProducts = null; + } } } diff --git a/Course/DatabaseImplement/Models/Production.cs b/Course/DatabaseImplement/Models/Production.cs index 547fe59..289fac8 100644 --- a/Course/DatabaseImplement/Models/Production.cs +++ b/Course/DatabaseImplement/Models/Production.cs @@ -1,12 +1,93 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using DataModels; +using DataModels.Models; +using Contracts.BindingModels; +using Contracts.ViewModels; +using Contracts.SearchModels; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace DatabaseImplement.Models { - internal class Production + public class Production : IProductionModel { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public int UserId { get; set; } + private Dictionary? _detailProductions = null; + [NotMapped] + public Dictionary? DetailProductions + { + get + { + if (_detailProductions == null) + { + _detailProductions = Details.ToDictionary(recDP => recDP.DetailId, recDp => (recDp.Detail as IDetailModel, recDp.Count)); + } + return _detailProductions; + } + } + [ForeignKey("ProductionId")] + public List Details { get; set; } + + [ForeignKey("UserId")] + public virtual Implementer User { get; set; } + public static Production Create(FactoryGoWorkDatabase context, ProductionBindingModel model) + { + return new Production() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + UserId = model.UserId, + Details = model.ProductionDetails.Select(x => new DetailProduction + { + Detail = context.Details.First(y => y.Id == x.Key), + }).ToList(), + }; + } + public void Update(ProductionBindingModel model) + { + Name = model.Name; + Cost = model.Cost; + } + public ProductionViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost, + UserId = UserId, + DetailProductions = DetailProductions + }; + public void UpdateDetails(FactoryGoWorkDatabase context, ProductionBindingModel model) + { + var productionDetails = context.DetailProductions.Where(rec => rec.ProductionId == model.Id).ToList(); + if (productionDetails != null && productionDetails.Count > 0) + { + context.DetailProductions.RemoveRange(productionDetails.Where(rec => !model.ProductionDetails.ContainsKey(rec.DetailId))); + context.SaveChanges(); + foreach(var upDetail in productionDetails) + { + upDetail.Count = model.ProductionDetails[upDetail.DetailId].Item2; + model.ProductionDetails.Remove(upDetail.DetailId); + } + context.SaveChanges(); + } + var production = context.Productions.First(x => x.Id == model.Id); + foreach (var dp in model.ProductionDetails) + { + context.DetailProductions.Add(new DetailProduction + { + Production = production, + Detail = context.Details.First(x => x.Id == dp.Key), + Count = dp.Value.Item2 + }); + context.SaveChanges(); + } + _detailProductions = null; + } } } From 0c1801324549f047b5f8712cfde2b8bfeacbac1c Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Wed, 24 Apr 2024 12:35:17 +0400 Subject: [PATCH 2/3] DBUpdate --- Course/DatabaseImplement/Models/Detail.cs | 1 + .../DatabaseImplement/Models/Implementer.cs | 71 +++++++++++++++++-- Course/DatabaseImplement/Models/Product.cs | 3 +- Course/DatabaseImplement/Models/Production.cs | 5 +- 4 files changed, 70 insertions(+), 10 deletions(-) diff --git a/Course/DatabaseImplement/Models/Detail.cs b/Course/DatabaseImplement/Models/Detail.cs index d80048d..45988de 100644 --- a/Course/DatabaseImplement/Models/Detail.cs +++ b/Course/DatabaseImplement/Models/Detail.cs @@ -19,6 +19,7 @@ namespace DatabaseImplement.Models public virtual List DetailProducts { get; set; } = new(); [ForeignKey("DetailId")] public virtual List DetailProductions { get; set; } = new(); + public virtual Implementer User { get; set; } public static Detail? Create(DetailBindingModel model) { if (model == null) diff --git a/Course/DatabaseImplement/Models/Implementer.cs b/Course/DatabaseImplement/Models/Implementer.cs index 905b347..0dc064a 100644 --- a/Course/DatabaseImplement/Models/Implementer.cs +++ b/Course/DatabaseImplement/Models/Implementer.cs @@ -1,12 +1,71 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Contracts.BindingModels; +using Contracts.ViewModels; +using DataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; namespace DatabaseImplement.Models { - internal class Implementer + public class Implementer : IImplementerModel { + public int Id { get; set; } + [Required] + public string Email { get; set; } = string.Empty; + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Login { get; set; } = string.Empty; + [Required] + public string Password { get; set;} = string.Empty; + [ForeignKey("UserId")] + public virtual List Details { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Products { get; set; } = new(); + [ForeignKey("UserId")] + public virtual List Productions { get; set; } = new(); + + public static Implementer? Create(ImplementerBindingModel model) + { + if (model == null) + { + return null; + } + return new Implementer + { + Id = model.Id, + Email = model.Email, + Name = model.Name, + Login = model.Login, + Password = model.Password + }; + } + public static Implementer Create(ImplementerViewModel model) + { + return new Implementer + { + Id = model.Id, + Email = model.Email, + Name = model.Name, + Login = model.Login, + Password = model.Password + }; + } + public void Update(ImplementerBindingModel model) + { + if (model == null) + return; + Email = model.Email; + Name = model.Name; + Password = model.Password; + } + public ImplementerViewModel GetViewModel => new() + { + Id = Id, + Email = Email, + Name = Name, + Login = Login, + Password = Password + }; + } } diff --git a/Course/DatabaseImplement/Models/Product.cs b/Course/DatabaseImplement/Models/Product.cs index b79c0a5..6099269 100644 --- a/Course/DatabaseImplement/Models/Product.cs +++ b/Course/DatabaseImplement/Models/Product.cs @@ -15,6 +15,7 @@ namespace DatabaseImplement.Models public double Cost { get; set; } [Required] public int UserId { get; set; } + public virtual Implementer User { get; set; } private Dictionary? _detailProducts = null; [NotMapped] public Dictionary DetailProducts @@ -43,7 +44,7 @@ namespace DatabaseImplement.Models Name = model.Name, Cost = model.Cost, UserId = model.UserId, - Details = model.DetailProducts.Select(x => new DetailProduct + Details = model.ProductDetails.Select(x => new DetailProduct { Detail = context.Details.First(y => y.Id == x.Key), Count = x.Value.Item2 diff --git a/Course/DatabaseImplement/Models/Production.cs b/Course/DatabaseImplement/Models/Production.cs index 289fac8..1cd7cfd 100644 --- a/Course/DatabaseImplement/Models/Production.cs +++ b/Course/DatabaseImplement/Models/Production.cs @@ -31,10 +31,9 @@ namespace DatabaseImplement.Models } } [ForeignKey("ProductionId")] - public List Details { get; set; } - - [ForeignKey("UserId")] + public List Details { get; set; } = new(); public virtual Implementer User { get; set; } + public static Production Create(FactoryGoWorkDatabase context, ProductionBindingModel model) { return new Production() From 7830d9c21f7556d6a9771fa3f65dcc9ddceaba4b Mon Sep 17 00:00:00 2001 From: Sergey Kozyrev Date: Wed, 24 Apr 2024 12:50:12 +0400 Subject: [PATCH 3/3] ImplementerStorage --- .../DatabaseImplement.csproj | 4 -- .../Implements/ImplementerStorage.cs | 72 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 Course/DatabaseImplement/Implements/ImplementerStorage.cs diff --git a/Course/DatabaseImplement/DatabaseImplement.csproj b/Course/DatabaseImplement/DatabaseImplement.csproj index 3dbe418..dae78de 100644 --- a/Course/DatabaseImplement/DatabaseImplement.csproj +++ b/Course/DatabaseImplement/DatabaseImplement.csproj @@ -6,10 +6,6 @@ enable - - - - diff --git a/Course/DatabaseImplement/Implements/ImplementerStorage.cs b/Course/DatabaseImplement/Implements/ImplementerStorage.cs new file mode 100644 index 0000000..c96278d --- /dev/null +++ b/Course/DatabaseImplement/Implements/ImplementerStorage.cs @@ -0,0 +1,72 @@ +using DatabaseImplement.Models; +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Contracts.StoragesContracts; + +namespace DatabaseImplement.Implements +{ + public class ImplementerStorage : IImplementerStorage + { + public ImplementerViewModel? Delete(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (newImplementer == null) + return null; + context.Implementers.Remove(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? GetElement(ImplementerSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) { return null; } + using var context = new FactoryGoWorkDatabase(); + return context.Implementers.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id) || (!string.IsNullOrEmpty(model.Login) && x.Login.Equals(model.Login)))?.GetViewModel; ; + } + + public List GetFilteredList(ImplementerSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Login)) + return new(); + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Implementers.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Implementers.Where(x => x.Login.Equals(model.Login)).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Implementers.Select(x => x.GetViewModel).ToList(); + } + + public ImplementerViewModel? Insert(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = Implementer.Create(model); + if (newImplementer == null) + return null; + context.Implementers.Add(newImplementer); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + + public ImplementerViewModel? Update(ImplementerBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newImplementer = context.Implementers.FirstOrDefault(x => x.Id == model.Id); + if (newImplementer == null) + return null; + newImplementer.Update(model); + context.SaveChanges(); + return newImplementer.GetViewModel; + } + } +}