Бизнес-логика

This commit is contained in:
dasha 2023-03-28 17:32:55 +04:00
parent ba10c6695a
commit db49f38477
22 changed files with 613 additions and 12 deletions

View File

@ -9,6 +9,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BeautySaloonDataModels", "B
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonContracts", "BeautySaloonContracts\BeautySaloonContracts.csproj", "{7494D3AF-2581-4128-9183-BB87A9DC4B10}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonContracts", "BeautySaloonContracts\BeautySaloonContracts.csproj", "{7494D3AF-2581-4128-9183-BB87A9DC4B10}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BeautySaloonBusinessLogic", "BeautySaloonBusinessLogic\BeautySaloonBusinessLogic.csproj", "{E43A1394-BC9A-430B-B984-BCCD828FFF45}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -27,6 +29,10 @@ Global
{7494D3AF-2581-4128-9183-BB87A9DC4B10}.Debug|Any CPU.Build.0 = Debug|Any CPU {7494D3AF-2581-4128-9183-BB87A9DC4B10}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7494D3AF-2581-4128-9183-BB87A9DC4B10}.Release|Any CPU.ActiveCfg = Release|Any CPU {7494D3AF-2581-4128-9183-BB87A9DC4B10}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7494D3AF-2581-4128-9183-BB87A9DC4B10}.Release|Any CPU.Build.0 = Release|Any CPU {7494D3AF-2581-4128-9183-BB87A9DC4B10}.Release|Any CPU.Build.0 = Release|Any CPU
{E43A1394-BC9A-430B-B984-BCCD828FFF45}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{E43A1394-BC9A-430B-B984-BCCD828FFF45}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E43A1394-BC9A-430B-B984-BCCD828FFF45}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E43A1394-BC9A-430B-B984-BCCD828FFF45}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE

View File

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

View File

@ -0,0 +1,103 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class ClientLogic : IClientLogic
{
private readonly IClientStorage _clientStorage;
public ClientLogic(IClientStorage clientStorage)
{
_clientStorage = clientStorage;
}
public bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
if (_clientStorage.Delete(model) == null)
{
return false;
}
return true;
}
public ClientViewModel? ReadElement(ClientSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _clientStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
var list = model == null ? _clientStorage.GetFullList() :
_clientStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ClientBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени клиента", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии клиента", nameof(model.Surname));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона клиента", nameof(model.Phone));
}
var element = _clientStorage.GetElement(new ClientSearchModel
{
Phone = model.Phone
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с таким номером телефона уже есть");
}
}
}
}

View File

