diff --git a/LawFirm/LawFirmListImplements/DataListSingleton.cs b/LawFirm/LawFirmListImplements/DataListSingleton.cs index dbbf993..8020567 100644 --- a/LawFirm/LawFirmListImplements/DataListSingleton.cs +++ b/LawFirm/LawFirmListImplements/DataListSingleton.cs @@ -14,11 +14,13 @@ namespace LawFirmListImplements public List Blanks { get; set; } public List Orders { get; set; } public List Documents { get; set; } + public List Shops { get; set; } private DataListSingleton() { Blanks = new List(); Orders = new List(); Documents = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() { diff --git a/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs b/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs new file mode 100644 index 0000000..11f9ac9 --- /dev/null +++ b/LawFirm/LawFirmListImplements/Implements/ShopStorage.cs @@ -0,0 +1,120 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.SearchModels; +using LawFirmContracts.StorageContracts; +using LawFirmContracts.ViewModels; +using LawFirmListImplements.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using static System.Formats.Asn1.AsnWriter; + +namespace LawFirmListImplements.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public ShopViewModel? GetElement(ShopSearchModel model) + { + if (string.IsNullOrEmpty(model.Name) && !model.Id.HasValue) + { + return null; + } + foreach (var shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(model.Name) && shop.Name == model.Name) || (model.Id.HasValue && shop.Id == model.Id)) + { + return shop.GetViewModel; + } + } + + return null; + } + + public List GetFilteredList(ShopSearchModel model) + { + var result = new List(); + + if (string.IsNullOrEmpty(model.Name)) + { + return result; + } + foreach (var shop in _source.Shops) + { + if (shop.Name.Contains(model.Name)) + { + result.Add(shop.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var shop in _source.Shops) + { + result.Add(shop.GetViewModel); + } + return result; + } + + public ShopViewModel? Insert(ShopBindingModel model) + { + model.Id = 1; + + foreach (var shop in _source.Shops) + { + if (model.Id <= shop.Id) + { + model.Id = shop.Id + 1; + } + } + + var newShop = Shop.Create(model); + + if (newShop == null) + { + return null; + } + _source.Shops.Add(newShop); + + return newShop.GetViewModel; + } + + public ShopViewModel? Update(ShopBindingModel model) + { + foreach (var shop in _source.Shops) + { + if (shop.Id == model.Id) + { + shop.Update(model); + return shop.GetViewModel; + } + } + + return null; + } + public ShopViewModel? Delete(ShopBindingModel model) + { + for (int i = 0; i < _source.Shops.Count; ++i) + { + if (_source.Shops[i].Id == model.Id) + { + var element = _source.Shops[i]; + _source.Shops.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + + } +} diff --git a/LawFirm/LawFirmListImplements/Models/Shop.cs b/LawFirm/LawFirmListImplements/Models/Shop.cs new file mode 100644 index 0000000..82805db --- /dev/null +++ b/LawFirm/LawFirmListImplements/Models/Shop.cs @@ -0,0 +1,63 @@ +using LawFirmContracts.BindingModels; +using LawFirmContracts.ViewModels; +using LawFirmDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using System.Threading.Tasks; +using static System.Formats.Asn1.AsnWriter; + +namespace LawFirmListImplements.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string Name { get; private set; } = string.Empty; + + public string Adress { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + public Dictionary ShopDocuments { get; private set; } = new(); + + public static Shop? Create (ShopBindingModel model) + { + if (model == null) + { + return null; + } + return new Shop() + { + Id = model.Id, + Name = model.Name, + Adress = model.Adress, + OpeningDate = model.OpeningDate, + ShopDocuments = new() + }; + } + + public void Update(ShopBindingModel? model) + { + if (model == null) + { + return; + } + Name = model.Name; + Adress = model.Adress; + OpeningDate = model.OpeningDate; + ShopDocuments = model.ShopDocuments; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + Name = Name, + Adress = Adress, + OpeningDate = OpeningDate, + ShopDocuments = ShopDocuments + }; + } +}