diff --git a/Course/Contracts/ViewModels/ProductionViewModel.cs b/Course/Contracts/ViewModels/ProductionViewModel.cs index f580fda..c47bea0 100644 --- a/Course/Contracts/ViewModels/ProductionViewModel.cs +++ b/Course/Contracts/ViewModels/ProductionViewModel.cs @@ -16,6 +16,6 @@ namespace Contracts.ViewModels [DisplayName("Цена производства")] public double Cost { get; set; } public int UserId { get; set; } - public Dictionary DetailProductions { get; set; } = new(); + public Dictionary DetailProductions { get; set; } = new(); } } diff --git a/Course/DatabaseImplement/DatabaseImplement.csproj b/Course/DatabaseImplement/DatabaseImplement.csproj index dae78de..184aa5f 100644 --- a/Course/DatabaseImplement/DatabaseImplement.csproj +++ b/Course/DatabaseImplement/DatabaseImplement.csproj @@ -7,6 +7,11 @@ + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + diff --git a/Course/DatabaseImplement/Implements/DetailStorage.cs b/Course/DatabaseImplement/Implements/DetailStorage.cs new file mode 100644 index 0000000..bfe1472 --- /dev/null +++ b/Course/DatabaseImplement/Implements/DetailStorage.cs @@ -0,0 +1,77 @@ +using DatabaseImplement.Models; +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.ViewModels; +using Contracts.StoragesContracts; + +namespace DatabaseImplement.Implements +{ + public class DetailStorage : IDetailStorage + { + public DetailViewModel? Delete(DetailBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newDetail = context.Details.FirstOrDefault(x => x.Id == model.Id); + if (newDetail == null) + return null; + context.Details.Remove(newDetail); + context.SaveChanges(); + return newDetail.GetViewModel; + } + + public DetailViewModel? GetElement(DetailSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Details.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(DetailSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Details.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Details.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Details.Where(x => model.Name == x.Name).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Details.Select(x => x.GetViewModel).ToList(); + } + + public DetailViewModel? Insert(DetailBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newDetail = Detail.Create(model); + if (newDetail == null) + return null; + context.Details.Add(newDetail); + context.SaveChanges(); + return newDetail.GetViewModel; + } + + public DetailViewModel? Update(DetailBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newDetail = context.Details.FirstOrDefault(x => x.Id == model.Id); + if (newDetail == null) + return null; + newDetail.Update(model); + context.SaveChanges(); + return newDetail.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Implements/ProductStorage.cs b/Course/DatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..4f835fa --- /dev/null +++ b/Course/DatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,77 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; + +namespace DatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduct = context.Products.FirstOrDefault(x => x.Id == model.Id); + if (newProduct == null) + return null; + context.Products.Remove(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Products.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ProductSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Products.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Products.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Products.Where(x => model.Name == x.Name).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Products.Select(x => x.GetViewModel).ToList(); + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + return null; + context.Products.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduct = context.Products.FirstOrDefault(x => x.Id == model.Id); + if (newProduct == null) + return null; + newProduct.Update(model); + context.SaveChanges(); + return newProduct.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Implements/ProductionStorage.cs b/Course/DatabaseImplement/Implements/ProductionStorage.cs new file mode 100644 index 0000000..b5f0dcd --- /dev/null +++ b/Course/DatabaseImplement/Implements/ProductionStorage.cs @@ -0,0 +1,77 @@ +using Contracts.BindingModels; +using Contracts.SearchModels; +using Contracts.StoragesContracts; +using Contracts.ViewModels; +using DatabaseImplement.Models; + +namespace DatabaseImplement.Implements +{ + public class ProductionionStorage : IProductionStorage + { + public ProductionViewModel? Delete(ProductionBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduction = context.Productions.FirstOrDefault(x => x.Id == model.Id); + if (newProduction == null) + return null; + context.Productions.Remove(newProduction); + context.SaveChanges(); + return newProduction.GetViewModel; + } + + public ProductionViewModel? GetElement(ProductionSearchModel model) + { + using var context = new FactoryGoWorkDatabase(); + return context.Productions.FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && x.Name.Contains(model.Name)) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ProductionSearchModel model) + { + if (!model.Id.HasValue && string.IsNullOrEmpty(model.Name) && !model.UserId.HasValue) + { + return new(); + } + using var context = new FactoryGoWorkDatabase(); + if (model.Id.HasValue) + { + return context.Productions.Where(x => x.Id == model.Id).Select(x => x.GetViewModel).ToList(); + } + else if (model.UserId.HasValue) + { + return context.Productions.Where(x => x.UserId == model.Id).Select(x => x.GetViewModel).ToList(); + } + else + { + return context.Productions.Where(x => model.Name == x.Name).Select(x => x.GetViewModel).ToList(); + } + } + + public List GetFullList() + { + using var context = new FactoryGoWorkDatabase(); + return context.Productions.Select(x => x.GetViewModel).ToList(); + } + + public ProductionViewModel? Insert(ProductionBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduction = Production.Create(context, model); + if (newProduction == null) + return null; + context.Productions.Add(newProduction); + context.SaveChanges(); + return newProduction.GetViewModel; + } + + public ProductionViewModel? Update(ProductionBindingModel model) + { + using var context = new FactoryGoWorkDatabase(); + var newProduction = context.Productions.FirstOrDefault(x => x.Id == model.Id); + if (newProduction == null) + return null; + newProduction.Update(model); + context.SaveChanges(); + return newProduction.GetViewModel; + } + } +} diff --git a/Course/DatabaseImplement/Models/DetailProduction.cs b/Course/DatabaseImplement/Models/DetailProduction.cs index 1c1a260..b9f1d55 100644 --- a/Course/DatabaseImplement/Models/DetailProduction.cs +++ b/Course/DatabaseImplement/Models/DetailProduction.cs @@ -14,8 +14,6 @@ namespace DatabaseImplement.Models 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 6099269..d3d568d 100644 --- a/Course/DatabaseImplement/Models/Product.cs +++ b/Course/DatabaseImplement/Models/Product.cs @@ -32,7 +32,7 @@ namespace DatabaseImplement.Models [ForeignKey("ProductId")] public virtual List Details { get; set; } = new(); - public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel model) + public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel? model) { if (model == null) { diff --git a/Course/DatabaseImplement/Models/Production.cs b/Course/DatabaseImplement/Models/Production.cs index 1cd7cfd..e1d8074 100644 --- a/Course/DatabaseImplement/Models/Production.cs +++ b/Course/DatabaseImplement/Models/Production.cs @@ -17,15 +17,15 @@ namespace DatabaseImplement.Models public double Cost { get; set; } [Required] public int UserId { get; set; } - private Dictionary? _detailProductions = null; + private Dictionary? _detailProductions = null; [NotMapped] - public Dictionary? DetailProductions + public Dictionary? DetailProductions { get { if (_detailProductions == null) { - _detailProductions = Details.ToDictionary(recDP => recDP.DetailId, recDp => (recDp.Detail as IDetailModel, recDp.Count)); + _detailProductions = Details.ToDictionary(recDP => recDP.DetailId, recDp => recDp.Detail as IDetailModel); } return _detailProductions; } @@ -68,12 +68,6 @@ namespace DatabaseImplement.Models { 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) @@ -82,7 +76,6 @@ namespace DatabaseImplement.Models { Production = production, Detail = context.Details.First(x => x.Id == dp.Key), - Count = dp.Value.Item2 }); context.SaveChanges(); }