@ -0,0 +1,107 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class EmployeeLogic : IEmployeeLogic
{
private readonly IEmployeeStorage _employeeStorage;
public EmployeeLogic(IEmployeeStorage employeeStorage)
{
_employeeStorage = employeeStorage;
}
public bool Create(EmployeeBindingModel model)
{
CheckModel(model);
if (_employeeStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(EmployeeBindingModel model)
{
CheckModel(model, false);
if (_employeeStorage.Delete(model) == null)
{
return false;
}
return true;
}
public EmployeeViewModel? ReadElement(EmployeeSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _employeeStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<EmployeeViewModel>? ReadList(EmployeeSearchModel? model)
{
var list = model == null ? _employeeStorage.GetFullList() :
_employeeStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(EmployeeBindingModel model)
{
CheckModel(model);
if (_employeeStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(EmployeeBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет имени сотрудника", nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии сотрудника", nameof(model.Surname));
}
if (string.IsNullOrEmpty(model.Phone))
{
throw new ArgumentNullException("Нет телефона сотрудника", nameof(model.Phone));
}
if (model.PositionId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор у должности", nameof(model.PositionId));
}
var element = _employeeStorage.GetElement(new EmployeeSearchModel
{
Phone = model.Phone
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Сотрудник с таким номером телефона уже есть");
}
}
}
}

View File

@ -0,0 +1,63 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class OrderLogic : IOrderLogic
{
private readonly IOrderStorage _orderStorage;
public OrderLogic(IOrderStorage orderStorage)
{
_orderStorage = orderStorage;
}
public bool CreateOrder(OrderBindingModel model)
{
CheckModel(model);
if (_orderStorage.Insert(model) == null)
{
return false;
}
return true;
}
public List<OrderViewModel>? ReadList(OrderSearchModel? model)
{
var list = model == null ? _orderStorage.GetFullList() :
_orderStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
private void CheckModel(OrderBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.Sum <= 0)
{
throw new ArgumentNullException("Сумма заказа должна быть больше 0", nameof(model.Sum));
}
if (model.ClientId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор клиента",
nameof(model.ClientId));
}
if (model.EmployeeId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор сотрудника",
nameof(model.ClientId));
}
}
}
}

View File

@ -0,0 +1,95 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class PositionLogic : IPositionLogic
{
private readonly IPositionStorage _positionStorage;
public PositionLogic(IPositionStorage positionStorage)
{
_positionStorage = positionStorage;
}
public bool Create(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(PositionBindingModel model)
{
CheckModel(model, false);
if (_positionStorage.Delete(model) == null)
{
return false;
}
return true;
}
public PositionViewModel? ReadElement(PositionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _positionStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<PositionViewModel>? ReadList(PositionSearchModel? model)
{
var list = model == null ? _positionStorage.GetFullList() :
_positionStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(PositionBindingModel model)
{
CheckModel(model);
if (_positionStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(PositionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия у должности", nameof(model.Name));
}
var element = _positionStorage.GetElement(new PositionSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Должность с таким названием уже есть");
}
}
}
}

View File

@ -0,0 +1,95 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class ServiceLogic : IServiceLogic
{
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(IServiceStorage serviceStorage)
{
_serviceStorage = serviceStorage;
}
public bool Create(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
if (_serviceStorage.Delete(model) == null)
{
return false;
}
return true;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _serviceStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
var list = model == null ? _serviceStorage.GetFullList() :
_serviceStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия у услуги", nameof(model.Name));
}
var element = _serviceStorage.GetElement(new ServiceSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Услуга с таким названием уже есть");
}
}
}
}

View File

@ -6,10 +6,6 @@
<Nullable>enable</Nullable> <Nullable>enable</Nullable>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<Folder Include="StoragesContracts\" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\BeautySaloonDataModels\BeautySaloonDataModels.csproj" /> <ProjectReference Include="..\BeautySaloonDataModels\BeautySaloonDataModels.csproj" />
</ItemGroup> </ItemGroup>

View File

@ -9,7 +9,8 @@ namespace BeautySaloonContracts.BindingModels
public double Sum { get; set; } public double Sum { get; set; }
public int ClientId { get; set; } public int ClientId { get; set; }
public int EmployeeId { get; set; } public int EmployeeId { get; set; }
public Dictionary<DateTime, (IServiceModel, IEmployeeModel)> OrderServices
public Dictionary<int, (DateTime, IServiceModel, IEmployeeModel)> OrderServices
{ {
get; get;
set; set;

View File

@ -7,7 +7,6 @@ namespace BeautySaloonContracts.BusinessLogicsContracts
public interface IOrderLogic public interface IOrderLogic
{ {
List<OrderViewModel>? ReadList(OrderSearchModel? model); List<OrderViewModel>? ReadList(OrderSearchModel? model);
bool CreateOrder(OrderBindingModel model); bool CreateOrder(OrderBindingModel model);
} }
} }

View File

@ -0,0 +1,16 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonContracts.StoragesContracts
{
public interface IClientStorage
{
List<ClientViewModel> GetFullList();
List<ClientViewModel> GetFilteredList(ClientSearchModel model);
ClientViewModel? GetElement(ClientSearchModel model);
ClientViewModel? Insert(ClientBindingModel model);
ClientViewModel? Update(ClientBindingModel model);
ClientViewModel? Delete(ClientBindingModel model);
}
}

View File

@ -0,0 +1,16 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonContracts.StoragesContracts
{
public interface IEmployeeStorage
{
List<EmployeeViewModel> GetFullList();
List<EmployeeViewModel> GetFilteredList(EmployeeSearchModel model);
EmployeeViewModel? GetElement(EmployeeSearchModel model);
EmployeeViewModel? Insert(EmployeeBindingModel model);
EmployeeViewModel? Update(EmployeeBindingModel model);
EmployeeViewModel? Delete(EmployeeBindingModel model);
}
}

View File

@ -0,0 +1,16 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonContracts.StoragesContracts
{
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

@ -0,0 +1,16 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonContracts.StoragesContracts
{
public interface IPositionStorage
{
List<PositionViewModel> GetFullList();
List<PositionViewModel> GetFilteredList(PositionSearchModel model);
PositionViewModel? GetElement(PositionSearchModel model);
PositionViewModel? Insert(PositionBindingModel model);
PositionViewModel? Update(PositionBindingModel model);
PositionViewModel? Delete(PositionBindingModel model);
}
}

View File

@ -0,0 +1,16 @@
using BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonContracts.StoragesContracts
{
public interface IServiceStorage
{
List<ServiceViewModel> GetFullList();
List<ServiceViewModel> GetFilteredList(ServiceSearchModel model);
ServiceViewModel? GetElement(ServiceSearchModel model);
ServiceViewModel? Insert(ServiceBindingModel model);
ServiceViewModel? Update(ServiceBindingModel model);
ServiceViewModel? Delete(ServiceBindingModel model);
}
}

View File

@ -17,6 +17,10 @@ namespace BeautySaloonContracts.ViewModels
public int EmployeeId { get; set; } public int EmployeeId { get; set; }
[DisplayName("Продавец")] [DisplayName("Продавец")]
public string EmployeeName { get; set; } = string.Empty; public string EmployeeName { get; set; } = string.Empty;
public Dictionary<DateTime, (IServiceModel, IEmployeeModel)> OrderServices { get; set; } = new(); public Dictionary<int, (DateTime, IServiceModel, IEmployeeModel)> OrderServices
{
get;
set;
} = new();
} }
} }

