diff --git a/ComputersShop/ComputersShop.sln b/ComputersShop/ComputersShop.sln index 7873db4..a517877 100644 --- a/ComputersShop/ComputersShop.sln +++ b/ComputersShop/ComputersShop.sln @@ -3,7 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.3.32825.248 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopView", "ComputersShopView\ComputersShopView.csproj", "{21D38D2F-5FB3-423E-86C1-B0496B9F9062}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopView", "ComputersShopView\ComputersShopView.csproj", "{21D38D2F-5FB3-423E-86C1-B0496B9F9062}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopDataModels", "ComputersShopDataModels\ComputersShopDataModels.csproj", "{EED0877E-3A07-4FF7-80AC-6DCB02D11D61}" +EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ComputersShopContracts", "ComputersShopContracts\ComputersShopContracts.csproj", "{7404526F-8C94-457B-BF18-85445EADC75F}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopBusinessLogic", "ComputersShopBusinessLogic\ComputersShopBusinessLogic.csproj", "{5270DCF9-79F8-45B5-90F4-D460B0F0F571}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ComputersShopListImplement", "ComputersShopListImplement\ComputersShopListImplement.csproj", "{F4C10F5F-A602-48E7-9820-C26B774203E7}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -15,6 +23,22 @@ Global {21D38D2F-5FB3-423E-86C1-B0496B9F9062}.Debug|Any CPU.Build.0 = Debug|Any CPU {21D38D2F-5FB3-423E-86C1-B0496B9F9062}.Release|Any CPU.ActiveCfg = Release|Any CPU {21D38D2F-5FB3-423E-86C1-B0496B9F9062}.Release|Any CPU.Build.0 = Release|Any CPU + {EED0877E-3A07-4FF7-80AC-6DCB02D11D61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EED0877E-3A07-4FF7-80AC-6DCB02D11D61}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EED0877E-3A07-4FF7-80AC-6DCB02D11D61}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EED0877E-3A07-4FF7-80AC-6DCB02D11D61}.Release|Any CPU.Build.0 = Release|Any CPU + {7404526F-8C94-457B-BF18-85445EADC75F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {7404526F-8C94-457B-BF18-85445EADC75F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {7404526F-8C94-457B-BF18-85445EADC75F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {7404526F-8C94-457B-BF18-85445EADC75F}.Release|Any CPU.Build.0 = Release|Any CPU + {5270DCF9-79F8-45B5-90F4-D460B0F0F571}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5270DCF9-79F8-45B5-90F4-D460B0F0F571}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5270DCF9-79F8-45B5-90F4-D460B0F0F571}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5270DCF9-79F8-45B5-90F4-D460B0F0F571}.Release|Any CPU.Build.0 = Release|Any CPU + {F4C10F5F-A602-48E7-9820-C26B774203E7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F4C10F5F-A602-48E7-9820-C26B774203E7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F4C10F5F-A602-48E7-9820-C26B774203E7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F4C10F5F-A602-48E7-9820-C26B774203E7}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs b/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs new file mode 100644 index 0000000..ea37014 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/ComponentLogic.cs @@ -0,0 +1,114 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic +{ + public class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + + public List? ReadList(ComponentSearchModel? model) + { + _logger.LogInformation("ReadList. ComponentName:{ComponentName}.Id:{ Id}", model?.ComponentName, model?.Id); + var list = model == null ? _componentStorage.GetFullList() : _componentStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + public ComponentViewModel? ReadElement(ComponentSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComponentName:{ComponentName}.Id:{ Id}", model.ComponentName, model.Id); + var element = _componentStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + public bool Create(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + public bool Update(ComponentBindingModel model) + { + CheckModel(model); + if (_componentStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + public bool Delete(ComponentBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_componentStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + private void CheckModel(ComponentBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComponentName)) + { + throw new ArgumentNullException("Нет названия компонента", nameof(model.ComponentName)); + } + if (model.Cost <= 0) + { + throw new ArgumentNullException("Цена компонента должна быть больше 0", nameof(model.Cost)); + } + _logger.LogInformation("Component. ComponentName:{ComponentName}.Cost:{ Cost}. Id: { Id}", model.ComponentName, model.Cost, model.Id); + var element = _componentStorage.GetElement(new ComponentSearchModel + { + ComponentName = model.ComponentName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компонент с таким названием уже есть"); + } + } + } +} diff --git a/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs b/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs new file mode 100644 index 0000000..dfd2e92 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/ComputerLogic.cs @@ -0,0 +1,119 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic +{ + public class ComputerLogic : IComputerLogic + { + private readonly ILogger _logger; + private readonly IComputerStorage _computerStorage; + + public ComputerLogic(ILogger logger, IComputerStorage computerStorage) + { + _logger = logger; + _computerStorage = computerStorage; + } + + public List? ReadList(ComputerSearchModel? model) + { + _logger.LogInformation("ReadList. ComputerName:{ComputerName}.Id:{ Id}", model?.ComputerName, model?.Id); + var list = model == null ? _computerStorage.GetFullList() : _computerStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public ComputerViewModel? ReadElement(ComputerSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. ComputerName:{ComputerName}.Id:{ Id}", model.ComputerName, model.Id); + var element = _computerStorage.GetElement(model); + if (element == null) + { + _logger.LogWarning("ReadElement element not found"); + return null; + } + _logger.LogInformation("ReadElement find. Id:{Id}", element.Id); + return element; + } + + public bool Create(ComputerBindingModel model) + { + CheckModel(model); + if (_computerStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Update(ComputerBindingModel model) + { + CheckModel(model); + if (_computerStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool Delete(ComputerBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_computerStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + private void CheckModel(ComputerBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.ComputerName)) + { + throw new ArgumentNullException("Нет названия компьютера", nameof(model.ComputerName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Цена компьютера должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Computer. ComputerName:{ComputerName}.Cost:{ Cost}. Id: { Id}", model.ComputerName, model.Price, model.Id); + var element = _computerStorage.GetElement(new ComputerSearchModel + { + ComputerName = model.ComputerName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Компьютер с таким названием уже есть"); + } + } + } +} diff --git a/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj b/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj new file mode 100644 index 0000000..5830b16 --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/ComputersShopBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs b/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs new file mode 100644 index 0000000..72f8fef --- /dev/null +++ b/ComputersShop/ComputersShopBusinessLogic/OrderLogic.cs @@ -0,0 +1,116 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.BusinessLogicContracts; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopBusinessLogic +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("ReadList. OrderId:{ Id}", model?.Id); + var list = model == null ? _orderStorage.GetFullList() : _orderStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if(!CheckStatus(model, OrderStatus.Принят, false)) return false; + if (_orderStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + CheckModel(model); + if (!CheckStatus(model, OrderStatus.Выполняется)) return false; + return true; + } + + public bool DeliveryOrder(OrderBindingModel model) + { + CheckModel(model); + if (!CheckStatus(model, OrderStatus.Готов)) return false; + return true; + } + + public bool FinishOrder(OrderBindingModel model) + { + CheckModel(model); + if (!CheckStatus(model, OrderStatus.Выдан)) return false; + return true; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.ComputerId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор компьютера", nameof(model.ComputerId)); + } + if (model.Count <= 0) + { + throw new ArgumentNullException("Количество компьютеров в заказе должно быть больше 0", nameof(model.Count)); + } + if (model.Sum <= 0) + { + throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum)); + } + _logger.LogInformation("Order. OrderId:{Id}.Sum:{ Sum}. ComputerId: { ComputerId}", model.Id, model.Sum, model.ComputerId); + } + + private bool CheckStatus(OrderBindingModel model, OrderStatus newstatus, bool update = true) + { + if (model.Status != newstatus - 1) + { + _logger.LogWarning("Failed to change status"); + return false; + } + model.Status = newstatus; + if(!update) return true; + if (_orderStorage.Update(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + return true; + } + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/ComponentBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/ComponentBindingModel.cs new file mode 100644 index 0000000..252577d --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/ComponentBindingModel.cs @@ -0,0 +1,17 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BindingModels +{ + public class ComponentBindingModel : IComponentModel + { + public int Id { get; set; } + public string ComponentName { get; set; } = string.Empty; + public double Cost { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/ComputerBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/ComputerBindingModel.cs new file mode 100644 index 0000000..b3626c2 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/ComputerBindingModel.cs @@ -0,0 +1,20 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BindingModels +{ + public class ComputerBindingModel : IComputerModel + { + public int Id { get; set; } + + public string ComputerName { get; set; } = string.Empty; + + public double Price { get; set; } + + public Dictionary ComputerComponents { get; set; } = new(); + } +} diff --git a/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs b/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs new file mode 100644 index 0000000..b07eaa5 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BindingModels/OrderBindingModel.cs @@ -0,0 +1,21 @@ +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BindingModels +{ + public class OrderBindingModel : IOrderModel + { + public int Id { get; set; } + public int ComputerId { get; set; } + public int Count { get; set; } + public double Sum { get; set; } + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + public DateTime DateCreate { get; set; } = DateTime.Now; + public DateTime? DateImplement { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComponentLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComponentLogic.cs new file mode 100644 index 0000000..7711b03 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComponentLogic.cs @@ -0,0 +1,20 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IComponentLogic + { + List? ReadList(ComponentSearchModel? model); + ComponentViewModel? ReadElement(ComponentSearchModel model); + bool Create(ComponentBindingModel model); + bool Update(ComponentBindingModel model); + bool Delete(ComponentBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComputerLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComputerLogic.cs new file mode 100644 index 0000000..0aafee3 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IComputerLogic.cs @@ -0,0 +1,20 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IComputerLogic + { + List? ReadList(ComputerSearchModel? model); + ComputerViewModel? ReadElement(ComputerSearchModel model); + bool Create(ComputerBindingModel model); + bool Update(ComputerBindingModel model); + bool Delete(ComputerBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IOrderLogic.cs b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IOrderLogic.cs new file mode 100644 index 0000000..ee3c552 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/BusinessLogicContracts/IOrderLogic.cs @@ -0,0 +1,20 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.BusinessLogicContracts +{ + public interface IOrderLogic + { + List? ReadList(OrderSearchModel? model); + bool CreateOrder(OrderBindingModel model); + bool TakeOrderInWork(OrderBindingModel model); + bool FinishOrder(OrderBindingModel model); + bool DeliveryOrder(OrderBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj new file mode 100644 index 0000000..9c95599 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ComputersShopContracts.csproj @@ -0,0 +1,13 @@ + + + + net6.0 + enable + enable + + + + + + + diff --git a/ComputersShop/ComputersShopContracts/SearchModels/ComponentSearchModel.cs b/ComputersShop/ComputersShopContracts/SearchModels/ComponentSearchModel.cs new file mode 100644 index 0000000..013fe82 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/SearchModels/ComponentSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.SearchModels +{ + public class ComponentSearchModel + { + public int? Id { get; set; } + public string? ComponentName { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/SearchModels/ComputerSearchModel.cs b/ComputersShop/ComputersShopContracts/SearchModels/ComputerSearchModel.cs new file mode 100644 index 0000000..7db80ed --- /dev/null +++ b/ComputersShop/ComputersShopContracts/SearchModels/ComputerSearchModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.SearchModels +{ + public class ComputerSearchModel + { + public int? Id { get; set; } + public string? ComputerName { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/SearchModels/OrderSearchModel.cs b/ComputersShop/ComputersShopContracts/SearchModels/OrderSearchModel.cs new file mode 100644 index 0000000..db6c0c1 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/SearchModels/OrderSearchModel.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.SearchModels +{ + public class OrderSearchModel + { + public int? Id { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/StoragesContracts/IComponentStorage.cs b/ComputersShop/ComputersShopContracts/StoragesContracts/IComponentStorage.cs new file mode 100644 index 0000000..4b302f1 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StoragesContracts/IComponentStorage.cs @@ -0,0 +1,21 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.StoragesContracts +{ + public interface IComponentStorage + { + List GetFullList(); + List GetFilteredList(ComponentSearchModel model); + ComponentViewModel? GetElement(ComponentSearchModel model); + ComponentViewModel? Insert(ComponentBindingModel model); + ComponentViewModel? Update(ComponentBindingModel model); + ComponentViewModel? Delete(ComponentBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/StoragesContracts/IComputerStorage.cs b/ComputersShop/ComputersShopContracts/StoragesContracts/IComputerStorage.cs new file mode 100644 index 0000000..4a6871f --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StoragesContracts/IComputerStorage.cs @@ -0,0 +1,21 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.StoragesContracts +{ + public interface IComputerStorage + { + List GetFullList(); + List GetFilteredList(ComputerSearchModel model); + ComputerViewModel? GetElement(ComputerSearchModel model); + ComputerViewModel? Insert(ComputerBindingModel model); + ComputerViewModel? Update(ComputerBindingModel model); + ComputerViewModel? Delete(ComputerBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/StoragesContracts/IOrderStorage.cs b/ComputersShop/ComputersShopContracts/StoragesContracts/IOrderStorage.cs new file mode 100644 index 0000000..fa407d9 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/StoragesContracts/IOrderStorage.cs @@ -0,0 +1,21 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.ViewModels; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.StoragesContracts +{ + public interface IOrderStorage + { + List GetFullList(); + List GetFilteredList(OrderSearchModel model); + OrderViewModel? GetElement(OrderSearchModel model); + OrderViewModel? Insert(OrderBindingModel model); + OrderViewModel? Update(OrderBindingModel model); + OrderViewModel? Delete(OrderBindingModel model); + } +} diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs new file mode 100644 index 0000000..b0925a7 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ViewModels/ComponentViewModel.cs @@ -0,0 +1,19 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.ViewModels +{ + public class ComponentViewModel : IComponentModel + { + public int Id { get; set; } + [DisplayName("Название компонента")] + public string ComponentName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Cost { get; set; } + } +} diff --git a/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs new file mode 100644 index 0000000..aeb3348 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ViewModels/ComputerViewModel.cs @@ -0,0 +1,27 @@ +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.ViewModels +{ + public class ComputerViewModel : IComputerModel + { + public int Id { get; set; } + + [DisplayName("Название компьютера")] + public string ComputerName { get; set; } = string.Empty; + + [DisplayName("Цена")] + public double Price { get; set; } + + public Dictionary ComputerComponents + { + get; + set; + } = new(); + } +} diff --git a/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs new file mode 100644 index 0000000..5f077c9 --- /dev/null +++ b/ComputersShop/ComputersShopContracts/ViewModels/OrderViewModel.cs @@ -0,0 +1,30 @@ +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopContracts.ViewModels +{ + public class OrderViewModel : IOrderModel + { + [DisplayName("Номер")] + public int Id { get; set; } + public int ComputerId { get; set; } + [DisplayName("Компьютер")] + public string ComputerName { get; set; } = string.Empty; + [DisplayName("Количество")] + public int Count { get; set; } + [DisplayName("Сумма")] + public double Sum { get; set; } + [DisplayName("Статус")] + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + [DisplayName("Дата создания")] + public DateTime DateCreate { get; set; } = DateTime.Now; + [DisplayName("Дата выполнения")] + public DateTime? DateImplement { get; set; } + } +} diff --git a/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj new file mode 100644 index 0000000..132c02c --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/ComputersShopDataModels.csproj @@ -0,0 +1,9 @@ + + + + net6.0 + enable + enable + + + diff --git a/ComputersShop/ComputersShopDataModels/Enums/OrderStatus.cs b/ComputersShop/ComputersShopDataModels/Enums/OrderStatus.cs new file mode 100644 index 0000000..18066d8 --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/Enums/OrderStatus.cs @@ -0,0 +1,17 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Enums +{ + public enum OrderStatus + { + Неизвестен = -1, + Принят = 0, + Выполняется = 1, + Готов = 2, + Выдан = 3 + } +} diff --git a/ComputersShop/ComputersShopDataModels/IId.cs b/ComputersShop/ComputersShopDataModels/IId.cs new file mode 100644 index 0000000..99b74e8 --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/IId.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels +{ + public interface IId + { + int Id { get; } + } +} diff --git a/ComputersShop/ComputersShopDataModels/Models/IComponentModel.cs b/ComputersShop/ComputersShopDataModels/Models/IComponentModel.cs new file mode 100644 index 0000000..4c20baa --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/Models/IComponentModel.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Models +{ + public interface IComponentModel : IId + { + string ComponentName { get; } + double Cost { get; } + } +} diff --git a/ComputersShop/ComputersShopDataModels/Models/IComputerModel.cs b/ComputersShop/ComputersShopDataModels/Models/IComputerModel.cs new file mode 100644 index 0000000..39ae356 --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/Models/IComputerModel.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Models +{ + public interface IComputerModel : IId + { + string ComputerName { get; } + double Price { get; } + Dictionary ComputerComponents { get; } + } +} diff --git a/ComputersShop/ComputersShopDataModels/Models/IOrderModel.cs b/ComputersShop/ComputersShopDataModels/Models/IOrderModel.cs new file mode 100644 index 0000000..d958bbd --- /dev/null +++ b/ComputersShop/ComputersShopDataModels/Models/IOrderModel.cs @@ -0,0 +1,19 @@ +using ComputersShopDataModels.Enums; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopDataModels.Models +{ + public interface IOrderModel : IId + { + int ComputerId { get; } + int Count { get; } + double Sum { get; } + OrderStatus Status { get; } + DateTime DateCreate { get; } + DateTime? DateImplement { get; } + } +} diff --git a/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj new file mode 100644 index 0000000..b897a9c --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/ComputersShopListImplement.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/ComputersShop/ComputersShopListImplement/DataListSingleton.cs b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs new file mode 100644 index 0000000..cd26fef --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/DataListSingleton.cs @@ -0,0 +1,31 @@ +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Computers { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Computers = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs new file mode 100644 index 0000000..709b3ee --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/ComponentStorage.cs @@ -0,0 +1,106 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Implements +{ + public class ComponentStorage : IComponentStorage + { + private readonly DataListSingleton _source; + public ComponentStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var component in _source.Components) + { + result.Add(component.GetViewModel); + } + return result; + } + public List GetFilteredList(ComponentSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComponentName)) + { + return result; + } + foreach (var component in _source.Components) + { + if (component.ComponentName.Contains(model.ComponentName)) + { + result.Add(component.GetViewModel); + } + } + return result; + } + public ComponentViewModel? GetElement(ComponentSearchModel model) + { + if (string.IsNullOrEmpty(model.ComponentName) && !model.Id.HasValue) + { + return null; + } + foreach (var component in _source.Components) + { + if ((!string.IsNullOrEmpty(model.ComponentName) && component.ComponentName == model.ComponentName) || + (model.Id.HasValue && component.Id == model.Id)) + { + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Insert(ComponentBindingModel model) + { + model.Id = 1; + foreach (var component in _source.Components) + { + if (model.Id <= component.Id) + { + model.Id = component.Id + 1; + } + } + var newComponent = Component.Create(model); + if (newComponent == null) + { + return null; + } + _source.Components.Add(newComponent); + return newComponent.GetViewModel; + } + public ComponentViewModel? Update(ComponentBindingModel model) + { + foreach (var component in _source.Components) + { + if (component.Id == model.Id) + { + component.Update(model); + return component.GetViewModel; + } + } + return null; + } + public ComponentViewModel? Delete(ComponentBindingModel model) + { + for (int i = 0; i < _source.Components.Count; ++i) + { + if (_source.Components[i].Id == model.Id) + { + var element = _source.Components[i]; + _source.Components.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs new file mode 100644 index 0000000..91e4957 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/ComputerStorage.cs @@ -0,0 +1,106 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Implements +{ + public class ComputerStorage : IComputerStorage + { + private readonly DataListSingleton _source; + public ComputerStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var Computer in _source.Computers) + { + result.Add(Computer.GetViewModel); + } + return result; + } + public List GetFilteredList(ComputerSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.ComputerName)) + { + return result; + } + foreach (var Computer in _source.Computers) + { + if (Computer.ComputerName.Contains(model.ComputerName)) + { + result.Add(Computer.GetViewModel); + } + } + return result; + } + public ComputerViewModel? GetElement(ComputerSearchModel model) + { + if (string.IsNullOrEmpty(model.ComputerName) && !model.Id.HasValue) + { + return null; + } + foreach (var Computer in _source.Computers) + { + if ((!string.IsNullOrEmpty(model.ComputerName) && Computer.ComputerName == model.ComputerName) || + (model.Id.HasValue && Computer.Id == model.Id)) + { + return Computer.GetViewModel; + } + } + return null; + } + public ComputerViewModel? Insert(ComputerBindingModel model) + { + model.Id = 1; + foreach (var Computer in _source.Computers) + { + if (model.Id <= Computer.Id) + { + model.Id = Computer.Id + 1; + } + } + var newComputer = Computer.Create(model); + if (newComputer == null) + { + return null; + } + _source.Computers.Add(newComputer); + return newComputer.GetViewModel; + } + public ComputerViewModel? Update(ComputerBindingModel model) + { + foreach (var Computer in _source.Computers) + { + if (Computer.Id == model.Id) + { + Computer.Update(model); + return Computer.GetViewModel; + } + } + return null; + } + public ComputerViewModel? Delete(ComputerBindingModel model) + { + for (int i = 0; i < _source.Computers.Count; ++i) + { + if (_source.Computers[i].Id == model.Id) + { + var element = _source.Computers[i]; + _source.Computers.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs new file mode 100644 index 0000000..ec8a9be --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Implements/OrderStorage.cs @@ -0,0 +1,106 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.SearchModels; +using ComputersShopContracts.StoragesContracts; +using ComputersShopContracts.ViewModels; +using ComputersShopListImplement; +using ComputersShopListImplement.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace OrdersShopListImplement.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + public List GetFullList() + { + var result = new List(); + foreach (var Order in _source.Orders) + { + result.Add(Order.GetViewModel); + } + return result; + } + public List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (model == null) + { + return result; + } + foreach (var Order in _source.Orders) + { + if (Order.Id == model.Id) + { + result.Add(Order.GetViewModel); + } + } + return result; + } + public OrderViewModel? GetElement(OrderSearchModel model) + { + if (!model.Id.HasValue) + { + return null; + } + foreach (var Order in _source.Orders) + { + if (model.Id.HasValue && Order.Id == model.Id) + { + return Order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Insert(OrderBindingModel model) + { + model.Id = 1; + foreach (var Order in _source.Orders) + { + if (model.Id <= Order.Id) + { + model.Id = Order.Id + 1; + } + } + var newOrder = Order.Create(model); + if (newOrder == null) + { + return null; + } + _source.Orders.Add(newOrder); + return newOrder.GetViewModel; + } + public OrderViewModel? Update(OrderBindingModel model) + { + foreach (var Order in _source.Orders) + { + if (Order.Id == model.Id) + { + Order.Update(model); + return Order.GetViewModel; + } + } + return null; + } + public OrderViewModel? Delete(OrderBindingModel model) + { + for (int i = 0; i < _source.Orders.Count; ++i) + { + if (_source.Orders[i].Id == model.Id) + { + var element = _source.Orders[i]; + _source.Orders.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Component.cs b/ComputersShop/ComputersShopListImplement/Models/Component.cs new file mode 100644 index 0000000..a9c9dc7 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Component.cs @@ -0,0 +1,46 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Component : IComponentModel + { + public int Id { get; private set; } + public string ComponentName { get; private set; } = string.Empty; + public double Cost { get; set; } + public static Component? Create(ComponentBindingModel? model) + { + if (model == null) + { + return null; + } + return new Component() + { + Id = model.Id, + ComponentName = model.ComponentName, + Cost = model.Cost + }; + } + public void Update(ComponentBindingModel? model) + { + if (model == null) + { + return; + } + ComponentName = model.ComponentName; + Cost = model.Cost; + } + public ComponentViewModel GetViewModel => new() + { + Id = Id, + ComponentName = ComponentName, + Cost = Cost + }; + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Computer.cs b/ComputersShop/ComputersShopListImplement/Models/Computer.cs new file mode 100644 index 0000000..366a455 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Computer.cs @@ -0,0 +1,54 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Computer : IComputerModel + { + public int Id { get; private set; } + public string ComputerName { get; private set; } = string.Empty; + public double Price { get; private set; } + public Dictionary ComputerComponents + { + get; + private set; + } = new Dictionary(); + public static Computer? Create(ComputerBindingModel? model) + { + if (model == null) + { + return null; + } + return new Computer() + { + Id = model.Id, + ComputerName = model.ComputerName, + Price = model.Price, + ComputerComponents = model.ComputerComponents + }; + } + public void Update(ComputerBindingModel? model) + { + if (model == null) + { + return; + } + ComputerName = model.ComputerName; + Price = model.Price; + ComputerComponents = model.ComputerComponents; + } + public ComputerViewModel GetViewModel => new() + { + Id = Id, + ComputerName = ComputerName, + Price = Price, + ComputerComponents = ComputerComponents + }; + } +} diff --git a/ComputersShop/ComputersShopListImplement/Models/Order.cs b/ComputersShop/ComputersShopListImplement/Models/Order.cs new file mode 100644 index 0000000..1fc7b93 --- /dev/null +++ b/ComputersShop/ComputersShopListImplement/Models/Order.cs @@ -0,0 +1,73 @@ +using ComputersShopContracts.BindingModels; +using ComputersShopContracts.ViewModels; +using ComputersShopDataModels.Enums; +using ComputersShopDataModels.Models; +using System; +using System.Collections.Generic; +using System.Diagnostics; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace ComputersShopListImplement.Models +{ + public class Order : IOrderModel + { + public int Id { get; private set; } + + public int ComputerId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; set; } = DateTime.Now; + + public DateTime? DateImplement { get; set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order() + { + Id = model.Id, + ComputerId = model.ComputerId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + ComputerId = model.ComputerId; + Count = model.Count; + Sum = model.Sum; + Status = model.Status; + DateCreate = model.DateCreate; + DateImplement = model.DateImplement; + } + public OrderViewModel GetViewModel => new() + { + Id = Id, + ComputerId = ComputerId, + Count = Count, + Sum = Sum, + Status = Status, + DateCreate = DateCreate, + DateImplement = DateImplement + }; + } +}