PIbd-21_MasenkinMS_SUBD_Rou.../RouteGuide/RouteGuideBusinessLogics/BusinessLogics/ScheduleLogic.cs
2024-04-02 18:46:47 +04:00

165 lines
5.1 KiB
C#

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 ScheduleLogic : IScheduleLogic
{
/// <summary>
/// Логгер
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// Хранилище
/// </summary>
private readonly IScheduleStorage _scheduleStorage;
/// <summary>
/// Конструктор
/// </summary>
/// <param name="logger"></param>
/// <param name="scheduleStorage"></param>
public ScheduleLogic(ILogger<ScheduleLogic> logger, IScheduleStorage scheduleStorage)
{
_logger = logger;
_scheduleStorage = scheduleStorage;
}
/// <summary>
/// Получение списка
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
public List<ScheduleViewModel>? 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;
}
/// <summary>
/// Получение отдельной записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <exception cref="ArgumentNullException"></exception>
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;
}
/// <summary>
/// Создание записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Изменение записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Удаление записи
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
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;
}
/// <summary>
/// Проверка модели
/// </summary>
/// <param name="model"></param>
/// <param name="withParams"></param>
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);
}
}
}