Создана бизнес логика для сущностей, классы-компоненты, классы хранилищ

This commit is contained in:
Safgerd 2023-02-12 22:52:23 +04:00
parent e46be09449
commit 44f5e6ac59
33 changed files with 1085 additions and 32 deletions

View File

@ -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

View File

@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\AutomobilePlantContracts\AutomobilePlantContracts.csproj" />
</ItemGroup>
</Project>

View File

@ -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<CarLogic> 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<CarViewModel>? 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("Авто с таким названием уже есть");
}
}
}
}

View File

@ -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<ComponentLogic> 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<ComponentViewModel>? 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("Компонент с таким названием уже есть");
}
}
}
}

View File

@ -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<OrderLogic> 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<OrderViewModel>? 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);
}
}
}

View File

@ -6,4 +6,8 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AutomobilePlantDataModels\AutomobilePlantDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -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<int, (IComponentModel, int)> CarComponents { get; set; } = new();
}
}

View File

@ -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; }
}
}

View File

@ -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<CarViewModel>? ReadList(CarSearchModel? model);
CarViewModel? ReadElement(CarSearchModel model);
bool Create(CarBindingModel model);
bool Update(CarBindingModel model);
bool Delete(CarBindingModel model);
}
}

View File

@ -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<ComponentViewModel>? ReadList(ComponentSearchModel? model);
ComponentViewModel? ReadElement(ComponentSearchModel model);
bool Create(ComponentBindingModel model);
bool Update(ComponentBindingModel model);
bool Delete(ComponentBindingModel model);
}
}

View File

@ -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<OrderViewModel>? ReadList(OrderSearchModel? model);
bool CreateOrder(OrderBindingModel model);
bool TakeOrderInWork(OrderBindingModel model);
bool FinishOrder(OrderBindingModel model);
bool DeliveryOrder(OrderBindingModel model);
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -6,7 +6,8 @@ using System.Threading.Tasks;
namespace AutomobilePlantContracts.SearchModels
{
internal class OrderSearchModel
public class OrderSearchModel
{
public int? Id { get; set; }
}
}

View File

@ -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<CarViewModel> GetFullList();
List<CarViewModel> GetFilteredList(CarSearchModel model);
CarViewModel? GetElement(CarSearchModel model);
CarViewModel? Insert(CarBindingModel model);
CarViewModel? Update(CarBindingModel model);
CarViewModel? Delete(CarBindingModel model);
}
}

View File

@ -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<ComponentViewModel> GetFullList();
List<ComponentViewModel> GetFilteredList(ComponentSearchModel model);
ComponentViewModel? GetElement(ComponentSearchModel model);
ComponentViewModel? Insert(ComponentBindingModel model);
ComponentViewModel? Update(ComponentBindingModel model);
ComponentViewModel? Delete(ComponentBindingModel model);
}
}

View File

@ -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<OrderViewModel> GetFullList();
List<OrderViewModel> GetFilteredList(OrderSearchModel model);
OrderViewModel? GetElement(OrderSearchModel model);
OrderViewModel? Insert(OrderBindingModel model);
OrderViewModel? Update(OrderBindingModel model);
OrderViewModel? Delete(OrderBindingModel model);
}
}

View File

@ -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<int, (IComponentModel, int)> CarComponents { get; set; } = new();
}
}

View File

@ -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; }
}
}

View File

@ -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; }
}
}

View File

@ -6,7 +6,12 @@ using System.Threading.Tasks;
namespace AutomobilePlantDataModels.Enums
{
internal class OrderStatus
public enum OrderStatus
{
Неизвестен = -1,
Принят = 0,
Выполняется = 1,
Готов = 2,
Выдан = 3,
}
}

View File

@ -1,7 +1,7 @@
namespace AutomobilePlantDataModels
{
public class Class1
public interface IId
{
int Id { get; }
}
}

View File

@ -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<int, (IComponentModel, int)> CarComponents { get; }
}
}

View File

@ -6,7 +6,9 @@ using System.Threading.Tasks;
namespace AutomobilePlantDataModels.Models
{
internal interface IComponentModel
public interface IComponentModel : IId
{
string ComponentName { get; }
double Cost { get; }
}
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\AutomobilePlantContracts\AutomobilePlantContracts.csproj" />
<ProjectReference Include="..\AutomobilePlantDataModels\AutomobilePlantDataModels.csproj" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,26 @@
using AutomobilePlantListImplements.Models;
namespace AutomobilePlantListImplements
{
public class DataListSingleton
{
private static DataListSingleton? _instance;
public List<Component> Components { get; set; }
public List<Order> Orders { get; set; }
public List<Car> Cars { get; set; }
private DataListSingleton()
{
Components = new List<Component>();
Orders = new List<Order>();
Cars = new List<Car>();
}
public static DataListSingleton GetInstance()
{
if (_instance == null)
{
_instance = new DataListSingleton();
}
return _instance;
}
}
}

View File

@ -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<CarViewModel> GetFilteredList(CarSearchModel model)
{
var result = new List<CarViewModel>();
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<CarViewModel> GetFullList()
{
var result = new List<CarViewModel>();
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;
}
}
}

View File

@ -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<ComponentViewModel> GetFullList()
{
var result = new List<ComponentViewModel>();
foreach (var component in _source.Components)
{
result.Add(component.GetViewModel);
}
return result;
}
public List<ComponentViewModel> GetFilteredList(ComponentSearchModel model)
{
var result = new List<ComponentViewModel>();
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;
}
}
}

View File

@ -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<OrderViewModel> GetFilteredList(OrderSearchModel model)
{
var result = new List<OrderViewModel>();
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<OrderViewModel> GetFullList()
{
var result = new List<OrderViewModel>();
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;
}
}
}

View File

@ -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<int, (IComponentModel, int)> CarComponents { get; private set; } = new Dictionary<int, (IComponentModel, int)>();
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
};
}
}

View File

@ -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
};
}
}

View File

@ -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,
};
}
}