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
{
///
/// Бизнес-логика для сущности "Расписание"
///
public class ScheduleLogic : IScheduleLogic
{
///
/// Логгер
///
private readonly ILogger _logger;
///
/// Хранилище
///
private readonly IScheduleStorage _scheduleStorage;
///
/// Конструктор
///
///
///
public ScheduleLogic(ILogger logger, IScheduleStorage scheduleStorage)
{
_logger = logger;
_scheduleStorage = scheduleStorage;
}
///
/// Получение списка
///
///
///
public List? ReadList(ScheduleSearchModel? model)
{
_logger.LogInformation("ReadList. Schedule.{Id}", model?.Id);
var list = model == null ? _scheduleStorage.GetFullList() : _scheduleStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList. Returned null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
///
/// Получение отдельной записи
///
///
///
///
public ScheduleViewModel? ReadElement(ScheduleSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Schedule.{Id}", model?.Id);
var element = _scheduleStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement. Element not found");
return null;
}
_logger.LogInformation("ReadElement. Find Schedule.Id: {Id}", element.Id);
return element;
}
///
/// Создание записи
///
///
///
public bool Create(ScheduleBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Create. Schedule.Id: {Id}", model.Id);
if (_scheduleStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
///
/// Изменение записи
///
///
///
public bool Update(ScheduleBindingModel model)
{
CheckModel(model);
_logger.LogInformation("Update. Schedule.Id: {Id}", model.Id);
if (_scheduleStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
///
/// Удаление записи
///
///
///
public bool Delete(ScheduleBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Schedule.Id: {Id}", model.Id);
if (_scheduleStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
///
/// Проверка модели
///
///
///
private void CheckModel(ScheduleBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.RouteId < 0)
{
throw new ArgumentNullException("Некорректный идентификатор маршрута", nameof(model.RouteId));
}
_logger.LogInformation("CheckModel. Schedule.Id: {Id}", model.Id);
}
}
}