TryForProduction

This commit is contained in:
Sergey Kozyrev 2024-04-27 21:07:45 +04:00
parent 605be259cc
commit e708ad671d
8 changed files with 241 additions and 14 deletions

View File

@ -16,6 +16,6 @@ namespace Contracts.ViewModels
[DisplayName("Цена производства")]
public double Cost { get; set; }
public int UserId { get; set; }
public Dictionary<int, (IDetailModel, int)> DetailProductions { get; set; } = new();
public Dictionary<int, IDetailModel> DetailProductions { get; set; } = new();
}
}

View File

@ -7,6 +7,11 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.29" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="6.0.29">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="6.0.22" />
</ItemGroup>

View File

@ -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<DetailViewModel> 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<DetailViewModel> 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;
}
}
}

View File

@ -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<ProductViewModel> 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<ProductViewModel> 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;
}
}
}

View File

@ -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<ProductionViewModel> 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<ProductionViewModel> 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;
}
}
}

View File

@ -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();

View File

@ -32,7 +32,7 @@ namespace DatabaseImplement.Models
[ForeignKey("ProductId")]
public virtual List<DetailProduct> Details { get; set; } = new();
public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel model)
public static Product? Create(FactoryGoWorkDatabase context, ProductBindingModel? model)
{
if (model == null)
{

View File

@ -17,15 +17,15 @@ namespace DatabaseImplement.Models
public double Cost { get; set; }
[Required]
public int UserId { get; set; }
private Dictionary<int, (IDetailModel, int)>? _detailProductions = null;
private Dictionary<int, IDetailModel>? _detailProductions = null;
[NotMapped]
public Dictionary<int, (IDetailModel, int)>? DetailProductions
public Dictionary<int, IDetailModel>? 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();
}