diff --git a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs index 9de3c29..d8663ec 100644 --- a/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs +++ b/SecuritySystem/SecuritySystemBusinessLogic/BusinessLogics/OrderLogic.cs @@ -13,12 +13,14 @@ namespace SecuritySystemBusinessLogic.BusinessLogics private readonly ILogger _logger; private readonly IOrderStorage _orderStorage; private readonly IShopLogic _shopLogic; + private readonly ISecureStorage _secureStorage; - public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic) + public OrderLogic(ILogger logger, IOrderStorage orderStorage, IShopLogic shopLogic, ISecureStorage secureStorage) { _logger = logger; _orderStorage = orderStorage; _shopLogic = shopLogic; + _secureStorage = secureStorage; } public List? ReadList(OrderSearchModel? model) @@ -61,9 +63,10 @@ namespace SecuritySystemBusinessLogic.BusinessLogics _logger.LogWarning("Status change operation failed"); throw new InvalidOperationException("Текущий статус заказа не может быть переведен в выбранный"); } + var secure = _secureStorage.GetElement(new SecureSearchModel { Id = model.SecureId }); if (targetStatus == OrderStatus.Выдан) { - _shopLogic.SupplySecures(); + _shopLogic.SupplySecures(secure, model.Count); } model.Status = targetStatus; if (model.Status == OrderStatus.Выдан) diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..b10383f --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Implements/ShopStorage.cs @@ -0,0 +1,40 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.SearchModels; +using SecuritySystemContracts.StoragesContracts; +using SecuritySystemContracts.ViewModels; + +namespace SecuritySystemDatabaseImplement.Implements +{ + public class ShopStorage : IShopStorage + { + public ShopViewModel? Delete(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFilteredList(ShopSearchModel model) + { + throw new NotImplementedException(); + } + + public List GetFullList() + { + throw new NotImplementedException(); + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + throw new NotImplementedException(); + } + + public ShopViewModel? Update(ShopBindingModel model) + { + throw new NotImplementedException(); + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs new file mode 100644 index 0000000..3b2e23b --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/Shop.cs @@ -0,0 +1,99 @@ +using SecuritySystemContracts.BindingModels; +using SecuritySystemContracts.ViewModels; +using SecuritySystemDataModels.Models; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel.DataAnnotations.Schema; +using System.Diagnostics; + +namespace SecuritySystemDatabaseImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; set; } + [Required] + public string Name { get; set; } = string.Empty; + [Required] + public string Address { get; set; } = string.Empty; + [Required] + public DateTime OpeningDate { get; set; } + [Required] + public int MaxSecuresCount { get; set; } + [ForeignKey("ShopId")] + public virtual List Secures { get; set; } = new(); + private Dictionary? _shopSecures = null; + public Dictionary ShopSecures + { + get + { + if (_shopSecures == null) + { + _shopSecures = Secures + .ToDictionary(shopSecure => shopSecure.SecureId, shopSecure => (shopSecure.Secure as ISecureModel, shopSecure.Count)); + } + return _shopSecures; + } + } + public static Shop Create(SecuritySystemDatabase context, ShopBindingModel model) + { + return new Shop() + { + Id = model.Id, + Name = model.Name, + Address = model.Address, + OpeningDate = model.OpeningDate, + MaxSecuresCount = model.MaxSecuresCount, + Secures = model.ShopSecures.Select(x => new ShopSecure + { + Secure = context.Secures.First(y => y.Id == x.Key), + Count = x.Value.Item2 + }).ToList() + }; + } + public void Update(ShopBindingModel model) + { + Name = model.Name; + Address = model.Address; + OpeningDate = model.OpeningDate; + MaxSecuresCount = model.MaxSecuresCount; + } + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Address = Address, + OpeningDate = OpeningDate, + MaxSecuresCount = MaxSecuresCount, + ShopSecures = ShopSecures + + }; + public void UpdateComponents(SecuritySystemDatabase context, ShopBindingModel model) + { + var shopSecures = context.ShopSecures.Where(rec => rec.SecureId == model.Id).ToList(); + if (shopSecures != null && shopSecures.Count > 0) + { + // удалили те, которых нет в модели + context.ShopSecures.RemoveRange(shopSecures.Where(rec => !model.ShopSecures.ContainsKey(rec.SecureId))); + context.SaveChanges(); + // обновили количество у существующих записей + foreach (var updateSecure in shopSecures) + { + updateSecure.Count = model.ShopSecures[updateSecure.SecureId].Item2; + model.ShopSecures.Remove(updateSecure.SecureId); + } + context.SaveChanges(); + } + var shop = context.Shops.First(x => x.Id == Id); + foreach (var obj in model.ShopSecures) + { + context.ShopSecures.Add(new ShopSecure + { + Shop = shop, + Secure = context.Secures.First(x => x.Id == obj.Key), + Count = obj.Value.Item2 + }); + context.SaveChanges(); + } + _shopSecures = null; + } + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/Models/ShopSecure.cs b/SecuritySystem/SecuritySystemDatabaseImplement/Models/ShopSecure.cs new file mode 100644 index 0000000..307d068 --- /dev/null +++ b/SecuritySystem/SecuritySystemDatabaseImplement/Models/ShopSecure.cs @@ -0,0 +1,17 @@ +using System.ComponentModel.DataAnnotations; + +namespace SecuritySystemDatabaseImplement.Models +{ + public class ShopSecure + { + public int Id { get; set; } + [Required] + public int SecureId { get; set; } + [Required] + public int ShopId { get; set; } + [Required] + public int Count { get; set; } + public virtual Shop Shop { get; set; } = new(); + public virtual Secure Secure { get; set; } = new(); + } +} diff --git a/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs index e4f3268..3d6b9cd 100644 --- a/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs +++ b/SecuritySystem/SecuritySystemDatabaseImplement/SecuritySystemDatabase.cs @@ -12,7 +12,7 @@ namespace SecuritySystemDatabaseImplement { if (optionsBuilder.IsConfigured == false) { - optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SecuritySystemDatabase;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); + optionsBuilder.UseSqlServer(@"Data Source=localhost\SQLEXPRESS;Initial Catalog=SecuritySystemDatabaseHard;Integrated Security=True;MultipleActiveResultSets=True;;TrustServerCertificate=True"); } base.OnConfiguring(optionsBuilder); } @@ -20,6 +20,8 @@ namespace SecuritySystemDatabaseImplement public virtual DbSet Secures { set; get; } public virtual DbSet SecureComponents { set; get; } public virtual DbSet Orders { set; get; } + public virtual DbSet ShopSecures { get; set; } + public virtual DbSet Shops { get; set; } } }