сделала бизнес логику

This commit is contained in:
Nastya_Kozlova 2024-04-28 17:52:00 +04:00
parent 4202c78a7f
commit d935c309e1
5 changed files with 585 additions and 0 deletions

@ -0,0 +1,178 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using CarCenterContracts.BusinessLogicsContracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class AdministratorLogic : IAdministratorLogic
{
private readonly int _loginMaxLength = 50;
private readonly int _passwordMaxLength = 50;
private readonly int _passwordMinLength = 10;
private readonly ILogger _logger;
private readonly IAdministratorStorage _administratorStorage;
public AdministratorLogic(ILogger<AdministratorLogic> logger, IAdministratorStorage administratorStorage)
{
_logger = logger;
_administratorStorage = administratorStorage;
}
public bool Create(AdministratorBindingModel model)
{
CheckModel(model);
if (_administratorStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(AdministratorBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_administratorStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public AdministratorViewModel? ReadElement(AdministratorSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}.", model.AdministratorFIO, model.AdministratorLogin, model.Id);
var element = _administratorStorage.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<AdministratorViewModel>? ReadList(AdministratorSearchModel? model)
{
_logger.LogInformation("ReadList. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}.", model?.AdministratorFIO, model?.AdministratorLogin, model?.Id);
var list = model == null ? _administratorStorage.GetFullList() : _administratorStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public bool Update(AdministratorBindingModel model)
{
CheckModel(model);
if (_administratorStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(AdministratorBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.AdministratorFIO))
{
throw new ArgumentNullException("Нет ФИО администратора", nameof(model.AdministratorFIO));
}
if (string.IsNullOrEmpty(model.AdministratorLogin))
{
throw new ArgumentNullException("Нет логина администратора", nameof(model.AdministratorLogin));
}
if (model.AdministratorLogin.Length > _loginMaxLength)
{
throw new ArgumentNullException("Логин слишком длинный", nameof(model.AdministratorLogin));
}
if (string.IsNullOrEmpty(model.AdministratorNumber))
{
throw new ArgumentNullException("Нет номера телефона администратора", nameof(model.AdministratorNumber));
}
if (model.AdministratorEmail.Length > _loginMaxLength || !Regex.IsMatch(model.AdministratorEmail, @"([a-zA-Z0-9]+@[a-zA-Z0-9]+\.[a-zA-Z0-9]+)"))
{
throw new Exception($"В качестве логина должна быть указана почта и иметь длинну не более {_loginMaxLength} символов");
}
if (string.IsNullOrEmpty(model.AdministratorEmail))
{
throw new ArgumentNullException("Нет почты администратора", nameof(model.AdministratorEmail));
}
if (string.IsNullOrEmpty(model.AdministratorPassword))
{
throw new ArgumentNullException("Нет пароля администратора", nameof(model.AdministratorPassword));
}
if (model.AdministratorPassword.Length > _passwordMaxLength || model.AdministratorPassword.Length < _passwordMinLength
|| !Regex.IsMatch(model.AdministratorPassword, @"^((\w+\d+\W+)|(\w+\W+\d+)|(\d+\w+\W+)|(\d+\W+\w+)|(\W+\w+\d+)|(\W+\d+\w+))[\w\d\W]*$"))
{
throw new Exception($"Пароль длиной от {_passwordMinLength} до {_passwordMaxLength} должен состоять из цифр, букв и небуквенных символов");
}
_logger.LogInformation("Administrator. AdministratorFIO: {AdministratorFIO}. AdministratorLogin: {AdministratorLogin}. Id: {Id}", model.AdministratorFIO, model.AdministratorLogin, model.Id);
var element = _administratorStorage.GetElement(new AdministratorSearchModel
{
AdministratorEmail = model.AdministratorEmail
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("администратор с таким логином уже есть");
}
}
}
}

@ -0,0 +1,136 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class CarLogic : ICarLogic
{
private readonly ILogger _logger;
private readonly ICarStorage _carStorage;
private readonly IAdministratorLogic _administratorLogic;
public CarLogic(ILogger<CarLogic> logger, ICarStorage carStorage, IAdministratorLogic administratorLogic)
{
_logger = logger;
_carStorage = carStorage;
_administratorLogic = administratorLogic;
}
public bool Create(CarBindingModel model)
{
CheckModel(model);
var result = _carStorage.Insert(model);
if (result == 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);
var result = _carStorage.Delete(model);
if (result == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public CarViewModel? ReadElement(CarSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. BrandCar:{BrandCar}.Id:{Id}", model.BrandCar, 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. BrandCar:{BrandCar}.Id:{ Id}", model?.BrandCar, 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;
}
public bool Update(CarBindingModel model)
{
CheckModel(model);
if (_carStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(CarBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.BrandCar))
{
throw new ArgumentNullException("Нет названия бренда", nameof(model.BrandCar));
}
if (string.IsNullOrEmpty(model.Model))
{
throw new ArgumentNullException("Нет названия модели", nameof(model.Model));
}
_logger.LogInformation("Car. BrandCar:{BrandCar}.Model:{ Model}. Id: { Id}", model.BrandCar, model.BrandCar, model.Id);
}
}
}

@ -0,0 +1,135 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class EquipmentLogic : IEquipmentLogic
{
private readonly ILogger _logger;
private readonly IEquipmentStorage _equipmentStorage;
public EquipmentLogic(ILogger<EquipmentLogic> logger, IEquipmentStorage equipmentStorage)
{
_logger = logger;
_equipmentStorage = equipmentStorage;
}
public bool Create(EquipmentBindingModel model)
{
CheckModel(model);
model.EquipmentCars = new();
var result = _equipmentStorage.Insert(model);
if (result == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(EquipmentBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
var result = _equipmentStorage.Delete(model);
if (result == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public EquipmentViewModel? ReadElement(EquipmentSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. EquipmentName:{EquipmentName}.Id:{Id}", model.EquipmentName, model.Id);
var element = _equipmentStorage.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<EquipmentViewModel>? ReadList(EquipmentSearchModel? model)
{
_logger.LogInformation("ReadList. EquipmentName:{EquipmentName}.Id:{ Id}", model?.EquipmentName, model?.Id);
var list = model == null ? _equipmentStorage.GetFullList() : _equipmentStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(EquipmentBindingModel model)
{
CheckModel(model);
if (_equipmentStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(EquipmentBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.EquipmentName))
{
throw new ArgumentNullException("Нет названия комплектации", nameof(model.EquipmentName));
}
if (model.EquipmentPrice < 0)
{
throw new ArgumentNullException("Стоимость не может быть меньше 0", nameof(model.EquipmentPrice));
}
_logger.LogInformation("Equipment. EquipmentName:{EquipmentName}.EquipmentPrice:{ EquipmentPrice}. Id: { Id}", model.EquipmentName, model.EquipmentPrice, model.Id);
}
}
}

@ -0,0 +1,132 @@
using CarCenterContracts.BindingModels;
using CarCenterContracts.BusinessLogicsContracts;
using CarCenterContracts.SearchModels;
using CarCenterContracts.StoragesContracts;
using CarCenterContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
namespace CarCenterBusinessLogic.BusinessLogics
{
public class InspectionLogic : IInspectionLogic
{
private readonly ILogger _logger;
private readonly IInspectionStorage _inspectionStorage;
public InspectionLogic(ILogger<InspectionLogic> logger, IInspectionStorage inspectionStorage)
{
_logger = logger;
_inspectionStorage = inspectionStorage;
}
public bool Create(InspectionBindingModel model)
{
CheckModel(model);
model.InspectionCars = new();
var result = _inspectionStorage.Insert(model);
if (result == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(InspectionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
var result = _inspectionStorage.Delete(model);
if (result == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public InspectionViewModel? ReadElement(InspectionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Id:{Id}", model.Id);
var element = _inspectionStorage.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<InspectionViewModel>? ReadList(InspectionSearchModel? model)
{
_logger.LogInformation("ReadList. Id:{ Id}", model?.Id);
var list = model == null ? _inspectionStorage.GetFullList() : _inspectionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public bool Update(InspectionBindingModel model)
{
CheckModel(model);
if (_inspectionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(InspectionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.InspectionName))
{
throw new ArgumentNullException("Нет названия осмотра", nameof(model.InspectionName));
}
_logger.LogInformation("Inspection. Id: { Id}", model.Id);
}
}
}

@ -6,6 +6,10 @@
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\CarCenterContracts\CarCenterContracts.csproj" />
</ItemGroup>