diff --git a/.gitignore b/.gitignore index ca1c7a3..a299f6f 100644 --- a/.gitignore +++ b/.gitignore @@ -398,3 +398,4 @@ FodyWeavers.xsd # JetBrains Rider *.sln.iml +*.dll diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index b51e620..2d4399b 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -13,7 +13,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopBusinessLogic" EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopListImplement", "ComputersShopListImplement\ComputersShopListImplement.csproj", "{F4C10F5F-A602-48E7-9820-C26B774203E7}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{AC4BC639-3B3F-48B1-87D2-22601554F418}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopFileImplement", "ComputersShopFileImplement\ComputersShopFileImplement.csproj", "{AC4BC639-3B3F-48B1-87D2-22601554F418}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopDataBaseImplement", "ComputersShopDataBaseImplement\ComputersShopDataBaseImplement.csproj", "{AC8AD056-EF22-4CCD-BD3F-FE91E2222993}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -45,6 +47,10 @@ Global {AC4BC639-3B3F-48B1-87D2-22601554F418}.Debug|Any CPU.Build.0 = Debug|Any CPU {AC4BC639-3B3F-48B1-87D2-22601554F418}.Release|Any CPU.ActiveCfg = Release|Any CPU {AC4BC639-3B3F-48B1-87D2-22601554F418}.Release|Any CPU.Build.0 = Release|Any CPU + {AC8AD056-EF22-4CCD-BD3F-FE91E2222993}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AC8AD056-EF22-4CCD-BD3F-FE91E2222993}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AC8AD056-EF22-4CCD-BD3F-FE91E2222993}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AC8AD056-EF22-4CCD-BD3F-FE91E2222993}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs index ff9403e..448695a 100644 --- a/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/ComputersShop/ComputersShopBusinessLogic/BusinessLogics/OrderLogic.cs @@ -59,7 +59,7 @@ namespace ComputersShopBusinessLogic.BusinessLogics if (model.Status == OrderStatus.Готов) { - model.DateImplement = DateTime.Now; + model.DateImplement = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); var computer = _computerStorage.GetElement(new() { Id = viewModel.ComputerId }); if (computer == null) { diff --git a/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs index b07eaa5..f6f11c6 100644 --- a/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs +++ b/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs @@ -15,7 +15,7 @@ namespace ComputersShopContracts.BindingModels public int Count { get; set; } public double Sum { get; set; } public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public DateTime? DateImplement { get; set; } } } diff --git a/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs index f154e9d..6713ac6 100644 --- a/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs +++ b/ComputersShop/ComputersShopContracts/BindingModels/ShopBindingModel.cs @@ -12,7 +12,7 @@ namespace ComputersShopContracts.BindingModels public int Id { get; set; } public string ShopName { get; set; } = string.Empty; public string ShopAddress { get; set; } = string.Empty; - public DateTime DateOpening { get; set; } = DateTime.Now; + public DateTime DateOpening { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); public Dictionary Computers { get; set; } = new(); public int Capacity { get; set; } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs index 5f077c9..47a42c4 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs @@ -23,7 +23,7 @@ namespace ComputersShopContracts.ViewModels [DisplayName("Статус")] public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; [DisplayName("Дата создания")] - public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime DateCreate { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } } diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs index 9b28754..342d3b0 100644 --- a/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs +++ b/ComputersShop/ComputersShopContracts/ViewModels/ShopViewModel.cs @@ -18,7 +18,7 @@ namespace ComputersShopContracts.ViewModels [DisplayName("Адрес магазина")] public string ShopAddress { get; set; } = string.Empty; [DisplayName("Дата открытия")] - public DateTime DateOpening { get; set; } = DateTime.Now; + public DateTime DateOpening { get; set; } = DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc); [DisplayName("Вместимость магазина")] public int Capacity { get; set; } } diff --git a/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs new file mode 100644 index 0000000..3a6e758 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBase.cs @@ -0,0 +1,29 @@ +using Microsoft.EntityFrameworkCore; +using ComputersShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement +{ + public class ComputersShopDataBase : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + if (optionsBuilder.IsConfigured == false) + { + optionsBuilder.UseNpgsql(@"Host=localhost;Port=5432;Database=ComputersShopDatabaseHard;Username=postgres;Password=adam200396789"); + } + base.OnConfiguring(optionsBuilder); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Computers { set; get; } + public virtual DbSet ComputerComponents { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet ShopComputers { set; get; } + public virtual DbSet Shops { set; get; } + } +} + diff --git a/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj new file mode 100644 index 0000000..d567dd6 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/ComputersShopDataBaseImplement.csproj @@ -0,0 +1,23 @@ + + + + net6.0 + enable + enable + + + + + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + + + + + + + + + + diff --git a/ComputersShop/ComputersShopDataBaseImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..fdaaaea --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,89 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + return context.Components + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new ComputersShopDataBase(); + 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 ComputersShopDataBase(); + 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 ComputersShopDataBase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputersShopDataBase(); + 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 ComputersShopDataBase(); + 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/ComputersShop/ComputersShopDataBaseImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..262ee6c --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/ComputerStorage.cs @@ -0,0 +1,110 @@ +using Microsoft.EntityFrameworkCore; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataBaseImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class ComputerStorage : IComputerStorage + { + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + return context.Computers.Include(x => x.Components) + .ThenInclude(x => x.Component) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public List GetFilteredList(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName)) + { + return new(); + } + using var context = new ComputersShopDataBase(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.ComputerName.Contains(model.ComputerName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && + !model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDataBase(); + return context.Computers + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComputerName) && + x.ComputerName == model.ComputerName) || + (model.Id.HasValue && x.Id == + model.Id)) + ?.GetViewModel; + } + public ComputerViewModel? Insert(ComputerBindingModel model) + { + using var context = new ComputersShopDataBase(); + var newProduct = Computer.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Computers.Add(newProduct); + context.SaveChanges(); + return newProduct.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + using var context = new ComputersShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var product = context.Computers.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 ComputerViewModel? Delete(ComputerBindingModel model) + { + using var context = new ComputersShopDataBase(); + var element = context.Computers + .Include(x => x.Components) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Computers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..627e1ad --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,106 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataBaseImplement.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputersShopDataBase(); + + var element = context.Orders.FirstOrDefault(rec => rec.Id == model.Id); + + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + + return GetViewModel(element); + } + + return null; + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + + using var context = new ComputersShopDataBase(); + + return GetViewModel(context.Orders.FirstOrDefault(x => (model.Id.HasValue && x.Id == model.Id))); + } + + public List GetFilteredList(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return new(); + } + + using var context = new ComputersShopDataBase(); + + return context.Orders.Where(x => x.Id == model.Id).Select(x => GetViewModel(x)).ToList(); + } + + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + + return context.Orders.Select(x => GetViewModel(x)).ToList(); + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + + using var context = new ComputersShopDataBase(); + + context.Orders.Add(newOrder); + context.SaveChanges(); + return GetViewModel(newOrder); + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputersShopDataBase(); + + var order = context.Orders.FirstOrDefault(x => x.Id == model.Id); + + if (order == null) + { + return null; + } + + order.Update(model); + context.SaveChanges(); + + return GetViewModel(order); + } + + private static OrderViewModel GetViewModel(Order order) + { + var viewModel = order.GetViewModel; + using var context = new ComputersShopDataBase(); + var element = context.Computers.FirstOrDefault(x => x.Id == order.ComputerId); + viewModel.ComputerName = element.ComputerName; + return viewModel; + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Implements/ShopStorage.cs b/ComputersShop/ComputersShopDataBaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..205d283 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,154 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataBaseImplement.Models; +using ComputersShopDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + using var context = new ComputersShopDataBase(); + var element = context.Shops + .Include(x => x.shopComputers) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Shops.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputersShopDataBase(); + return context.Shops + .Include(x => x.shopComputers) + .ThenInclude(x => x.Computer) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ShopName) && x.ShopName == model.ShopName) || (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.ShopName)) + { + return new(); + } + using var context = new ComputersShopDataBase(); + return context.Shops + .Include(x => x.shopComputers) + .ThenInclude(x => x.Computer) + .Where(x => x.ShopName.Contains(model.ShopName)) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputersShopDataBase(); + return context.Shops + .Include(x => x.shopComputers) + .ThenInclude(x => x.Computer) + .ToList() + .Select(x => x.GetViewModel) + .ToList(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + using var context = new ComputersShopDataBase(); + var newComputer = Shop.Create(context, model); + if (newComputer == null) + { + return null; + } + context.Shops.Add(newComputer); + context.SaveChanges(); + return newComputer.GetViewModel; + } + + public bool SellComputers(IComputerModel model, int quantity) + { + using var context = new ComputersShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + List shopsWithComputer = context.Shops.Include(x => x.shopComputers).ThenInclude(x => x.Computer).Where(x => x.shopComputers.Any(x => x.ComputerId == model.Id)).ToList(); + foreach (var shop in shopsWithComputer) + { + int computerInShopCount = shop.Computers[model.Id].Item2; + if (quantity - computerInShopCount >= 0) + { + quantity -= computerInShopCount; + context.ShopComputers.Remove(shop.shopComputers.FirstOrDefault(x => x.ComputerId == model.Id)!); + shop.Computers.Remove(model.Id); + } + else + { + shop.Computers[model.Id] = (model, computerInShopCount - quantity); + quantity = 0; + shop.UpdateComputers(context, new() + { + Id = shop.Id, + Computers = shop.Computers + }); + } + if (quantity == 0) + { + context.SaveChanges(); + transaction.Commit(); + return true; + } + } + transaction.Rollback(); + return false; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public ShopViewModel? Update(ShopBindingModel model) + { + using var context = new ComputersShopDataBase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var shop = context.Shops.FirstOrDefault(rec => + rec.Id == model.Id); + if (shop == null) + { + return null; + } + shop.Update(model); + context.SaveChanges(); + shop.UpdateComputers(context, model); + transaction.Commit(); + return shop.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.Designer.cs b/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.Designer.cs new file mode 100644 index 0000000..8d34469 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.Designer.cs @@ -0,0 +1,246 @@ +// +using System; +using ComputersShopDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ComputersShopDataBaseImplement.Migrations +{ + [DbContext(typeof(ComputersShopDataBase))] + [Migration("20230504182237_InitMig")] + partial class InitMig + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Computer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Computers"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ComputerId"); + + b.ToTable("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpening") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopAddress") + .IsRequired() + .HasColumnType("text"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", null) + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDataBaseImplement.Models.Shop", "Shop") + .WithMany("shopComputers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b => + { + b.Navigation("shopComputers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.cs b/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.cs new file mode 100644 index 0000000..c3de5ee --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Migrations/20230504182237_InitMig.cs @@ -0,0 +1,185 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ComputersShopDataBaseImplement.Migrations +{ + /// + public partial class InitMig : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "Components", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComponentName = table.Column(type: "text", nullable: false), + Cost = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Components", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Computers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComputerName = table.Column(type: "text", nullable: false), + Price = table.Column(type: "double precision", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Computers", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "Shops", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopName = table.Column(type: "text", nullable: false), + ShopAddress = table.Column(type: "text", nullable: false), + DateOpening = table.Column(type: "timestamp with time zone", nullable: false), + Capacity = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_Shops", x => x.Id); + }); + + migrationBuilder.CreateTable( + name: "ComputerComponents", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComputerId = table.Column(type: "integer", nullable: false), + ComponentId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ComputerComponents", x => x.Id); + table.ForeignKey( + name: "FK_ComputerComponents_Components_ComponentId", + column: x => x.ComponentId, + principalTable: "Components", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ComputerComponents_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "Orders", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ComputerId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false), + Sum = table.Column(type: "double precision", nullable: false), + Status = table.Column(type: "integer", nullable: false), + DateCreate = table.Column(type: "timestamp with time zone", nullable: false), + DateImplement = table.Column(type: "timestamp with time zone", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("PK_Orders", x => x.Id); + table.ForeignKey( + name: "FK_Orders_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "ShopComputers", + columns: table => new + { + Id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + ShopId = table.Column(type: "integer", nullable: false), + ComputerId = table.Column(type: "integer", nullable: false), + Count = table.Column(type: "integer", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_ShopComputers", x => x.Id); + table.ForeignKey( + name: "FK_ShopComputers_Computers_ComputerId", + column: x => x.ComputerId, + principalTable: "Computers", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + table.ForeignKey( + name: "FK_ShopComputers_Shops_ShopId", + column: x => x.ShopId, + principalTable: "Shops", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "IX_ComputerComponents_ComponentId", + table: "ComputerComponents", + column: "ComponentId"); + + migrationBuilder.CreateIndex( + name: "IX_ComputerComponents_ComputerId", + table: "ComputerComponents", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_Orders_ComputerId", + table: "Orders", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ComputerId", + table: "ShopComputers", + column: "ComputerId"); + + migrationBuilder.CreateIndex( + name: "IX_ShopComputers_ShopId", + table: "ShopComputers", + column: "ShopId"); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "ComputerComponents"); + + migrationBuilder.DropTable( + name: "Orders"); + + migrationBuilder.DropTable( + name: "ShopComputers"); + + migrationBuilder.DropTable( + name: "Components"); + + migrationBuilder.DropTable( + name: "Computers"); + + migrationBuilder.DropTable( + name: "Shops"); + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Migrations/ComputersShopDataBaseModelSnapshot.cs b/ComputersShop/ComputersShopDataBaseImplement/Migrations/ComputersShopDataBaseModelSnapshot.cs new file mode 100644 index 0000000..f6bf8e0 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Migrations/ComputersShopDataBaseModelSnapshot.cs @@ -0,0 +1,243 @@ +// +using System; +using ComputersShopDataBaseImplement; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace ComputersShopDataBaseImplement.Migrations +{ + [DbContext(typeof(ComputersShopDataBase))] + partial class ComputersShopDataBaseModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "7.0.3") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Cost") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Components"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Computer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerName") + .IsRequired() + .HasColumnType("text"); + + b.Property("Price") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.ToTable("Computers"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComponentId") + .HasColumnType("integer"); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComponentId"); + + b.HasIndex("ComputerId"); + + b.ToTable("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("DateCreate") + .HasColumnType("timestamp with time zone"); + + b.Property("DateImplement") + .HasColumnType("timestamp with time zone"); + + b.Property("Status") + .HasColumnType("integer"); + + b.Property("Sum") + .HasColumnType("double precision"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.ToTable("Orders"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("Capacity") + .HasColumnType("integer"); + + b.Property("DateOpening") + .HasColumnType("timestamp with time zone"); + + b.Property("ShopAddress") + .IsRequired() + .HasColumnType("text"); + + b.Property("ShopName") + .IsRequired() + .HasColumnType("text"); + + b.HasKey("Id"); + + b.ToTable("Shops"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("ComputerId") + .HasColumnType("integer"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("ShopId") + .HasColumnType("integer"); + + b.HasKey("Id"); + + b.HasIndex("ComputerId"); + + b.HasIndex("ShopId"); + + b.ToTable("ShopComputers"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ComputerComponent", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Component", "Component") + .WithMany("ComputerComponents") + .HasForeignKey("ComponentId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") + .WithMany("Components") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Component"); + + b.Navigation("Computer"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Order", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", null) + .WithMany("Orders") + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.ShopComputer", b => + { + b.HasOne("ComputersShopDataBaseImplement.Models.Computer", "Computer") + .WithMany() + .HasForeignKey("ComputerId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.HasOne("ComputersShopDataBaseImplement.Models.Shop", "Shop") + .WithMany("shopComputers") + .HasForeignKey("ShopId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired(); + + b.Navigation("Computer"); + + b.Navigation("Shop"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Component", b => + { + b.Navigation("ComputerComponents"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Computer", b => + { + b.Navigation("Components"); + + b.Navigation("Orders"); + }); + + modelBuilder.Entity("ComputersShopDataBaseImplement.Models.Shop", b => + { + b.Navigation("shopComputers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/Component.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Component.cs new file mode 100644 index 0000000..2f4742a --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Component.cs @@ -0,0 +1,64 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; + +namespace ComputersShopDataBaseImplement.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 ComputerComponents { 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/ComputersShop/ComputersShopDataBaseImplement/Models/Computer.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Computer.cs new file mode 100644 index 0000000..60b02b3 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Computer.cs @@ -0,0 +1,98 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; + +namespace ComputersShopDataBaseImplement.Models +{ + public class Computer : IComputerModel + { + public int Id { get; set; } + [Required] + public string ComputerName { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _ComputerComponents = null; + [NotMapped] + public Dictionary ComputerComponents + { + get + { + if (_ComputerComponents == null) + { + _ComputerComponents = Components.ToDictionary(recPC => recPC.ComponentId, recPC => + (recPC.Component as IComponentModel, recPC.Count)); + } + return _ComputerComponents; + } + } + [ForeignKey("ComputerId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ComputerId")] + public virtual List Orders { get; set; } = new(); + public static Computer Create(ComputersShopDataBase context, ComputerBindingModel model) + { + return new Computer() + { + Id = model.Id, + ComputerName = model.ComputerName, + Price = model.Price, + Components = model.ComputerComponents.Select(x => new ComputerComponent + { + Component = context.Components.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ComputerBindingModel model) + { + ComputerName = model.ComputerName; + Price = model.Price; + } + public ComputerViewModel GetViewModel => new() + { + Id = Id, + ComputerName = ComputerName, + Price = Price, + ComputerComponents = ComputerComponents + }; + public void UpdateComponents(ComputersShopDataBase context, + ComputerBindingModel model) + { + var ComputerComponents = context.ComputerComponents.Where(rec => + rec.ComputerId == model.Id).ToList(); + if (ComputerComponents != null && ComputerComponents.Count > 0) + { // удалили те, которых нет в модели + context.ComputerComponents.RemoveRange(ComputerComponents.Where(rec + => !model.ComputerComponents.ContainsKey(rec.ComponentId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateComponent in ComputerComponents) + { + updateComponent.Count = model.ComputerComponents[updateComponent.ComponentId].Item2; + model.ComputerComponents.Remove(updateComponent.ComponentId); + } + context.SaveChanges(); + } + var Computer = context.Computers.First(x => x.Id == Id); + foreach (var pc in model.ComputerComponents) + { + context.ComputerComponents.Add(new ComputerComponent + { + Computer = Computer, + Component = context.Components.First(x => x.Id == pc.Key), + Count = pc.Value.Item2 + }); + context.SaveChanges(); + } + _ComputerComponents = null; + } + } +} + diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/ComputerComponent.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/ComputerComponent.cs new file mode 100644 index 0000000..145661e --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/ComputerComponent.cs @@ -0,0 +1,24 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Models +{ + public class ComputerComponent + { + public int Id { get; set; } + [Required] + public int ComputerId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Computer Computer { get; set; } = new(); + + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/Order.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Order.cs new file mode 100644 index 0000000..014e3fd --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Order.cs @@ -0,0 +1,70 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Models +{ + public class Order : IOrderModel + { + [Required] + public int ComputerId { get; set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } + [Required] + public DateTime DateCreate { get; set; } + + public DateTime? DateImplement { get; set; } + + public int Id { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ComputerId = model.ComputerId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + + public OrderViewModel GetViewModel => new() + { + Id = Id, + ComputerId = ComputerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/Shop.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/Shop.cs new file mode 100644 index 0000000..f883033 --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/Shop.cs @@ -0,0 +1,109 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Net; + +namespace ComputersShopDataBaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string ShopName { get; set; } = string.Empty; + [Required] + public string ShopAddress { get; set; } = string.Empty; + [Required] + public DateTime DateOpening { get; set; } + [Required] + public int Capacity { get; set; } + + private Dictionary? _computers = null; + + [NotMapped] + public Dictionary Computers + { + get + { + if (_computers == null) + { + _computers = shopComputers + .ToDictionary(rec => rec.ComputerId, + rec => (rec.Computer as IComputerModel, + rec.Count)); + } + return _computers; + } + } + [ForeignKey("ShopId")] + public virtual List shopComputers { get; set; } = new(); + public static Shop Create(ComputersShopDataBase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + ShopName = model.ShopName, + ShopAddress = model.ShopAddress, + DateOpening = model.DateOpening, + Capacity = model.Capacity, + shopComputers = model.Computers.Select(x => new ShopComputer + { + Computer = context.Computers.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + + public void Update(ShopBindingModel model) + { + ShopName = model.ShopName; + ShopAddress = model.ShopAddress; + DateOpening = model.DateOpening; + Capacity = model.Capacity; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + ShopAddress = ShopAddress, + DateOpening = DateOpening, + Capacity = Capacity, + Computers = Computers + }; + + public void UpdateComputers(ComputersShopDataBase context, ShopBindingModel model) + { + var computers = context.ShopComputers.Where(rec => rec.ShopId == model.Id).ToList(); + if (computers != null && computers.Count > 0) + { + context.ShopComputers.RemoveRange(computers.Where(rec => !model.Computers.ContainsKey(rec.ComputerId))); + context.SaveChanges(); + foreach (var computer in computers) + { + computer.Count = model.Computers[computer.ComputerId].Item2; + model.Computers.Remove(computer.ComputerId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var id in model.Computers) + { + context.ShopComputers.Add(new ShopComputer + { + Shop = shop, + Computer = context.Computers.First(x => x.Id == id.Key), + Count = id.Value.Item2 + }); + context.SaveChanges(); + } + _computers = null; + } + } +} diff --git a/ComputersShop/ComputersShopDataBaseImplement/Models/ShopComputer.cs b/ComputersShop/ComputersShopDataBaseImplement/Models/ShopComputer.cs new file mode 100644 index 0000000..88470bd --- /dev/null +++ b/ComputersShop/ComputersShopDataBaseImplement/Models/ShopComputer.cs @@ -0,0 +1,28 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataBaseImplement.Models +{ + public class ShopComputer + { + public int Id { get; set; } + + [Required] + public int ShopId { get; set; } + + [Required] + public int ComputerId { get; set; } + + [Required] + public int Count { get; set; } + + public virtual Computer Computer { get; set; } = new(); + + public virtual Shop Shop { get; set; } = new(); + } +} diff --git a/ComputersShop/ComputersShopView/ComputersShopView.csproj b/ComputersShop/ComputersShopView/ComputersShopView.csproj index ea5d5cb..c0cdbc4 100644 --- a/ComputersShop/ComputersShopView/ComputersShopView.csproj +++ b/ComputersShop/ComputersShopView/ComputersShopView.csproj @@ -9,6 +9,10 @@ + + all + runtime; build; native; contentfiles; analyzers; buildtransitive + @@ -18,6 +22,7 @@ + diff --git a/ComputersShop/ComputersShopView/FormShop.cs b/ComputersShop/ComputersShopView/FormShop.cs index 91ff44f..f96f648 100644 --- a/ComputersShop/ComputersShopView/FormShop.cs +++ b/ComputersShop/ComputersShopView/FormShop.cs @@ -100,7 +100,7 @@ namespace ComputersShopView Id = _id ?? 0, ShopName = textBoxName.Text, ShopAddress = textBoxAddress.Text, - DateOpening = dateTimePicker.Value.Date, + DateOpening = DateTime.SpecifyKind(DateTime.Parse(dateTimePicker.Text), DateTimeKind.Utc), Capacity = (int)numericUpDownCapacity.Value, Computers = _shopComputers }; diff --git a/ComputersShop/ComputersShopView/Program.cs b/ComputersShop/ComputersShopView/Program.cs index 68bf655..096eafa 100644 --- a/ComputersShop/ComputersShopView/Program.cs +++ b/ComputersShop/ComputersShopView/Program.cs @@ -1,7 +1,7 @@ using ComputersShopBusinessLogic.BusinessLogics; using ComputersShopContracts.BusinessLogicContracts; using ComputersShopContracts.StoragesContracts; -using ComputersShopFileImplement.Implements; +using ComputersShopDataBaseImplement.Implements; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using NLog.Extensions.Logging;