diff --git a/AutomobilePlant/AutomobilePlant.sln b/AutomobilePlant/AutomobilePlant.sln index 15cc0c6..7e4bb3f 100644 --- a/AutomobilePlant/AutomobilePlant.sln +++ b/AutomobilePlant/AutomobilePlant.sln @@ -9,6 +9,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantDataModels", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantContracts", "AutomobilePlantContracts\AutomobilePlantContracts.csproj", "{2F1D0A55-2722-48B4-87D1-0440E74D6E0A}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantBusinessLogic", "AutomobilePlantBusinessLogic\AutomobilePlantBusinessLogic.csproj", "{CC4E3C20-ABA4-4A54-B992-B68D39A1236E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AutomobilePlantListImplements", "AutomobilePlantListImplements\AutomobilePlantListImplements.csproj", "{B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -27,6 +31,14 @@ Global {2F1D0A55-2722-48B4-87D1-0440E74D6E0A}.Debug|Any CPU.Build.0 = Debug|Any CPU {2F1D0A55-2722-48B4-87D1-0440E74D6E0A}.Release|Any CPU.ActiveCfg = Release|Any CPU {2F1D0A55-2722-48B4-87D1-0440E74D6E0A}.Release|Any CPU.Build.0 = Release|Any CPU + {CC4E3C20-ABA4-4A54-B992-B68D39A1236E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {CC4E3C20-ABA4-4A54-B992-B68D39A1236E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {CC4E3C20-ABA4-4A54-B992-B68D39A1236E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {CC4E3C20-ABA4-4A54-B992-B68D39A1236E}.Release|Any CPU.Build.0 = Release|Any CPU + {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Debug|Any CPU.Build.0 = Debug|Any CPU + {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Release|Any CPU.ActiveCfg = Release|Any CPU + {B27736FF-39E9-4DEC-BCB4-35ACAF5ECEB9}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/AutomobilePlant/AutomobilePlantBusinessLogic/AutomobilePlantBusinessLogic.csproj b/AutomobilePlant/AutomobilePlantBusinessLogic/AutomobilePlantBusinessLogic.csproj new file mode 100644 index 0000000..9a26466 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/AutomobilePlantBusinessLogic.csproj @@ -0,0 +1,17 @@ + + + + net6.0 + enable + enable + + + + + + + + + + + diff --git a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/CarLogic.cs b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/CarLogic.cs new file mode 100644 index 0000000..3442776 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/CarLogic.cs @@ -0,0 +1,119 @@ +using AutomobilePlantContracts.BusinessLogicContracts; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantBusinessLogic.BusinessLogics +{ + public class CarLogic : ICarLogic + { + private readonly ILogger _logger; + private readonly ICarStorage _carStorage; + + public CarLogic(ILogger logger, ICarStorage carStorage) + { + _logger = logger; + _carStorage = carStorage; + } + + public bool Create(CarBindingModel model) + { + CheckModel(model); + if (_carStorage.Insert(model) == null) + { + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool Delete(CarBindingModel model) + { + CheckModel(model, false); + _logger.LogInformation("Delete. Id:{Id}", model.Id); + if (_carStorage.Delete(model) == null) + { + _logger.LogWarning("Delete operation failed"); + return false; + } + return true; + } + + public bool Update(CarBindingModel model) + { + CheckModel(model); + if (_carStorage.Update(model) == null) + { + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public CarViewModel? ReadElement(CarSearchModel model) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + _logger.LogInformation("ReadElement. CarName:{CarName}.Id:{ Id}", model.CarName, model.Id); + var element = _carStorage.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(CarSearchModel? model) + { + _logger.LogInformation("ReadList. CarName:{CarName}.Id:{ Id}", model?.CarName, model?.Id); + var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model); + if (list == null) + { + _logger.LogWarning("ReadList return null list"); + return null; + } + _logger.LogInformation("ReadList. Count:{Count}", list.Count); + return list; + } + + private void CheckModel(CarBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (string.IsNullOrEmpty(model.CarName)) + { + throw new ArgumentNullException("Нет названия авто", nameof(model.CarName)); + } + if (model.Price <= 0) + { + throw new ArgumentNullException("Стоимость авто должна быть больше 0", nameof(model.Price)); + } + _logger.LogInformation("Car. CarName:{CarName}.Price:{ Price}. Id: { Id}", model.CarName, model.Price, model.Id); + var element = _carStorage.GetElement(new CarSearchModel + { + CarName = model.CarName + }); + if (element != null && element.Id != model.Id) + { + throw new InvalidOperationException("Авто с таким названием уже есть"); + } + } + } +} diff --git a/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ComponentLogic.cs b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ComponentLogic.cs new file mode 100644 index 0000000..2a07959 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/ComponentLogic.cs @@ -0,0 +1,119 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.BusinessLogicContracts; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantBusinessLogic.BusinessLogics +{ + internal class ComponentLogic : IComponentLogic + { + private readonly ILogger _logger; + private readonly IComponentStorage _componentStorage; + + public ComponentLogic(ILogger logger, IComponentStorage componentStorage) + { + _logger = logger; + _componentStorage = componentStorage; + } + + 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; + } + + 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; + } + + 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/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs new file mode 100644 index 0000000..74b40fa --- /dev/null +++ b/AutomobilePlant/AutomobilePlantBusinessLogic/BusinessLogics/OrderLogic.cs @@ -0,0 +1,117 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.BusinessLogicContracts; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Enums; +using Microsoft.Extensions.Logging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantBusinessLogic.BusinessLogics +{ + public class OrderLogic : IOrderLogic + { + private readonly ILogger _logger; + private readonly IOrderStorage _orderStorage; + + public OrderLogic(ILogger logger, IOrderStorage orderStorage) + { + _logger = logger; + _orderStorage = orderStorage; + } + + public bool CreateOrder(OrderBindingModel model) + { + CheckModel(model); + if (model.Status != OrderStatus.Неизвестен) + { + _logger.LogWarning("Insert operation failed. Order status incorrect."); + return false; + } + model.Status = OrderStatus.Принят; + if (_orderStorage.Insert(model) == null) + { + model.Status = OrderStatus.Неизвестен; + _logger.LogWarning("Insert operation failed"); + return false; + } + return true; + } + + public bool StatusUpdate(OrderBindingModel model, OrderStatus newStatus) + { + CheckModel(model); + if (model.Status + 1 != newStatus) + { + _logger.LogWarning("Status update to " + newStatus.ToString() + " operation failed. Order status incorrect."); + return false; + } + model.Status = newStatus; + if (model.Status == OrderStatus.Выдан) model.DateImplement = DateTime.Now; + if (_orderStorage.Update(model) == null) + { + model.Status--; + _logger.LogWarning("Update operation failed"); + return false; + } + return true; + } + + public bool TakeOrderInWork(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выполняется); + } + + public bool DeliveryOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Готов); + } + + public bool FinishOrder(OrderBindingModel model) + { + return StatusUpdate(model, OrderStatus.Выдан); + } + + public List? ReadList(OrderSearchModel? model) + { + _logger.LogInformation("Order. 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; + } + + private void CheckModel(OrderBindingModel model, bool withParams = true) + { + if (model == null) + { + throw new ArgumentNullException(nameof(model)); + } + if (!withParams) + { + return; + } + if (model.CarId < 0) + { + throw new ArgumentNullException("Некорректный идентификатор авто", nameof(model.CarId)); + } + 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}. CarId: { CarId}", model.Id, model.Sum, model.CarId); + } + } +} diff --git a/AutomobilePlant/AutomobilePlantContracts/AutomobilePlantContracts.csproj b/AutomobilePlant/AutomobilePlantContracts/AutomobilePlantContracts.csproj index 132c02c..743e9bf 100644 --- a/AutomobilePlant/AutomobilePlantContracts/AutomobilePlantContracts.csproj +++ b/AutomobilePlant/AutomobilePlantContracts/AutomobilePlantContracts.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/AutomobilePlant/AutomobilePlantContracts/BindingModels/CarBindingModel.cs b/AutomobilePlant/AutomobilePlantContracts/BindingModels/CarBindingModel.cs index 1c94c76..b8170a5 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BindingModels/CarBindingModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BindingModels/CarBindingModel.cs @@ -1,4 +1,5 @@ -using System; +using AutomobilePlantDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +7,11 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.BindingModels { - internal class CarBindingModel + public class CarBindingModel : ICarModel { + public int Id { get; set; } + public string CarName { get; set; } = string.Empty; + public double Price { get; set; } + public Dictionary CarComponents { get; set; } = new(); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/BindingModels/OrderBindingModel.cs b/AutomobilePlant/AutomobilePlantContracts/BindingModels/OrderBindingModel.cs index 56150d0..a0e90ae 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BindingModels/OrderBindingModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BindingModels/OrderBindingModel.cs @@ -1,4 +1,6 @@ -using System; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +8,14 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.BindingModels { - internal class OrderBindingModel + public class OrderBindingModel : IOrderModel { + public int Id { get; set; } + public int CarId { 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/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/ICarLogic.cs b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/ICarLogic.cs index e7ea3c2..af898de 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/ICarLogic.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/ICarLogic.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,12 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.BusinessLogicContracts { - internal interface ICarLogic + public interface ICarLogic { + List? ReadList(CarSearchModel? model); + CarViewModel? ReadElement(CarSearchModel model); + bool Create(CarBindingModel model); + bool Update(CarBindingModel model); + bool Delete(CarBindingModel model); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IComponentLogic.cs b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IComponentLogic.cs index 6691c48..05bd306 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IComponentLogic.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IComponentLogic.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,13 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.BusinessLogicContracts { - internal interface IComponentLogic + 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/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IOrderLogic.cs b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IOrderLogic.cs index 8c616bd..c5bc886 100644 --- a/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IOrderLogic.cs +++ b/AutomobilePlant/AutomobilePlantContracts/BusinessLogicContracts/IOrderLogic.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,12 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.BusinessLogicContracts { - internal interface IOrderLogic + 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/AutomobilePlant/AutomobilePlantContracts/SearchModels/CarSearchModel.cs b/AutomobilePlant/AutomobilePlantContracts/SearchModels/CarSearchModel.cs index 6478e9b..8994576 100644 --- a/AutomobilePlant/AutomobilePlantContracts/SearchModels/CarSearchModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/SearchModels/CarSearchModel.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.SearchModels { - internal class CarSearchModel + public class CarSearchModel { + public int? Id { get; set; } + public string? CarName { get; set; } } } diff --git a/AutomobilePlant/AutomobilePlantContracts/SearchModels/ComponentSearchModel.cs b/AutomobilePlant/AutomobilePlantContracts/SearchModels/ComponentSearchModel.cs index a80a44d..f158a80 100644 --- a/AutomobilePlant/AutomobilePlantContracts/SearchModels/ComponentSearchModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/SearchModels/ComponentSearchModel.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.SearchModels { - internal class ComponentSearchModel + public class ComponentSearchModel { + public int? Id { get; set; } + public string? ComponentName { get; set; } } } diff --git a/AutomobilePlant/AutomobilePlantContracts/SearchModels/OrderSearchModel.cs b/AutomobilePlant/AutomobilePlantContracts/SearchModels/OrderSearchModel.cs index 41bd536..afbc5f1 100644 --- a/AutomobilePlant/AutomobilePlantContracts/SearchModels/OrderSearchModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/SearchModels/OrderSearchModel.cs @@ -6,7 +6,8 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.SearchModels { - internal class OrderSearchModel + public class OrderSearchModel { + public int? Id { get; set; } } } diff --git a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/ICarStorage.cs b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/ICarStorage.cs index adf89e7..0e64ae4 100644 --- a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/ICarStorage.cs +++ b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/ICarStorage.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.BindingModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,13 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.StorageContracts { - internal interface ICarStorage + public interface ICarStorage { + List GetFullList(); + List GetFilteredList(CarSearchModel model); + CarViewModel? GetElement(CarSearchModel model); + CarViewModel? Insert(CarBindingModel model); + CarViewModel? Update(CarBindingModel model); + CarViewModel? Delete(CarBindingModel model); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IComponentStorage.cs b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IComponentStorage.cs index d0bf943..d024fbd 100644 --- a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IComponentStorage.cs +++ b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IComponentStorage.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,13 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.StorageContracts { - internal interface IComponentStorage + 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/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IOrderStorage.cs b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IOrderStorage.cs index 29de6c2..321979f 100644 --- a/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IOrderStorage.cs +++ b/AutomobilePlant/AutomobilePlantContracts/StorageContracts/IOrderStorage.cs @@ -1,4 +1,7 @@ -using System; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.ViewModels; +using System; using System.Collections.Generic; using System.Linq; using System.Text; @@ -6,7 +9,13 @@ using System.Threading.Tasks; namespace AutomobilePlantContracts.StorageContracts { - internal interface IOrderStorage + 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/AutomobilePlant/AutomobilePlantContracts/ViewModels/CarViewModel.cs b/AutomobilePlant/AutomobilePlantContracts/ViewModels/CarViewModel.cs index 0238642..c095358 100644 --- a/AutomobilePlant/AutomobilePlantContracts/ViewModels/CarViewModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/ViewModels/CarViewModel.cs @@ -1,12 +1,20 @@ -using System; +using AutomobilePlantDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutomobilePlantContracts.ViewModels { - internal class CarViewModel + public class CarViewModel : ICarModel { + public int Id { get; set; } + [DisplayName("Название машины")] + public string CarName { get; set; } = string.Empty; + [DisplayName("Цена")] + public double Price { get; set; } + public Dictionary CarComponents { get; set; } = new(); } } diff --git a/AutomobilePlant/AutomobilePlantContracts/ViewModels/ComponentViewModel.cs b/AutomobilePlant/AutomobilePlantContracts/ViewModels/ComponentViewModel.cs index 80c1444..92d581d 100644 --- a/AutomobilePlant/AutomobilePlantContracts/ViewModels/ComponentViewModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/ViewModels/ComponentViewModel.cs @@ -1,12 +1,19 @@ -using System; +using AutomobilePlantDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutomobilePlantContracts.ViewModels { - internal class ComponentViewModel + 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/AutomobilePlant/AutomobilePlantContracts/ViewModels/OrderViewModel.cs b/AutomobilePlant/AutomobilePlantContracts/ViewModels/OrderViewModel.cs index d08e5d8..8bfe573 100644 --- a/AutomobilePlant/AutomobilePlantContracts/ViewModels/OrderViewModel.cs +++ b/AutomobilePlant/AutomobilePlantContracts/ViewModels/OrderViewModel.cs @@ -1,12 +1,30 @@ -using System; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; using System.Collections.Generic; +using System.ComponentModel; using System.Linq; using System.Text; using System.Threading.Tasks; namespace AutomobilePlantContracts.ViewModels { - internal class OrderViewModel + public class OrderViewModel : IOrderModel { + [DisplayName("Номер")] + public int Id { get; set; } + public int CarId { get; set; } + [DisplayName("Машина")] + public string CarName { 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/AutomobilePlant/AutomobilePlantDataModels/Enums/OrderStatus.cs b/AutomobilePlant/AutomobilePlantDataModels/Enums/OrderStatus.cs index 59b7bd7..9f6907e 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/Enums/OrderStatus.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/Enums/OrderStatus.cs @@ -6,7 +6,12 @@ using System.Threading.Tasks; namespace AutomobilePlantDataModels.Enums { - internal class OrderStatus + public enum OrderStatus { + Неизвестен = -1, + Принят = 0, + Выполняется = 1, + Готов = 2, + Выдан = 3, } } diff --git a/AutomobilePlant/AutomobilePlantDataModels/IID.cs b/AutomobilePlant/AutomobilePlantDataModels/IID.cs index 23fc0f3..e2268d5 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/IID.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/IID.cs @@ -1,7 +1,7 @@ namespace AutomobilePlantDataModels { - public class Class1 + public interface IId { - + int Id { get; } } } \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlantDataModels/Models/ICarModel.cs b/AutomobilePlant/AutomobilePlantDataModels/Models/ICarModel.cs index 38dbafa..53e8864 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/Models/ICarModel.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/Models/ICarModel.cs @@ -6,7 +6,10 @@ using System.Threading.Tasks; namespace AutomobilePlantDataModels.Models { - internal interface ICarModel + public interface ICarModel : IId { + string CarName { get; } + double Price { get; } + Dictionary CarComponents { get; } } } diff --git a/AutomobilePlant/AutomobilePlantDataModels/Models/IComponentModel.cs b/AutomobilePlant/AutomobilePlantDataModels/Models/IComponentModel.cs index 54e93e8..65039ce 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/Models/IComponentModel.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/Models/IComponentModel.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; namespace AutomobilePlantDataModels.Models { - internal interface IComponentModel + public interface IComponentModel : IId { + string ComponentName { get; } + double Cost { get; } } } diff --git a/AutomobilePlant/AutomobilePlantDataModels/Models/IOrderModel.cs b/AutomobilePlant/AutomobilePlantDataModels/Models/IOrderModel.cs index 06721c5..6acb442 100644 --- a/AutomobilePlant/AutomobilePlantDataModels/Models/IOrderModel.cs +++ b/AutomobilePlant/AutomobilePlantDataModels/Models/IOrderModel.cs @@ -1,12 +1,21 @@ -using System; +using AutomobilePlantDataModels.Enums; +using System; using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography; using System.Text; using System.Threading.Tasks; namespace AutomobilePlantDataModels.Models { - internal interface IOrderModel + public interface IOrderModel : IId { + int CarId { get; } + int Count { get; } + double Sum { get; } + OrderStatus Status { get; } + DateTime DateCreate { get; } + DateTime? DateImplement { get; } + } } diff --git a/AutomobilePlant/AutomobilePlantListImplements/AutomobilePlantListImplements.csproj b/AutomobilePlant/AutomobilePlantListImplements/AutomobilePlantListImplements.csproj new file mode 100644 index 0000000..a6820c2 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/AutomobilePlantListImplements.csproj @@ -0,0 +1,14 @@ + + + + net6.0 + enable + enable + + + + + + + + diff --git a/AutomobilePlant/AutomobilePlantListImplements/DataListSingleton.cs b/AutomobilePlant/AutomobilePlantListImplements/DataListSingleton.cs new file mode 100644 index 0000000..d18f01f --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/DataListSingleton.cs @@ -0,0 +1,26 @@ +using AutomobilePlantListImplements.Models; + +namespace AutomobilePlantListImplements +{ + public class DataListSingleton + { + private static DataListSingleton? _instance; + public List Components { get; set; } + public List Orders { get; set; } + public List Cars { get; set; } + private DataListSingleton() + { + Components = new List(); + Orders = new List(); + Cars = new List(); + } + public static DataListSingleton GetInstance() + { + if (_instance == null) + { + _instance = new DataListSingleton(); + } + return _instance; + } + } +} \ No newline at end of file diff --git a/AutomobilePlant/AutomobilePlantListImplements/Implements/CarStorage.cs b/AutomobilePlant/AutomobilePlantListImplements/Implements/CarStorage.cs new file mode 100644 index 0000000..ade393d --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Implements/CarStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantListImplements.Models; + +namespace AutomobilePlantListImplements.Implements +{ + public class CarStorage : ICarStorage + { + private readonly DataListSingleton _source; + + public CarStorage() + { + _source = DataListSingleton.GetInstance(); + } + public CarViewModel? GetElement(CarSearchModel model) + { + if (string.IsNullOrEmpty(model.CarName) && !model.Id.HasValue) + { + return null; + } + foreach (var car in _source.Cars) + { + if ((!string.IsNullOrEmpty(model.CarName) && car.CarName == model.CarName) || (model.Id.HasValue && car.Id == model.Id)) + { + return car.GetViewModel; + } + } + return null; + } + + public List GetFilteredList(CarSearchModel model) + { + var result = new List(); + if (string.IsNullOrEmpty(model.CarName)) + { + return result; + } + foreach (var car in _source.Cars) + { + if (car.CarName.Contains(model.CarName)) + { + result.Add(car.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var car in _source.Cars) + { + result.Add(car.GetViewModel); + } + return result; + } + + public CarViewModel? Insert(CarBindingModel model) + { + model.Id = 1; + foreach (var car in _source.Cars) + { + if (model.Id <= car.Id) + { + model.Id = car.Id + 1; + } + } + var newCar = Car.Create(model); + if (newCar == null) + { + return null; + } + _source.Cars.Add(newCar); + return newCar.GetViewModel; + } + + public CarViewModel? Update(CarBindingModel model) + { + foreach (var car in _source.Cars) + { + if (car.Id == model.Id) + { + car.Update(model); + return car.GetViewModel; + } + } + return null; + } + public CarViewModel? Delete(CarBindingModel model) + { + for (int i = 0; i < _source.Cars.Count; ++i) + { + if (_source.Cars[i].Id == model.Id) + { + var element = _source.Cars[i]; + _source.Cars.RemoveAt(i); + return element.GetViewModel; + } + } + return null; + } + } +} diff --git a/AutomobilePlant/AutomobilePlantListImplements/Implements/ComponentStorage.cs b/AutomobilePlant/AutomobilePlantListImplements/Implements/ComponentStorage.cs new file mode 100644 index 0000000..f458863 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Implements/ComponentStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantListImplements.Models; + + +namespace AutomobilePlantListImplements.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/AutomobilePlant/AutomobilePlantListImplements/Implements/OrderStorage.cs b/AutomobilePlant/AutomobilePlantListImplements/Implements/OrderStorage.cs new file mode 100644 index 0000000..9e90182 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Implements/OrderStorage.cs @@ -0,0 +1,111 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.SearchModels; +using AutomobilePlantContracts.StorageContracts; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantListImplements.Models; + +namespace AutomobilePlantListImplements.Implements +{ + public class OrderStorage : IOrderStorage + { + private readonly DataListSingleton _source; + + public OrderStorage() + { + _source = DataListSingleton.GetInstance(); + } + + 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 List GetFilteredList(OrderSearchModel model) + { + var result = new List(); + if (!model.Id.HasValue) + { + return result; + } + foreach (var order in _source.Orders) + { + if (order.Id == model.Id) + { + result.Add(order.GetViewModel); + } + } + return result; + } + + public List GetFullList() + { + var result = new List(); + foreach (var order in _source.Orders) + { + result.Add(order.GetViewModel); + } + return result; + } + + 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/AutomobilePlant/AutomobilePlantListImplements/Models/Car.cs b/AutomobilePlant/AutomobilePlantListImplements/Models/Car.cs new file mode 100644 index 0000000..064573a --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Models/Car.cs @@ -0,0 +1,56 @@ +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection.Metadata; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; + +namespace AutomobilePlantListImplements.Models +{ + public class Car : ICarModel + { + public string CarName { get; private set; } = string.Empty; + + public double Price { get; private set; } + + public Dictionary CarComponents { get; private set; } = new Dictionary(); + + public int Id { get; private set; } + + public static Car? Create(CarBindingModel? model) + { + if (model == null) + { + return null; + } + return new Car() + { + Id = model.Id, + CarName = model.CarName, + Price = model.Price, + CarComponents = model.CarComponents + }; + } + + public void Update(CarBindingModel? model) + { + if (model == null) + { + return; + } + CarName = model.CarName; + Price = model.Price; + CarComponents = model.CarComponents; + } + public CarViewModel GetViewModel => new() + { + Id = Id, + CarName = CarName, + Price = Price, + CarComponents = CarComponents + }; + } +} diff --git a/AutomobilePlant/AutomobilePlantListImplements/Models/Component.cs b/AutomobilePlant/AutomobilePlantListImplements/Models/Component.cs new file mode 100644 index 0000000..84f1876 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Models/Component.cs @@ -0,0 +1,46 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Models; + +namespace AutomobilePlantListImplements.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/AutomobilePlant/AutomobilePlantListImplements/Models/Order.cs b/AutomobilePlant/AutomobilePlantListImplements/Models/Order.cs new file mode 100644 index 0000000..8468ea4 --- /dev/null +++ b/AutomobilePlant/AutomobilePlantListImplements/Models/Order.cs @@ -0,0 +1,67 @@ +using AutomobilePlantContracts.BindingModels; +using AutomobilePlantContracts.ViewModels; +using AutomobilePlantDataModels.Enums; +using AutomobilePlantDataModels.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace AutomobilePlantListImplements.Models +{ + public class Order : IOrderModel + { + public int CarId { get; private set; } + + public int Count { get; private set; } + + public double Sum { get; private set; } + + public OrderStatus Status { get; private set; } = OrderStatus.Неизвестен; + + public DateTime DateCreate { get; private set; } = DateTime.Now; + + public DateTime? DateImplement { get; private set; } + + public int Id { get; private set; } + + public static Order? Create(OrderBindingModel? model) + { + if (model == null) + { + return null; + } + return new Order + { + CarId = model.CarId, + Count = model.Count, + Sum = model.Sum, + Status = model.Status, + DateCreate = model.DateCreate, + DateImplement = model.DateImplement, + Id = model.Id, + }; + } + + public void Update(OrderBindingModel? model) + { + if (model == null) + { + return; + } + Status = model.Status; + } + + public OrderViewModel GetViewModel => new() + { + CarId = CarId, + Count = Count, + Sum = Sum, + DateCreate = DateCreate, + DateImplement = DateImplement, + Id = Id, + Status = Status, + }; + } +}