2024-04-02 18:46:47 +04:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using RouteGuideContracts.BindingModels;
|
|
|
|
|
using RouteGuideContracts.BusinessLogicsContracts;
|
|
|
|
|
using RouteGuideContracts.SearchModels;
|
|
|
|
|
using RouteGuideContracts.StoragesContracts;
|
|
|
|
|
using RouteGuideContracts.ViewModels;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2024-04-09 13:48:30 +04:00
|
|
|
|
using System.Reflection;
|
2024-04-02 18:46:47 +04:00
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RouteGuideBusinessLogics.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Бизнес-логика для сущности "Водитель"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class DriverLogic : IDriverLogic
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логгер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IDriverStorage _driverStorage;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
/// <param name="driverStorage"></param>
|
|
|
|
|
public DriverLogic(ILogger<DriverLogic> logger, IDriverStorage driverStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_driverStorage = driverStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<DriverViewModel>? ReadList(DriverSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Driver.Id: {Id}", model?.Id);
|
|
|
|
|
|
|
|
|
|
var list = model == null ? _driverStorage.GetFullList() : _driverStorage.GetFilteredList(model);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList. Returned null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 13:48:30 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка из заданного количества элементов
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="count"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<DriverViewModel>? ReadList(int count)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", count);
|
|
|
|
|
|
|
|
|
|
var list = _driverStorage.GetList(count);
|
|
|
|
|
if (list == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadList. Returned null list");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
|
|
|
|
|
return list;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 18:46:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение отдельной записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
/// <exception cref="ArgumentNullException"></exception>
|
|
|
|
|
public DriverViewModel? ReadElement(DriverSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Driver.Id: {Id}", model?.Id);
|
|
|
|
|
|
|
|
|
|
var element = _driverStorage.GetElement(model!);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement. Element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Find Driver.Id: {Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Create(DriverBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
_logger.LogInformation("Create. Driver.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_driverStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Изменение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Update(DriverBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
_logger.LogInformation("Update. Driver.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_driverStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Delete(DriverBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Driver.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_driverStorage.Delete(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-09 13:48:30 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Delete()
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("Delete. Driver");
|
|
|
|
|
|
|
|
|
|
if (_driverStorage.Delete() == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление всех записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Clear()
|
|
|
|
|
{
|
|
|
|
|
int count = _driverStorage.Clear();
|
|
|
|
|
_logger.LogInformation("Clear. Delete {Count} Drivers", count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 18:46:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверка модели
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <param name="withParams"></param>
|
|
|
|
|
private void CheckModel(DriverBindingModel model, bool withParams = true)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
if (!withParams)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.FullName))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Не указано ФИО водителя", nameof(model.FullName));
|
|
|
|
|
}
|
|
|
|
|
if (string.IsNullOrEmpty(model.Phone))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Не указан номер телефона водителя", nameof(model.Phone));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("CheckModel. Driver.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _driverStorage.GetElement(new DriverSearchModel
|
|
|
|
|
{
|
|
|
|
|
Phone = model.Phone
|
|
|
|
|
});
|
2024-04-23 19:38:25 +04:00
|
|
|
|
if (element != null && !element.Id.Equals(model.Id))
|
2024-04-02 18:46:47 +04:00
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Водитель с таким номером телефона уже существует");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|