diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IBuildLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IBuildLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IBuildLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IBuildLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/ICommentLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/ICommentLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/ICommentLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/ICommentLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IComponentLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IComponentLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IComponentLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IComponentLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IGoodLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IGoodLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IGoodLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IGoodLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IOrderLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IOrderLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IOrderLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IOrderLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IPurchaseLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IPurchaseLogic.cs diff --git a/HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IUserLogic.cs b/HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IUserLogic.cs similarity index 100% rename from HardwareShop/HardwareShopContracts/BuisnessLogicsContracts/IUserLogic.cs rename to HardwareShop/HardwareShopContracts/BusinessLogicsContracts/IUserLogic.cs diff --git a/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs b/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs index 8733ba5..12b7779 100644 --- a/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs +++ b/HardwareShop/HardwareShopContracts/SearchModels/UserSearchModel.cs @@ -1,5 +1,4 @@ - -using System.ComponentModel; +using HardwareShopDataModels.Enums; namespace HardwareShopContracts.SearchModels { @@ -10,5 +9,9 @@ namespace HardwareShopContracts.SearchModels public string? Login { get; set; } = string.Empty; public string? Email { get; set; } = string.Empty; + + public string? Password { get; set; } = string.Empty; + + public UserRole? Role { get; set; } = UserRole.Неизвестен; } } \ No newline at end of file diff --git a/HardwareShop/HardwareShopDataModels/Models/IUserModel.cs b/HardwareShop/HardwareShopDataModels/Models/IUserModel.cs index 1afe0f4..6d75279 100644 --- a/HardwareShop/HardwareShopDataModels/Models/IUserModel.cs +++ b/HardwareShop/HardwareShopDataModels/Models/IUserModel.cs @@ -1,5 +1,4 @@ -using HardwareShopDataModels; -using HardwareShopDataModels.Enums; +using HardwareShopDataModels.Enums; namespace HardwareShopDataModels.Models { diff --git a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs index bd32b1e..a96fcc7 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabase.cs @@ -12,7 +12,7 @@ namespace HardwareShopDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseNpgsql("Host=192.168.0.101;Port=5432;Database=Computer_Hardware_Store;Username=user;Password=12345"); + optionsBuilder.UseNpgsql("Host=127.0.0.1;Port=5433;Database=Computer_Hardware_Store;Username=user;Password=12345"); } base.OnConfiguring(optionsBuilder); } @@ -23,6 +23,11 @@ namespace HardwareShopDatabaseImplement modelBuilder.Entity().HasKey(x => new { x.PurchaseId, x.BuildId }); modelBuilder.Entity().HasKey(x => new { x.PurchaseId, x.GoodId }); modelBuilder.Entity().HasKey(x => new { x.GoodId, x.ComponentId }); + + modelBuilder.Entity(entity => { + entity.HasIndex(e => e.Login).IsUnique(); + entity.HasIndex(e => e.Email).IsUnique(); + }); } public virtual DbSet Builds { set; get; } diff --git a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabaseImplement.csproj b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabaseImplement.csproj index a308b73..36c4945 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabaseImplement.csproj +++ b/HardwareShop/HardwareShopDatabaseImplement/HardwareShopDatabaseImplement.csproj @@ -6,12 +6,6 @@ enable - - - - - - diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs new file mode 100644 index 0000000..c101317 --- /dev/null +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/ComponentStorage.cs @@ -0,0 +1,87 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.Storekeeper; +using Microsoft.EntityFrameworkCore; + +namespace HardwareShopDatabaseImplement.Implements.Storekeeper +{ + public class ComponentStorage : IComponentStorage + { + public ComponentViewModel? Delete(ComponentBindingModel model) + { + using var context = new HardwareShopDatabase(); + var element = context.Components.Include(x => x.User).FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Components.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + using var context = new HardwareShopDatabase(); + return context.Components.Include(x => x.User) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.ComponentName) && x.ComponentName == model.ComponentName) || + (model.Id.HasValue && x.Id == model.Id))?.GetViewModel; + } + + public List GetFilteredList(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName)) + { + return new(); + } + using var context = new HardwareShopDatabase(); + return context.Components + .Include(x => x.User) + .Where(x => x.ComponentName.Contains(model.ComponentName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HardwareShopDatabase(); + return context.Components + .Include(x => x.User) + .Select(x => x.GetViewModel) + .ToList(); + } + + public ComponentViewModel? Insert(ComponentBindingModel model) + { + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + using var context = new HardwareShopDatabase(); + context.Components.Add(newComponent); + context.SaveChanges(); + return context.Components + .Include(x => x.User).FirstOrDefault(x => x.Id == newComponent.Id)?.GetViewModel; + } + + public ComponentViewModel? Update(ComponentBindingModel model) + { + using var context = new HardwareShopDatabase(); + var component = context.Components.Include(x => x.User).FirstOrDefault(x => x.Id == model.Id); + if (component == null) + { + return null; + } + component.Update(model); + context.SaveChanges(); + return component.GetViewModel; + } + } +} diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/GoodStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/GoodStorage.cs new file mode 100644 index 0000000..3183949 --- /dev/null +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/GoodStorage.cs @@ -0,0 +1,117 @@ +using HardwareShopContracts.BindingModels; +using HardwareShopContracts.SearchModels; +using HardwareShopContracts.StoragesContracts; +using HardwareShopContracts.ViewModels; +using HardwareShopDatabaseImplement.Models.Storekeeper; +using Microsoft.EntityFrameworkCore; + +namespace HardwareShopDatabaseImplement.Implements.Storekeeper +{ + public class GoodStorage : IGoodStorage + { + public GoodViewModel? Delete(GoodBindingModel model) + { + using var context = new HardwareShopDatabase(); + var element = context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(rec => rec.Id == model.Id); + if (element != null) + { + context.Goods.Remove(element); + context.SaveChanges(); + return element.GetViewModel; + } + return null; + } + + public GoodViewModel? GetElement(GoodSearchModel model) + { + if (string.IsNullOrEmpty(model.GoodName) && !model.Id.HasValue) + { + return null; + } + using var context = new HardwareShopDatabase(); + return context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => (!string.IsNullOrEmpty(model.GoodName) && x.GoodName == model.GoodName) || + (model.Id.HasValue && x.Id == model.Id)) + ?.GetViewModel; + } + + public List GetFilteredList(GoodSearchModel model) + { + if (string.IsNullOrEmpty(model.GoodName)) + { + return new(); + } + using var context = new HardwareShopDatabase(); + return context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Where(x => x.GoodName.Contains(model.GoodName)) + .Select(x => x.GetViewModel) + .ToList(); + } + + public List GetFullList() + { + using var context = new HardwareShopDatabase(); + return context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .Select(x => x.GetViewModel) + .ToList(); + } + + public GoodViewModel? Insert(GoodBindingModel model) + { + using var context = new HardwareShopDatabase(); + var newGood = Good.Create(context, model); + if (newGood == null) + { + return null; + } + context.Goods.Add(newGood); + context.SaveChanges(); + return context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(x => x.Id == newGood.Id)?.GetViewModel; + } + + public GoodViewModel? Update(GoodBindingModel model) + { + using var context = new HardwareShopDatabase(); + using var transaction = context.Database.BeginTransaction(); + try + { + var good = context.Goods + .Include(x => x.User) + .Include(x => x.Components) + .ThenInclude(x => x.Component) + .FirstOrDefault(rec => rec.Id == model.Id); + if (good == null) + { + return null; + } + good.Update(model); + context.SaveChanges(); + good.UpdateComponents(context, model); + transaction.Commit(); + return good.GetViewModel; + } + catch + { + transaction.Rollback(); + throw; + } + } + } +} diff --git a/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/OrderStorage.cs b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/OrderStorage.cs new file mode 100644 index 0000000..35b10c8 --- /dev/null +++ b/HardwareShop/HardwareShopDatabaseImplement/Implements/Storekeeper/OrderStorage.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HardwareShopDatabaseImplement.Implements.Storekeeper +{ + public class OrderStorage + { + } +} diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/BuildComponent.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/BuildComponent.cs index 371fc69..b85961e 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/BuildComponent.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/BuildComponent.cs @@ -1,12 +1,6 @@ using HardwareShopDatabaseImplement.Models.Storekeeper; using HardwareShopDatabaseImplement.Models.Worker; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace HardwareShopDatabaseImplement.Models.ManyToMany { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs index f51783b..fbc8f07 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/GoodComponent.cs @@ -1,6 +1,5 @@ using HardwareShopDatabaseImplement.Models.Storekeeper; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; namespace HardwareShopDatabaseImplement.Models.ManyToMany { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseBuild.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseBuild.cs index 248b4be..1eb146f 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseBuild.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseBuild.cs @@ -1,11 +1,5 @@ using HardwareShopDatabaseImplement.Models.Worker; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace HardwareShopDatabaseImplement.Models.ManyToMany { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseGood.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseGood.cs index dd11b99..a9b0716 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseGood.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/ManyToMany/PurchaseGood.cs @@ -1,12 +1,6 @@ using HardwareShopDatabaseImplement.Models.Storekeeper; using HardwareShopDatabaseImplement.Models.Worker; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace HardwareShopDatabaseImplement.Models.ManyToMany { diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs index 98a4ab6..210e5eb 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Component.cs @@ -21,7 +21,10 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper public int UserId { get; set; } [ForeignKey("ComponentId")] - public virtual List GoodComponents { get; set; } = new(); + public virtual List Goods { get; set; } = new(); + + [ForeignKey("ComponentId")] + public virtual List Builds { get; set; } = new(); public virtual User User { get; set; } = null!; diff --git a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs index e4679d0..2da3ab1 100644 --- a/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs +++ b/HardwareShop/HardwareShopDatabaseImplement/Models/Storekeeper/Good.cs @@ -30,6 +30,9 @@ namespace HardwareShopDatabaseImplement.Models.Storekeeper [ForeignKey("GoodId")] public virtual List Orders { get; set; } = new(); + [ForeignKey("GoodId")] + public virtual List Purchases { get; set; } = new(); + [NotMapped] public Dictionary GoodComponents {