116 lines
3.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Logging;
using RouteGuideContracts.BindingModels;
using RouteGuideContracts.BusinessLogicContracts;
using RouteGuideContracts.SearchModels;
using RouteGuideContracts.StoragesModels;
using RouteGuideContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RouteGuideBusinessLogic.BusinessLogic
{
public class StopLogic : IStopLogic
{
private readonly ILogger _logger;
private readonly IStopStorage _stopStorage;
public StopLogic(ILogger<StopLogic> logger, IStopStorage stopStorage)
{
_logger = logger;
_stopStorage = stopStorage;
}
public bool Create(StopBindingModel model)
{
CheckModel(model);
if (_stopStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(StopBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_stopStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public StopViewModel? ReadElement(StopSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Name:{Name}.Id:{ Id}", model.Name, model.Id);
var element = _stopStorage.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<StopViewModel>? ReadList(StopSearchModel? model)
{
_logger.LogInformation("ReadList. Name:{Name}.Id:{ Id}", model?.Name, model?.Id);
var list = model == null ? _stopStorage.GetFullList() : _stopStorage.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(StopBindingModel model)
{
CheckModel(model);
if (_stopStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
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));
}
_logger.LogInformation("Stop. Name:{Name}. Id: { Id}", model.Name, model.Id);
var element = _stopStorage.GetElement(new StopSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Stop с таким названием уже есть");
}
}
}
}