diff --git a/BlacksmithWorkshop/BlacksmithWorkshop.sln b/BlacksmithWorkshop/BlacksmithWorkshop.sln index f1639fc..9de2964 100644 --- a/BlacksmithWorkshop/BlacksmithWorkshop.sln +++ b/BlacksmithWorkshop/BlacksmithWorkshop.sln @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BlacksmithWorkshopView", "B EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopFileImplement", "BlacksmithWorkshopFileImplement\BlacksmithWorkshopFileImplement.csproj", "{C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BlacksmithWorkshopDatabaseImplement", "BlacksmithWorkshopDatabaseImplement\BlacksmithWorkshopDatabaseImplement.csproj", "{99CF0DD1-5945-426C-9343-461A72204B74}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -45,6 +47,10 @@ Global {C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Debug|Any CPU.Build.0 = Debug|Any CPU {C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Release|Any CPU.ActiveCfg = Release|Any CPU {C6F64BD8-13F0-459D-AC7E-DFEDBE30C09A}.Release|Any CPU.Build.0 = Release|Any CPU + {99CF0DD1-5945-426C-9343-461A72204B74}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {99CF0DD1-5945-426C-9343-461A72204B74}.Debug|Any CPU.Build.0 = Debug|Any CPU + {99CF0DD1-5945-426C-9343-461A72204B74}.Release|Any CPU.ActiveCfg = Release|Any CPU + {99CF0DD1-5945-426C-9343-461A72204B74}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs new file mode 100644 index 0000000..5a3753d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabase.cs @@ -0,0 +1,21 @@ +using BlacksmithWorkshopDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace BlacksmithWorkshopDatabaseImplement +{ + public class BlacksmithWorkshopDatabase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseSqlServer(@"Data Source=CHESHIR\SQLEXPRESS;Initial Catalog=AbstractShopDatabaseFull;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Manufactures { set; get; } + public virtual DbSet ManufactureComponents { set; get; } + public virtual DbSet Orders { set; get; } + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj new file mode 100644 index 0000000..bcff7ff --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/BlacksmithWorkshopDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + + + diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Component.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..cc61a50 --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Component.cs @@ -0,0 +1,56 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string ComponentName { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [ForeignKey("ComponentId")] + public virtual List ManufactureComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public static Component Create(ComponentViewModel model) + { + return new Component + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs new file mode 100644 index 0000000..08ed6ec --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/Manufacture.cs @@ -0,0 +1,91 @@ +using BlacksmithWorkshopContracts.BindingModels; +using BlacksmithWorkshopContracts.ViewModels; +using BlacksmithWorkshopDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class Manufacture : IManufactureModel + { + public int Id { get; set; } + [Required] + public string ManufactureName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _manufactureComponents = null; + [NotMapped] + public Dictionary ManufactureComponents + { + get + { + if (_manufactureComponents == null) + { + _manufactureComponents = Components + .ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _manufactureComponents; + } + } + [ForeignKey("ManufactureId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ManufactureId")] + public virtual List Orders { get; set; } = new(); + public static Manufacture Create(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + return new Manufacture() + { + Id = model.Id, + ManufactureName = model.ManufactureName, + Price = model.Price, + Components = model.ManufactureComponents.Select(x => new ManufactureComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ManufactureBindingModel model) + { + ManufactureName = model.ManufactureName; + Price = model.Price; + } + public ManufactureViewModel GetViewModel => new() + { + Id = Id, + ManufactureName = ManufactureName, + Price = Price, + ManufactureComponents = ManufactureComponents + }; + public void UpdateComponents(BlacksmithWorkshopDatabase context, ManufactureBindingModel model) + { + var manufactureComponents = context.ManufactureComponents.Where(rec => + rec.ManufactureId == model.Id).ToList(); + if (manufactureComponents != null && manufactureComponents.Count > 0) + { // удалили те, которых нет в модели + context.ManufactureComponents.RemoveRange(manufactureComponents.Where(rec + => !model.ManufactureComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in manufactureComponents) + { + updateComponent.Count = model.ManufactureComponents[updateComponent.ComponentId].Item2; + model.ManufactureComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var manufacture = context.Manufactures.First(x => x.Id == Id); + foreach (var pc in model.ManufactureComponents) + { + context.ManufactureComponents.Add(new ManufactureComponent + { + Manufacture = manufacture, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _manufactureComponents = null; + } + } +} \ No newline at end of file diff --git a/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureComponent.cs b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureComponent.cs new file mode 100644 index 0000000..327592d --- /dev/null +++ b/BlacksmithWorkshop/BlacksmithWorkshopDatabaseImplement/Models/ManufactureComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace BlacksmithWorkshopDatabaseImplement.Models +{ + public class ManufactureComponent + { + public int Id { get; set; } + [Required] + public int ManufactureId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Manufacture Manufacture { get; set; } = new(); + } +}