View File

@ -7,6 +7,6 @@
double Sum { get; } double Sum { get; }
int ClientId { get; } int ClientId { get; }
int EmployeeId { get; } int EmployeeId { get; }
Dictionary<DateTime, (IServiceModel, IEmployeeModel)> OrderServices { get; } Dictionary<int, (DateTime, IServiceModel, IEmployeeModel)> OrderServices { get; }
} }
} }

View File

@ -8,4 +8,14 @@
<ImplicitUsings>enable</ImplicitUsings> <ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup> </PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\BeautySaloonBusinessLogic\BeautySaloonBusinessLogic.csproj" />
<ProjectReference Include="..\BeautySaloonContracts\BeautySaloonContracts.csproj" />
<ProjectReference Include="..\BeautySaloonDataModels\BeautySaloonDataModels.csproj" />
</ItemGroup>
</Project> </Project>

View File

@ -1,6 +1,6 @@
namespace BeautySaloonView namespace BeautySaloonView
{ {
partial class Form1 partial class FormMain
{ {
/// <summary> /// <summary>
/// Required designer variable. /// Required designer variable.

View File

@ -1,8 +1,8 @@
namespace BeautySaloonView namespace BeautySaloonView
{ {
public partial class Form1 : Form public partial class FormMain : Form
{ {
public Form1() public FormMain()
{ {
InitializeComponent(); InitializeComponent();
} }

View File

@ -1,7 +1,14 @@
using BeautySaloonBusinessLogic;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.StoragesContracts;
using Microsoft.Extensions.DependencyInjection;
namespace BeautySaloonView namespace BeautySaloonView
{ {
internal static class Program internal static class Program
{ {
private static ServiceProvider? _serviceProvider;
public static ServiceProvider? ServiceProvider => _serviceProvider;
/// <summary> /// <summary>
/// The main entry point for the application. /// The main entry point for the application.
/// </summary> /// </summary>
@ -11,7 +18,29 @@ namespace BeautySaloonView
// To customize application configuration such as set high DPI settings or default font, // To customize application configuration such as set high DPI settings or default font,
// see https://aka.ms/applicationconfiguration. // see https://aka.ms/applicationconfiguration.
ApplicationConfiguration.Initialize(); ApplicationConfiguration.Initialize();
Application.Run(new Form1());
var services = new ServiceCollection();
ConfigureServices(services);
_serviceProvider = services.BuildServiceProvider();
Application.Run(_serviceProvider.GetRequiredService<FormMain>());
}
private static void ConfigureServices(ServiceCollection services)
{
/*services.AddTransient<IClientStorage, ClientStorage>();
services.AddTransient<IEmployeeStorage, IngredientStorage>();
services.AddTransient<IOrderStorage, OrderStorage>();
services.AddTransient<IPositionStorage, SushiStorage>();
services.AddTransient<IServiceStorage, SushiStorage>();*/
services.AddTransient<IClientLogic, ClientLogic>();
services.AddTransient<IEmployeeLogic, EmployeeLogic>();
services.AddTransient<IOrderLogic, OrderLogic>();
services.AddTransient<IPositionLogic, PositionLogic>();
services.AddTransient<IServiceLogic, ServiceLogic>();
services.AddTransient<FormMain>();
} }
} }
} }