diff --git a/JewelryStoreBusinessLogic/BusinessLogics/StoreLogic .cs b/JewelryStoreBusinessLogic/BusinessLogics/StoreLogic .cs new file mode 100644 index 0000000..52e2036 --- /dev/null +++ b/JewelryStoreBusinessLogic/BusinessLogics/StoreLogic .cs @@ -0,0 +1,181 @@ +using Microsoft.Extensions.Logging; +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.BusinessLogicsContracts; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreDataModels.Models; +using JewelryStoreContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreBusinessLogic.BusinessLogics +{ + internal class StoreLogic : IStoreLogic + { + private readonly ILogger _logger; + private readonly IStoreStorage _storeStorage; + public StoreLogic(ILogger logger, IStoreStorage storeStorage) + { + _logger = logger; + _storeStorage = storeStorage; + } + public bool AddJewel(StoreSearchModel model, IJewelModel jewel, int quantity) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (quantity <= 0) + { + throw new ArgumentException("Количество добавляемого изделия должно быть больше 0", nameof(quantity)); + } + + _logger.LogInformation("AddJewelInStore. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id); + var element = _storeStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("AddJewelInStore element not found"); + return false; + } + + _logger.LogInformation("AddJewelInStore find. Id:{Id}", element.Id); + + if (element.Jewels.TryGetValue(jewel.Id, out var pair)) + { + element.Jewels[jewel.Id] = (jewel, quantity + pair.Item2); + _logger.LogInformation("AddJewelInStore. Has been added {quantity} {jewel} in {StoreName}", quantity, jewel.JewelName, element.StoreName); + } + else + { + element.Jewels[jewel.Id] = (jewel, quantity); + _logger.LogInformation("AddPastryInShop. Has been added {quantity} new Jewel {jewel} in {StoreName}", quantity, jewel.JewelName, element.StoreName); + } + + _storeStorage.Update(new() + { + Id = element.Id, + StoreAdress = element.StoreAdress, + StoreName = element.StoreName, + OpeningDate = element.OpeningDate, + Jewels = element.Jewels + }); + return true; + } + + public bool Create(StoreBindingModel model) + { + CheckModel(model); + model.Jewels = new(); + + if (_storeStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + + return true; + } + + public bool Delete(StoreBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + + if (_storeStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + + return true; + } + + public StoreViewModel? ReadElement(StoreSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + _logger.LogInformation("ReadElement. StoreName:{StoreName}.Id:{ Id}", model.StoreName, model.Id); + var element = _storeStorage.GetElement(model); + + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public List? ReadList(StoreSearchModel? model) + { + _logger.LogInformation("ReadList. StoreName:{StoreName}.Id:{ Id} ", model?.StoreName, model?.Id); + + var list = (model == null) ? _storeStorage.GetFullList() : _storeStorage.GetFilteredList(model); + + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool Update(StoreBindingModel model) + { + CheckModel(model, false); + + if (string.IsNullOrEmpty(model.StoreName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.StoreName)); + } + + if (_storeStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + private void CheckModel(StoreBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + + if (!withParams) + { + return; + } + + if (string.IsNullOrEmpty(model.StoreName)) + { + throw new ArgumentNullException("Нет названия магазина", nameof(model.StoreName)); + } + + _logger.LogInformation("Store. StoreName:{0}.StoreAdress:{1}. Id: {2}", model.StoreName, model.StoreAdress, model.Id); + var element = _storeStorage.GetElement(new StoreSearchModel + { + StoreName = model.StoreName + }); + + if (element != null && element.Id != model.Id && element.StoreName == model.StoreName) + { + throw new InvalidOperationException("Магазин с таким названием уже есть"); + } + } + + } +} diff --git a/JewelryStoreContracts/BindingModels/StoreBindingModel.cs b/JewelryStoreContracts/BindingModels/StoreBindingModel.cs new file mode 100644 index 0000000..9198ea6 --- /dev/null +++ b/JewelryStoreContracts/BindingModels/StoreBindingModel.cs @@ -0,0 +1,21 @@ +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.BindingModels +{ + public class StoreBindingModel: IStoreModel + { + public string StoreName { get; set; } = string.Empty; + public string StoreAdress { get; set; } = string.Empty; + + public DateTime OpeningDate { get; set; } = DateTime.Now; + + public Dictionary Jewels { get; set; } = new(); + + public int Id { get; set; } + } +} diff --git a/JewelryStoreContracts/BusinessLogicsContracts/IStoreLogic.cs b/JewelryStoreContracts/BusinessLogicsContracts/IStoreLogic.cs new file mode 100644 index 0000000..4bf2807 --- /dev/null +++ b/JewelryStoreContracts/BusinessLogicsContracts/IStoreLogic.cs @@ -0,0 +1,22 @@ +using JewelryStoreContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.ViewModels; +using JewelryStoreDataModels.Models; + +namespace JewelryStoreContracts.BusinessLogicsContracts +{ + public interface IStoreLogic + { + List? ReadList(StoreSearchModel? model); + StoreViewModel? ReadElement(StoreSearchModel model); + bool Create(StoreBindingModel model); + bool Update(StoreBindingModel model); + bool Delete(StoreBindingModel model); + bool AddJewel(StoreSearchModel model, IJewelModel jewel, int quantity); + } +} diff --git a/JewelryStoreContracts/SearchModels/StoreSearchModel.cs b/JewelryStoreContracts/SearchModels/StoreSearchModel.cs new file mode 100644 index 0000000..1ffd758 --- /dev/null +++ b/JewelryStoreContracts/SearchModels/StoreSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.SearchModels +{ + public class StoreSearchModel + { + public int? Id { get; set; } + public string? StoreName { get; set; } + } +} diff --git a/JewelryStoreContracts/StoragesContracts/IStoreStorage.cs b/JewelryStoreContracts/StoragesContracts/IStoreStorage.cs new file mode 100644 index 0000000..ded1a42 --- /dev/null +++ b/JewelryStoreContracts/StoragesContracts/IStoreStorage.cs @@ -0,0 +1,21 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.StoragesContracts +{ + public interface IStoreStorage + { + List GetFullList(); + List GetFilteredList(StoreSearchModel model); + StoreViewModel? GetElement(StoreSearchModel model); + StoreViewModel? Insert(StoreBindingModel model); + StoreViewModel? Update(StoreBindingModel model); + StoreViewModel? Delete(StoreBindingModel model); + } +} diff --git a/JewelryStoreContracts/ViewModels/StoreViewModel.cs b/JewelryStoreContracts/ViewModels/StoreViewModel.cs new file mode 100644 index 0000000..3487455 --- /dev/null +++ b/JewelryStoreContracts/ViewModels/StoreViewModel.cs @@ -0,0 +1,23 @@ +using JewelryStoreDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreContracts.ViewModels +{ + public class StoreViewModel : IStoreModel + { + public Dictionary Jewels { get; set; } = new(); + public int Id { get; set; } + + [DisplayName("Название магазина")] + public string StoreName { get; set; } = string.Empty; + [DisplayName("Адрес магазина")] + public string StoreAdress { get; set; } = string.Empty; + [DisplayName("Дата открытия")] + public DateTime OpeningDate { get; set; } = DateTime.Now; + } +} diff --git a/JewelryStoreDataModels/Models/IStoreModel.cs b/JewelryStoreDataModels/Models/IStoreModel.cs new file mode 100644 index 0000000..b4c8b1b --- /dev/null +++ b/JewelryStoreDataModels/Models/IStoreModel.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreDataModels.Models +{ + public interface IStoreModel : IId + { + public string StoreName { get; } + public string StoreAdress { get; } + DateTime OpeningDate { get; } + Dictionary Jewels { get; } + } +} diff --git a/JewelryStoreListImplement/DataListSingleton.cs b/JewelryStoreListImplement/DataListSingleton.cs index 1b280c4..49ea268 100644 --- a/JewelryStoreListImplement/DataListSingleton.cs +++ b/JewelryStoreListImplement/DataListSingleton.cs @@ -13,11 +13,13 @@ namespace JewelryStoreListImplement public List Components { get; set; } public List Orders { get; set; } public List Jewels { get; set; } + public List Stores { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Jewels = new List(); + Stores = new List(); } public static DataListSingleton GetInstance() { diff --git a/JewelryStoreListImplement/Implements/StoreStorage.cs b/JewelryStoreListImplement/Implements/StoreStorage.cs new file mode 100644 index 0000000..023c1ff --- /dev/null +++ b/JewelryStoreListImplement/Implements/StoreStorage.cs @@ -0,0 +1,123 @@ +using JewelryStoreContracts.BindingModels; +using JewelryStoreContracts.SearchModels; +using JewelryStoreContracts.StoragesContracts; +using JewelryStoreContracts.ViewModels; +using JewelryStoreListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreListImplement.Implements +{ + public class StoreStorage: IStoreStorage + { + private readonly DataListSingleton _source; + public StoreStorage() + { + _source = DataListSingleton.GetInstance(); + } + public StoreViewModel? Delete(StoreBindingModel model) + { + for (int i = 0; i < _source.Stores.Count; ++i) + { + if (_source.Stores[i].Id == model.Id) + { + var element = _source.Stores[i]; + _source.Stores.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + public StoreViewModel? GetElement(StoreSearchModel model) + { + if (string.IsNullOrEmpty(model.StoreName) && !model.Id.HasValue) + { + return null; + } + + foreach (var store in _source.Stores) + { + if ((!string.IsNullOrEmpty(model.StoreName) && store.StoreName == model.StoreName) || (model.Id.HasValue && store.Id == model.Id)) + { + return store.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(StoreSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.StoreName)) + { + return result; + } + + foreach (var store in _source.Stores) + { + if (store.StoreName.Contains(model.StoreName)) + { + result.Add(store.GetViewModel); + } + } + + return result; + } + + public List GetFullList() + { + var result = new List(); + + foreach (var store in _source.Stores) + { + result.Add(store.GetViewModel); + } + + return result; + } + + public StoreViewModel? Insert(StoreBindingModel model) + { + model.Id = 1; + + foreach (var store in _source.Stores) + { + if (model.Id <= store.Id) + { + model.Id = store.Id + 1; + } + } + + var newStore = Store.Create(model); + + if (newStore == null) + { + return null; + } + + _source.Stores.Add(newStore); + + return newStore.GetViewModel; + } + + public StoreViewModel? Update(StoreBindingModel model) + { + foreach (var store in _source.Stores) + { + if (store.Id == model.Id) + { + store.Update(model); + return store.GetViewModel; + } + } + + return null; + } + } +} diff --git a/JewelryStoreListImplement/Models/Store.cs b/JewelryStoreListImplement/Models/Store.cs new file mode 100644 index 0000000..e1b80f3 --- /dev/null +++ b/JewelryStoreListImplement/Models/Store.cs @@ -0,0 +1,60 @@ +using JewelryStoreDataModels.Models; +using JewelryStoreContracts.ViewModels; +using JewelryStoreContracts.BindingModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace JewelryStoreListImplement.Models +{ + public class Store : IStoreModel + { + public string StoreName { get; private set; } = string.Empty; + public string StoreAdress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + public Dictionary Jewels { get; private set; } = new(); + + public int Id { get; private set; } + + public static Store? Create(StoreBindingModel? model) + { + if (model == null) + { + return null; + } + return new Store() + { + Id = model.Id, + StoreName = model.StoreName, + StoreAdress = model.StoreAdress, + OpeningDate = model.OpeningDate, + Jewels = new() + }; + } + + public void Update(StoreBindingModel? model) + { + if (model == null) + { + return; + } + StoreName = model.StoreName; + StoreAdress = model.StoreAdress; + OpeningDate = model.OpeningDate; + Jewels = model.Jewels; + } + + public StoreViewModel GetViewModel => new() + { + Id = Id, + StoreName = StoreName, + StoreAdress = StoreAdress, + OpeningDate = OpeningDate, + Jewels = Jewels + }; + } +}