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;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace RouteGuideBusinessLogics.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Бизнес-логика для сущности "Остановка"
|
|
|
|
|
/// </summary>
|
|
|
|
|
public class StopLogic : IStopLogic
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Логгер
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Хранилище
|
|
|
|
|
/// </summary>
|
|
|
|
|
private readonly IStopStorage _stopStorage;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Конструктор
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="logger"></param>
|
|
|
|
|
/// <param name="stopStorage"></param>
|
|
|
|
|
public StopLogic(ILogger<StopLogic> logger, IStopStorage stopStorage)
|
|
|
|
|
{
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_stopStorage = stopStorage;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Получение списка
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public List<StopViewModel>? ReadList(StopSearchModel? model)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Stop: {Name}.{Id}", model?.Id, model?.Name);
|
|
|
|
|
|
|
|
|
|
var list = model == null ? _stopStorage.GetFullList() : _stopStorage.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<StopViewModel>? ReadList(int count)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation("ReadList. Count: {Count}", count);
|
|
|
|
|
|
|
|
|
|
var list = _stopStorage.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 StopViewModel? ReadElement(StopSearchModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException(nameof(model));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Stop: {Name}.{Id}", model?.Id, model?.Name);
|
|
|
|
|
|
|
|
|
|
var element = _stopStorage.GetElement(model!);
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("ReadElement. Element not found");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("ReadElement. Find Stop.Id: {Id}", element.Id);
|
|
|
|
|
return element;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Создание записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Create(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
_logger.LogInformation("Create. Stop.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_stopStorage.Insert(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Insert operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Изменение записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Update(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model);
|
|
|
|
|
_logger.LogInformation("Update. Stop.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_stopStorage.Update(model) == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Update operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление записи
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public bool Delete(StopBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
CheckModel(model, false);
|
|
|
|
|
_logger.LogInformation("Delete. Stop.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
if (_stopStorage.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. Stop");
|
|
|
|
|
|
|
|
|
|
if (_stopStorage.Delete() == null)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning("Delete operation failed");
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Удаление всех записей
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
public int Clear()
|
|
|
|
|
{
|
|
|
|
|
int count = _stopStorage.Clear();
|
|
|
|
|
_logger.LogInformation("Clear. Delete {Count} Stops", count);
|
|
|
|
|
return count;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-02 18:46:47 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Проверка модели
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="model"></param>
|
|
|
|
|
/// <param name="withParams"></param>
|
|
|
|
|
private void CheckModel(StopBindingModel 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.Street))
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Не указано название улицы", nameof(model.Street));
|
|
|
|
|
}
|
|
|
|
|
if (model.Number <= 0)
|
|
|
|
|
{
|
|
|
|
|
throw new ArgumentNullException("Не указан номер дома", nameof(model.Number));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_logger.LogInformation("CheckModel. Stop.Id: {Id}", model.Id);
|
|
|
|
|
|
|
|
|
|
var element = _stopStorage.GetElement(new StopSearchModel
|
|
|
|
|
{
|
|
|
|
|
Name = model.Name
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new InvalidOperationException("Остановка с таким названием уже существует");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|