PIbd-21_Anisin_R.S._CourseW.../TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/ExcursionLogic.cs

120 lines
4.4 KiB
C#
Raw Normal View History

2024-04-29 19:16:39 +04:00
using Microsoft.Extensions.Logging;
using TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class ExcursionLogic : IExcursionLogic
{
private readonly ILogger _logger;
private readonly IExcursionStorage _excursionStorage;
public ExcursionLogic(ILogger<ExcursionLogic> logger, IExcursionStorage excursionStorage)
{
_logger = logger;
_excursionStorage = excursionStorage;
}
public List<ExcursionViewModel>? ReadList(ExcursionSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, ExcursionName: {ExcursionName}, UserId: {UserId}, PlaceId: {PlaceId}.", model?.Id, model?.ExcursionName, model?.UserId, model?.PlaceId);
2024-04-29 19:16:39 +04:00
var list = (model == null) ? _excursionStorage.GetFullList() : _excursionStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ExcursionViewModel? ReadElement(ExcursionSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Excursion id: {Id}, Excursion name: {ExcursionName}", model?.Id, model?.ExcursionName);
var element = _excursionStorage.GetElement(model!);
if (element == null)
{
_logger.LogWarning("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id: {Id}", element.Id);
return element;
}
public bool Create(ExcursionBindingModel model)
{
CheckModel(model);
if (_excursionStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ExcursionBindingModel model)
{
CheckModel(model);
if (_excursionStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ExcursionBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_excursionStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ExcursionBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ExcursionName))
{
throw new ArgumentNullException("Нет названия экскурсии", nameof(model.ExcursionName));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена экскурсии должна быть больше 0", nameof(model.Price));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("Excursion. Id: {id}, ExcursionName: {ExcursionName}, Price: {Price}, UserId: {UserId}", model.Id, model.ExcursionName, model.Price, model.UserId);
var element = _excursionStorage.GetElement(new ExcursionSearchModel
{
ExcursionName = model.ExcursionName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Экскурсия с таким названием уже есть");
}
}
}
}