diff --git a/AutoWorkshopImplement/DataListSingleton.cs b/AutoWorkshopImplement/DataListSingleton.cs index 2bec800..c210d2a 100644 --- a/AutoWorkshopImplement/DataListSingleton.cs +++ b/AutoWorkshopImplement/DataListSingleton.cs @@ -11,12 +11,15 @@ namespace AutoWorkshopListImplement public List Orders { get; set; } public List Repairs { get; set; } + + public List Shops { get; set; } private DataListSingleton() { Components = new List(); Orders = new List(); Repairs = new List(); + Shops = new List(); } public static DataListSingleton GetInstance() diff --git a/AutoWorkshopImplement/Implements/ShopStorage.cs b/AutoWorkshopImplement/Implements/ShopStorage.cs new file mode 100644 index 0000000..8e6d8e7 --- /dev/null +++ b/AutoWorkshopImplement/Implements/ShopStorage.cs @@ -0,0 +1,112 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.SearchModels; +using AutoWorkshopContracts.StoragesContracts; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopListImplement.Models; + +namespace AutoWorkshopListImplement.Implements +{ + public class ShopStorage : IShopStorage + { + private readonly DataListSingleton _source; + + public ShopStorage() + { + _source = DataListSingleton.GetInstance(); + } + + public List GetFullList() + { + var Result = new List(); + + foreach (var Shop in _source.Shops) + { + Result.Add(Shop.GetViewModel); + } + + return Result; + } + + public List GetFilteredList(ShopSearchModel Model) + { + var Result = new List(); + + if (string.IsNullOrEmpty(Model.ShopName)) + return Result; + + foreach (var Shop in _source.Shops) + { + if (Shop.ShopName.Contains(Model.ShopName)) + Result.Add(Shop.GetViewModel); + } + + return Result; + } + + public ShopViewModel? GetElement(ShopSearchModel Model) + { + if (string.IsNullOrEmpty(Model.ShopName) && !Model.Id.HasValue) + return null; + + foreach (var Shop in _source.Shops) + { + if ((!string.IsNullOrEmpty(Model.ShopName) && Shop.ShopName == Model.ShopName) || + (Model.Id.HasValue && Shop.Id == Model.Id)) + { + return Shop.GetViewModel; + } + } + + return null; + } + + 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 is 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 Shop = _source.Shops[i]; + _source.Shops.RemoveAt(i); + + return Shop.GetViewModel; + } + } + + return null; + } + } +} diff --git a/AutoWorkshopImplement/Models/Shop.cs b/AutoWorkshopImplement/Models/Shop.cs new file mode 100644 index 0000000..bf6eec9 --- /dev/null +++ b/AutoWorkshopImplement/Models/Shop.cs @@ -0,0 +1,52 @@ +using AutoWorkshopContracts.BindingModels; +using AutoWorkshopContracts.ViewModels; +using AutoWorkshopDataModels.Models; + +namespace AutoWorkshopListImplement.Models +{ + public class Shop : IShopModel + { + public int Id { get; private set; } + + public string ShopName { get; private set; } = string.Empty; + + public string Address { get; private set; } = string.Empty; + + public DateTime OpeningDate { get; private set; } + + public Dictionary ShopRepairs { get; private set; } = new(); + + public static Shop? Create(ShopBindingModel? Model) + { + if (Model is null) + return null; + + return new Shop() + { + Id = Model.Id, + ShopName = Model.ShopName, + Address = Model.Address, + OpeningDate = Model.OpeningDate + }; + } + + public void Update(ShopBindingModel? Model) + { + if (Model is null) + return; + + ShopName = Model.ShopName; + Address = Model.Address; + OpeningDate = Model.OpeningDate; + } + + public ShopViewModel GetViewModel => new() + { + Id = Id, + ShopName = ShopName, + Address = Address, + OpeningDate = OpeningDate, + ShopRepairs = ShopRepairs + }; + } +}