diff --git a/ComputerHardwareStore/ComputerHardwareStore.sln b/ComputerHardwareStore/ComputerHardwareStore.sln index 1edd1ac..14229ae 100644 --- a/ComputerHardwareStore/ComputerHardwareStore.sln +++ b/ComputerHardwareStore/ComputerHardwareStore.sln @@ -9,7 +9,9 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDataMo EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreContracts", "ComputerHardwareStoreContracts\ComputerHardwareStoreContracts.csproj", "{5F394E21-2597-432B-AE73-BBAFD8D9F50E}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreBusinessLogic", "ComputerHardwareStoreBusinessLogic\ComputerHardwareStoreBusinessLogic.csproj", "{D32DEB60-AF40-46AF-8914-DC6A19BD66CD}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputerHardwareStoreDatabaseImplement", "ComputerHardwareStoreDatabaseImplement\ComputerHardwareStoreDatabaseImplement.csproj", "{93BD4E28-48D8-4D3A-87FB-FB96F00DA64B}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution diff --git a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj index 47a15a0..982ae40 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj +++ b/ComputerHardwareStore/ComputerHardwareStoreBusinessLogic/ComputerHardwareStoreBusinessLogic.csproj @@ -6,6 +6,12 @@ enable + + + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs index e437bec..e52972b 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/BuildBindingModel.cs @@ -7,7 +7,8 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Price { get; set; } - public int VendorId { get; set; } + public IVendorModel Vendor { get; set; } public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs index af0759b..e14244d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/ComponentBindingModel.cs @@ -7,6 +7,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public int Id { get; set; } public string Name { get; set; } = string.Empty; public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs index 9cf80bd..e1017a0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/OrderBindingModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.BindingModels public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; public DateTime DateCreate { get; set; } = DateTime.Now; public DateTime? DateImplement { get; set; } = null; - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs index af08205..e28c326 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/BindingModels/PurchaseBindingModel.cs @@ -8,7 +8,8 @@ namespace ComputerHardwareStoreContracts.BindingModels public int VendorId { get; set; } public double Cost { get; set; } public DateTime DateCreate { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public DateTime? DateImplement { get; set; } + public Dictionary PurchaseBuilds { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs index a6183f7..5b6b533 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/BuildSearchModel.cs @@ -3,7 +3,7 @@ public class BuildSearchModel { public int? Id { get; set; } + public int VendorId { get; set; } public string? Name { get; set; } - public int? VendorId { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs index 1378964..3dcc2c0 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/CommentSearchModel.cs @@ -3,6 +3,5 @@ public class CommentSearchModel { public int? Id { get; set; } - public DateTime? Date { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs index 046e6d7..1271144 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/ProductSearchModel.cs @@ -4,6 +4,7 @@ { public int? Id { get; set; } public string? Name { get; set; } - public int? StoreKeeperId { get; set; } + public int VendorId { get; set; } + public List? Builds { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs index ed748fe..21c4265 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/SearchModels/StoreKeeperSearchModel.cs @@ -4,6 +4,5 @@ { public int? Id { get; set; } public string? Login { get; set; } - public string? Password { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs index 763cf75..8274f6d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/BuildViewModel.cs @@ -7,10 +7,11 @@ namespace ComputerHardwareStoreContracts.ViewModels { public int Id { get; set; } [DisplayName("Название сборки")] - public string Name { get; } = string.Empty; + public string Name { get; set; } = string.Empty; [DisplayName("Стоимость")] - public double Price { get; } - public int VendorId { get; } - public Dictionary BuildComponents { get; } = new(); + public double Price { get; set; } + public IVendorModel Vendor { get; set; } + public Dictionary BuildComponents { get; set; } = new(); + public List Comments { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs index b62b98c..0978a2f 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/CommentViewModel.cs @@ -7,6 +7,5 @@ namespace ComputerHardwareStoreContracts.ViewModels public int Id { get; set; } public DateTime Date { get; set; } public string Text { get; set; } = string.Empty; - public int BuildId { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs index 6aadb04..eb9fd9a 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ComponentViewModel.cs @@ -10,6 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Cost { get; set; } - public int StoreKeeperId { get; set; } + public IStoreKeeperModel StoreKeeper { get; set; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs index d2f0ff1..a0b6602 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/OrderViewModel.cs @@ -16,6 +16,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public DateTime DateCreate { get; set; } [DisplayName("Дата выполнения")] public DateTime? DateImplement { get; set; } - public Dictionary OrderProduct { get; set; } = new(); + public Dictionary OrderProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs index 00cc4c2..c1ba122 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/ProductViewModel.cs @@ -10,8 +10,6 @@ namespace ComputerHardwareStoreContracts.ViewModels public string Name { get; set; } = string.Empty; [DisplayName("Цена")] public double Price { get; set; } - public Dictionary CannedComponents { get; set; } = new(); - - public Dictionary ProductComponents => throw new NotImplementedException(); + public Dictionary ProductComponents { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs index d0de94d..4911e1d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreContracts/ViewModels/PurchaseViewModel.cs @@ -10,11 +10,12 @@ namespace ComputerHardwareStoreContracts.ViewModels public double Cost { get; set; } [DisplayName("Дата создания")] public DateTime DateCreate { get; set; } + public DateTime? DateImplement { get; set; } public int VendorId { get; set; } [DisplayName("Сумма")] public double Sum { get; set; } - public Dictionary PurchaseBuild { get; set; } = new(); + public Dictionary PurchaseBuilds { get; set; } = new(); - public Dictionary PurchaseProduct { get; set; } = new(); + public Dictionary PurchaseProducts { get; set; } = new(); } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs index 6485ba0..dee1777 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IBuildModel.cs @@ -4,7 +4,8 @@ { string Name { get; } double Price { get; } - int VendorId { get; } + IVendorModel Vendor { get; } public Dictionary BuildComponents { get; } + public List Comments { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs index 76d1cdd..da32990 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/ICommentModel.cs @@ -4,6 +4,5 @@ { DateTime Date { get; } string Text { get; } - int BuildId { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs index 6eea153..84f259d 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IComponentModel.cs @@ -4,6 +4,6 @@ { string Name { get; } double Cost { get; } - int StoreKeeperId { get; } + IStoreKeeperModel StoreKeeper { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs index 06f933b..3e7f94e 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IOrderModel.cs @@ -8,6 +8,6 @@ namespace ComputerHardwareStoreDataModels.Models OrderStatus Status { get; } DateTime DateCreate { get; } DateTime? DateImplement { get; } - public Dictionary OrderProduct { get; } + public Dictionary OrderProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs index 9afa7b6..efc0095 100644 --- a/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs +++ b/ComputerHardwareStore/ComputerHardwareStoreDataModels/Models/IPurchaseModel.cs @@ -5,7 +5,8 @@ int VendorId { get; } double Cost { get; } DateTime DateCreate { get; } - public Dictionary PurchaseBuild { get; } - public Dictionary PurchaseProduct { get; } + DateTime? DateImplement { get; } + public Dictionary PurchaseBuilds { get; } + public Dictionary PurchaseProducts { get; } } } diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs new file mode 100644 index 0000000..0be2db5 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDBContext.cs @@ -0,0 +1,44 @@ +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement +{ + public class ComputerHardwareStoreDBContext : DbContext + { + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + /* + * в program добавить: + * // получаем строку подключения из файла конфигурации + * string connection = builder.Configuration.GetConnectionString("DefaultConnection"); + * + * // добавляем контекст ApplicationContext в качестве сервиса в приложение + * builder.Services.AddDbContext(options => options.UseSqlServer(connection)); + * + * в appsettings: + * "ConnectionStrings": { + * "DefaultConnection": "Host=localhost;Database=ProductBar_db;Username=compstore;Password=compstore" + * }, + */ + + base.OnConfiguring(optionsBuilder); + AppContext.SetSwitch("Npgsql.EnableLegacyTimestampBehavior", true); + AppContext.SetSwitch("Npgsql.DisableDateTimeInfinityConversions", true); + } + public virtual DbSet Components { set; get; } + public virtual DbSet Products { set; get; } + public virtual DbSet ProductComponents { set; get; } + + public virtual DbSet StoreKeepers { set; get; } + public virtual DbSet Builds { set; get; } + public virtual DbSet Orders { set; get; } + public virtual DbSet OrderProducts { set; get; } + public virtual DbSet BuildComponents { set; get; } + public virtual DbSet Comments { set; get; } + public virtual DbSet Vendors { set; get; } + public virtual DbSet Purchases { set; get; } + public virtual DbSet PurchaseBuilds { set; get; } + public virtual DbSet PurchaseProducts { set; get; } + public virtual DbSet Vendors { set; get; } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj new file mode 100644 index 0000000..a7da7e9 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/ComputerHardwareStoreDatabaseImplement.csproj @@ -0,0 +1,19 @@ + + + + net8.0 + enable + enable + + + + + + + + + + + + + diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs new file mode 100644 index 0000000..d546f43 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/BuildStorage.cs @@ -0,0 +1,117 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class BuildStorage : IBuildStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Select(b => b.GetViewModel) + .ToList(); + } + + public List GetFilteredList(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId ) + .Where(b => b.Name.Contains(model.Name)) + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Select(b => b.GetViewModel) + .ToList(); + } + + public BuildViewModel? GetElement(BuildSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Builds + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.VendorId) + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(b => + (!string.IsNullOrEmpty(model.Name) && b.Name == model.Name) || + (model.Id.HasValue && b.Id == model.Id))? + .GetViewModel; + } + + public BuildViewModel? Insert(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newBuild = Build.Create(context, model); + if (newBuild == null) + { + return null; + } + context.Builds.Add(newBuild); + context.SaveChanges(); + return context.Builds + .Include(b => b.Vendor) + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .FirstOrDefault(p => p.Id == newBuild.Id)? + .GetViewModel; + } + + public BuildViewModel? Update(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return build.GetViewModel; + } + public BuildViewModel? Delete(BuildBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var build = context.Builds + .Include(b => b.Comments) + .Include(b => b.Components) + .ThenInclude(b => b.Component) + .Include(b => b.Vendor) + .Where(b => b.Vendor.Id == model.Vendor.Id) + .FirstOrDefault(p => p.Id == model.Id); + if (build == null) + { + return null; + } + context.Builds.Remove(build); + context.SaveChanges(); + return build.GetViewModel; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs new file mode 100644 index 0000000..a357455 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/CommentStorage.cs @@ -0,0 +1,77 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class CommentStorage : ICommentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFilteredList(CommentSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .Select(x => x.GetViewModel) + .ToList(); + } + + public CommentViewModel? GetElement(CommentSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Comments + .FirstOrDefault(x => x.Id == model.Id) + ?.GetViewModel; + } + + public CommentViewModel? Insert(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComment = Comment.Create(model); + if (newComment == null) + { + return null; + } + context.Comments.Add(newComment); + context.SaveChanges(); + return newComment.GetViewModel; + } + public CommentViewModel? Update(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + + public CommentViewModel? Delete(CommentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Comments.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Comments.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..9fdc559 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ComponentStorage.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Select(c => c.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .Where(c => c.Name.Contains(model.Name)) + .Select(c => c.GetViewModel) + .ToList(); + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Components + .FirstOrDefault(c => + (!string.IsNullOrEmpty(model.Name) && c .Name == model.Name) || + (model.Id.HasValue && c.Id == model.Id)) + ?.GetViewModel; + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newComponent = Component.Create(context, model); + if (newComponent == null) + { + return null; + } + context.Components.Add(newComponent); + context.SaveChanges(); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Components.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Components.FirstOrDefault(c => c.Id == model.Id); + if (element == null) + { + return null; + } + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..2378d44 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/OrderStorage.cs @@ -0,0 +1,108 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Select(o => o.GetViewModel) + .ToList(); + } + + public List GetFilteredList(OrderSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .Where(o => + (model.Id.HasValue && o.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= o.DateCreate && o.DateCreate <= model.DateTo) || + (o.DateImplement.HasValue && o.DateCreate < model.DateFrom && model.DateFrom < o.DateImplement))) + ) + .Select(o => o.GetViewModel) + .ToList(); + } + + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id)? + .GetViewModel; + + } + + public OrderViewModel? Insert(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newOrder = Order.Create(context, model); + if (newOrder == null) + { + return null; + } + context.Orders.Add(newOrder); + context.SaveChanges(); + return newOrder.GetViewModel; + } + + public OrderViewModel? Update(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var order = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (order == null) + { + return null; + } + order.Update(model); + context.SaveChanges(); + transaction.Commit(); + return order.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public OrderViewModel? Delete(OrderBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Orders + .Include(o => o.Products) + .ThenInclude(o => o.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Orders.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs new file mode 100644 index 0000000..ae83b6b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/ProductStorage.cs @@ -0,0 +1,102 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class ProductStorage : IProductStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Where(p => p.Name.Contains(model.Name)) + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .Select(p => p.GetViewModel) + .ToList(); + } + + public ProductViewModel? GetElement(ProductSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => + (!string.IsNullOrEmpty(model.Name) && p.Name == model.Name) || + (model.Id.HasValue && p.Id == model.Id))? + .GetViewModel; + } + + public ProductViewModel? Insert(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newProduct = Product.Create(context, model); + if (newProduct == null) + { + return null; + } + context.Products.Add(newProduct); + context.SaveChanges(); + return context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == newProduct.Id)? + .GetViewModel; + } + + public ProductViewModel? Update(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Update(model); + context.SaveChanges(); + return product.GetViewModel; + } + + public ProductViewModel? Delete(ProductBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var product = context.Products + .Include(p => p.Components) + .ThenInclude(p => p.Component) + .FirstOrDefault(p => p.Id == model.Id); + if (product == null) + { + return null; + } + context.Products.Remove(product); + context.SaveChanges(); + return product.GetViewModel; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs new file mode 100644 index 0000000..bbce924 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/PurchaseStorage.cs @@ -0,0 +1,117 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class PurchaseStorage : IPurchaseStorage + { + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Select(p => p.GetViewModel) + .ToList(); + } + + public List GetFilteredList(PurchaseSearchModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .Where(p => + (model.Id.HasValue && p.Id == model.Id) || + ((model.DateFrom.HasValue && model.DateTo.HasValue) && + ((model.DateFrom <= p.DateCreate && p.DateCreate <= model.DateTo) || + (p.DateImplement.HasValue && p.DateCreate < model.DateFrom && model.DateFrom < p.DateImplement))) + ) + .Select(p => p.GetViewModel) + .ToList(); + } + + public PurchaseViewModel? GetElement(PurchaseSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id)? + .GetViewModel; + } + + public PurchaseViewModel? Insert(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newPurchase = Purchase.Create(context, model); + if (newPurchase == null) + { + return null; + } + context.Purchases.Add(newPurchase); + context.SaveChanges(); + return newPurchase.GetViewModel; + } + + public PurchaseViewModel? Update(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + using var transaction = context.Database.BeginTransaction(); + try + { + var purchase = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(p => p.Id == model.Id); + if (purchase == null) + { + return null; + } + purchase.Update(model); + context.SaveChanges(); + transaction.Commit(); + return purchase.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + + public PurchaseViewModel? Delete(PurchaseBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Purchases + .Include(p => p.Builds) + .ThenInclude(p => p.Build) + .Include(p => p.Products) + .ThenInclude(p => p.Product) + .FirstOrDefault(o => o.Id == model.Id); + if (element != null) + { + context.Purchases.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs new file mode 100644 index 0000000..bd4aff8 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/StoreKeeperStorage.cs @@ -0,0 +1,86 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; +using Microsoft.EntityFrameworkCore; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class StoreKeeperStorage : IStoreKeeperStorage + { + public StoreKeeperViewModel? GetElement(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(StoreKeeperSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.StoreKeepers + .Include(x => x.Components) + .Select(x => x.GetViewModel) + .ToList(); + } + + public StoreKeeperViewModel? Insert(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newStoreKeeper = StoreKeeper.Create(context, model); + if (newStoreKeeper == null) + { + return null; + } + context.StoreKeepers.Add(newStoreKeeper); + context.SaveChanges(); + return newStoreKeeper.GetViewModel; + } + + public StoreKeeperViewModel? Update(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public StoreKeeperViewModel? Delete(StoreKeeperBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.StoreKeepers.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.StoreKeepers.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs new file mode 100644 index 0000000..1e74a2a --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Implements/Vendor.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.SearchModels; +using ComputerHardwareStoreContracts.StorageContracts; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDatabaseImplement.Models; + +namespace ComputerHardwareStoreDatabaseImplement.Implements +{ + public class VendorStorage : IVendorStorage + { + public VendorViewModel? GetElement(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login) && !model.Id.HasValue) + { + return null; + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.Login) + && x.Login == model.Login) + || (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(VendorSearchModel model) + { + if (string.IsNullOrEmpty(model.Login)) + { + return new(); + } + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Where(x => x.Login.Contains(model.Login)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new ComputerHardwareStoreDBContext(); + return context.Vendors + .Select(x => x.GetViewModel) + .ToList(); + } + + public VendorViewModel? Insert(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var newVendor = Vendor.Create(context, model); + if (newVendor == null) + { + return null; + } + context.Vendors.Add(newVendor); + context.SaveChanges(); + return newVendor.GetViewModel; + } + + public VendorViewModel? Update(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(x => x.Id == model.Id); + if (element == null) + { + return null; + } + element.Update(model); + context.SaveChanges(); + return element.GetViewModel; + } + public VendorViewModel? Delete(VendorBindingModel model) + { + using var context = new ComputerHardwareStoreDBContext(); + var element = context.Vendors.FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Vendors.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs new file mode 100644 index 0000000..c4c8f29 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Build.cs @@ -0,0 +1,88 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore.Metadata.Internal; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Build : IBuildModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + [NotMapped] + private Dictionary? _buildComponents = null; + [NotMapped] + public Dictionary BuildComponents + { + get + { + if (_buildComponents == null) + { + _buildComponents = Components + .ToDictionary(bc => bc.ComponentId, bc => + (bc.Component as IComponentModel, bc.Count)); + } + return _buildComponents; + } + } + [ForeignKey("BuildId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("BuildId")] + + public virtual List Comments { get; set; } = new(); + [NotMapped] + List IBuildModel.Comments => Comments.Select(c => c as ICommentModel).ToList(); + + public virtual Vendor Vendor { get; private set; } = new(); + IVendorModel IBuildModel.Vendor => Vendor as IVendorModel; + + public static Build? Create(ComputerHardwareStoreDBContext context, BuildBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Vendor = context.Vendors.First(v => v.Id == model.Vendor.Id), + Components = context.Components + .Where(c => model.BuildComponents.ContainsKey(c.Id)) + .Select(c => new BuildComponent() + { + BuildId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.BuildComponents[c.Id].Item2 + }).ToList() + }; + } + + public void Update(BuildBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + } + + public BuildViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + Vendor = Vendor, + Comments = Comments.Select(c => c as ICommentModel).ToList(), + BuildComponents = BuildComponents + }; + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs new file mode 100644 index 0000000..4f98220 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/BuildComponent.cs @@ -0,0 +1,22 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class BuildComponent + { + public int Id { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Build Build { get; set; } = new(); + public virtual Component Component { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs new file mode 100644 index 0000000..dc10685 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Comment.cs @@ -0,0 +1,48 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Comment : ICommentModel + { + public int Id { get; set; } + [Required] + public DateTime Date { get; set; } + [Required] + public string Text { get; set; } = string.Empty; + [Required] + public int BuildId { get; set; } + public static Comment? Create(CommentBindingModel model) + { + if (model == null) + { + return null; + } + return new() + { + Id = model.Id, + Date = model.Date, + Text = model.Text, + BuildId = model.BuildId, + }; + } + + public void Update(CommentBindingModel model) + { + if (model == null) + { + return; + } + Text = model.Text; + } + + public CommentViewModel GetViewModel => new() + { + Id = Id, + Date = Date, + Text = Text, + }; + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs new file mode 100644 index 0000000..4acbece --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Component.cs @@ -0,0 +1,54 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + public double Cost { get; set; } + [Required] + public virtual StoreKeeper? StoreKeeperЗЛ { get; set; } + [NotMapped] + public IStoreKeeperModel? StoreKeeper { get; set; } + + [ForeignKey("ComponentId")] + public virtual List ProductComponents { get; set; } = new(); + public static Component? Create(ComponentBindingModel model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + Name = model.Name, + Cost = model.Cost, + StoreKeeper = model.StoreKeeper + }; + } + public void Update (ComponentBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Cost = model.Cost; + } + + public ComponentViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Cost = Cost + }; + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs new file mode 100644 index 0000000..bf50b41 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Order.cs @@ -0,0 +1,84 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Enums; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + [Required] + public int Count { get; set; } + [Required] + public double Sum { get; set; } + [Required] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + + private Dictionary? _orderProducts = null; + [NotMapped] + public Dictionary OrderProducts + { + get + { + if (_orderProducts == null) + { + _orderProducts = Products + .ToDictionary( + op => op.ProductId, + op => (op.Product as IProductModel, op.Count)); + } + return _orderProducts; + } + } + [ForeignKey("OrderId")] + public virtual List Products { get; set; } = new(); + public static Order? Create(ComputerHardwareStoreDBContext context, OrderBindingModel model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Products = context.Products + .Where(p => model.OrderProducts.ContainsKey(p.Id)) + .Select(p => new OrderProduct() + { + OrderId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.OrderProducts[p.Id].Item2 + }) + .ToList() + }; + } + public void Update(OrderBindingModel model) + { + if (model == null) + { + return; + } + Status = model.Status; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement, + }; + } +} \ No newline at end of file diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs new file mode 100644 index 0000000..9930fe7 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/OrderProduct.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class OrderProduct + { + public int Id { get; set; } + [Required] + public int OrderId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Product Product { get; set; } = new(); + public virtual Order Order { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs new file mode 100644 index 0000000..023758a --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Product.cs @@ -0,0 +1,98 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Product : IProductModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public double Price { get; set; } + private Dictionary? _productComponents = null; + [NotMapped] + public Dictionary ProductComponents + { + get + { + if (_productComponents == null) + { + _productComponents = Components + .ToDictionary(c => c.ComponentId, c => (c.Component as IComponentModel, c.Count)); + } + return _productComponents; + } + } + [ForeignKey("ProductId")] + public virtual List Components { get; set; } = new(); + [ForeignKey("ProductId")] + public virtual List Orders { get; set; } = new(); + public static Product Create(ComputerHardwareStoreDBContext context, ProductBindingModel model) + { + return new Product() + { + Id = model.Id, + Name = model.Name, + Price = model.Price, + Components = context.Components + .Where(c => model.ProductComponents.ContainsKey(c.Id)) + .Select(c => new ProductComponent() + { + ProductId = model.Id, + ComponentId = c.Id, + Component = c, + Count = model.ProductComponents[c.Id].Item2 + }).ToList(), + }; + } + public void Update(ProductBindingModel model) + { + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Price = model.Price; + + } + public ProductViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Price = Price, + ProductComponents = ProductComponents + }; + + public static void UpdateComponents(ComputerHardwareStoreDBContext context, ProductBindingModel model) + { + var productComponents = context.ProductComponents + .Where(pc => pc.ProductId == model.Id) + .ToList(); + if (productComponents.Count != 0 && productComponents.Count > 0) + { + // удалили те, которых нет в модели + context.ProductComponents + .Where(pc => !model.ProductComponents.ContainsKey(pc.ComponentId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productComponents + .ForEach(updateComponent => + { + updateComponent.Count = model.ProductComponents[updateComponent.ComponentId].Item2; + model.ProductComponents.Remove(updateComponent.ComponentId); + }); + } + // добавили новые + context.ProductComponents + .AddRange(model.ProductComponents.Values + .Select(val => new ProductComponent() + { + ProductId = model.Id, + ComponentId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs new file mode 100644 index 0000000..27d43d4 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/ProductComponent.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class ProductComponent + { + public int Id { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int ComponentId { get; set; } + [Required] + public int Count { get; set; } + public virtual Component Component { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs new file mode 100644 index 0000000..470cea3 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Purchase.cs @@ -0,0 +1,159 @@ + +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using Microsoft.EntityFrameworkCore; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Purchase : IPurchaseModel + { + public int Id { get; private set; } + [Required] + public int VendorId { get; set; } + [Required] + public double Cost { get; set; } + [Required] + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } = null; + private Dictionary? _purchaseBuilds = null; + [NotMapped] + public Dictionary PurchaseBuilds + { + get + { + if (_purchaseBuilds == null) + { + _purchaseBuilds = Builds + .ToDictionary(op => op.BuildId, op => (op.Build as IBuildModel, op.Count)); + } + return _purchaseBuilds; + } + } + + [ForeignKey("PurchaseId")] + public virtual List Builds { get; set; } = new(); + private Dictionary? _purchaseProducts = null; + [NotMapped] + public Dictionary PurchaseProducts + { + get + { + if (_purchaseProducts == null) + { + _purchaseProducts = Products + .ToDictionary(op => op.ProductId, op => (op.Product as IProductModel, op.Count)); + } + return _purchaseProducts; + } + } + [ForeignKey("PurchaseId")] + public virtual List Products { get; set; } = new(); + + public static Purchase Create(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + return new Purchase() + { + Id = model.Id, + Cost = model.Cost, + DateCreate = model.DateCreate, + Builds = context.Builds + .Where(b => model.PurchaseBuilds.ContainsKey(b.Id)) + .Select(b => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = b.Id, + Build = b, + Count = model.PurchaseProducts[b.Id].Item2 + }).ToList(), + Products = context.Products + .Where(p => model.PurchaseProducts.ContainsKey(p.Id)) + .Select (p => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = p.Id, + Product = p, + Count = model.PurchaseProducts[p.Id].Item2 + }) + .ToList() + }; + } + public void Update(PurchaseBindingModel model) + { + Cost = model.Cost; + + } + public PurchaseViewModel GetViewModel => new() + { + Id = Id, + Cost = Cost, + DateCreate = DateCreate, + PurchaseBuilds = PurchaseBuilds, + PurchaseProducts = PurchaseProducts + }; + + public static void UpdateBuilds(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productBuilds = context.PurchaseBuilds + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productBuilds.Count != 0 && productBuilds.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseBuilds + .Where(pc => !model.PurchaseBuilds.ContainsKey(pc.BuildId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productBuilds + .ForEach(updateBuild => + { + updateBuild.Count = model.PurchaseBuilds[updateBuild.BuildId].Item2; + model.PurchaseBuilds.Remove(updateBuild.BuildId); + }); + } + // добавили новые + context.PurchaseBuilds + .AddRange(model.PurchaseBuilds.Values + .Select(val => new PurchaseBuild() + { + PurchaseId = model.Id, + BuildId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + + public static void UpdateProducts(ComputerHardwareStoreDBContext context, PurchaseBindingModel model) + { + var productProducts = context.PurchaseProducts + .Where(pc => pc.PurchaseId == model.Id) + .ToList(); + if (productProducts.Count != 0 && productProducts.Count > 0) + { + // удалили те, которых нет в модели + context.PurchaseProducts + .Where(pc => !model.PurchaseProducts.ContainsKey(pc.ProductId)) + .ExecuteDelete(); + // обновили количество у существующих записей + productProducts + .ForEach(updateProduct => + { + updateProduct.Count = model.PurchaseProducts[updateProduct.ProductId].Item2; + model.PurchaseProducts.Remove(updateProduct.ProductId); + }); + } + // добавили новые + context.PurchaseProducts + .AddRange(model.PurchaseProducts.Values + .Select(val => new PurchaseProduct() + { + PurchaseId = model.Id, + ProductId = val.Item1.Id, + Count = val.Item2 + })); + context.SaveChanges(); + } + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs new file mode 100644 index 0000000..2f5c1af --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseBuild.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseBuild + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int BuildId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Build Build { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs new file mode 100644 index 0000000..2bc9116 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/PurchaseProduct.cs @@ -0,0 +1,18 @@ + +using System.ComponentModel.DataAnnotations; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class PurchaseProduct + { + public int Id { get; set; } + [Required] + public int PurchaseId { get; set; } + [Required] + public int ProductId { get; set; } + [Required] + public int Count { get; set; } + public virtual Purchase Purchase { get; set; } = new(); + public virtual Product Product { get; set; } = new(); + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs new file mode 100644 index 0000000..1fd691b --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/StoreKeeper.cs @@ -0,0 +1,56 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class StoreKeeper : IStoreKeeperModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + public string Password { get; private set; } = string.Empty; + [ForeignKey("StoreKeeperId")] + public virtual List Products { get; set; } = new(); + [ForeignKey("StoreKeeperId")] + public virtual List Components { get; set; } = new(); + + public static StoreKeeper? Create(ComputerHardwareStoreDBContext context, StoreKeeperBindingModel model) + { + if (model == null) + { + return null; + } + return new StoreKeeper() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(StoreKeeperBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public StoreKeeperViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +} diff --git a/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs new file mode 100644 index 0000000..4465a20 --- /dev/null +++ b/ComputerHardwareStore/ComputerHardwareStoreDatabaseImplement/Models/Vendor.cs @@ -0,0 +1,57 @@ +using ComputerHardwareStoreContracts.BindingModels; +using ComputerHardwareStoreContracts.ViewModels; +using ComputerHardwareStoreDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; + +namespace ComputerHardwareStoreDatabaseImplement.Models +{ + public class Vendor : IVendorModel + { + public int Id { get; private set; } + [Required] + public string Name { get; private set; } = string.Empty; + [Required] + + public string Login { get; private set; } = string.Empty; + [Required] + + public string Password { get; private set; } = string.Empty; + [ForeignKey("VendorId")] + public virtual List Purchases { get; set; } = new(); + [ForeignKey("VendorId")] + public virtual List Builds { get; set; } = new(); + + public static Vendor? Create(ComputerHardwareStoreDBContext context, VendorBindingModel model) + { + if (model == null) + { + return null; + } + return new Vendor() + { + Id = model.Id, + Name = model.Name, + Login = model.Login, + Password = model.Password, + }; + } + public void Update(VendorBindingModel model) + { + if (model == null) + { + return; + } + Name = string.IsNullOrEmpty(model.Name) ? Name : model.Name; + Password = string.IsNullOrEmpty(model.Password) ? Password : model.Password; + } + + public VendorViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Login = Login, + Password = Password + }; + } +}