PIbd-21_Anisin_R.S._CourseW.../TravelAgency/TravelAgencyBusinessLogic/BusinessLogics/TourLogic.cs
2024-04-29 19:16:39 +04:00

124 lines
4.3 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 TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class TourLogic : ITourLogic
{
private readonly ILogger _logger;
private readonly ITourStorage _tourStorage;
public TourLogic(ILogger<TourLogic> logger, ITourStorage tourStorage)
{
_logger = logger;
_tourStorage = tourStorage;
}
public List<TourViewModel>? ReadList(TourSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, TourName: {TourName}, UserId: {UserId}.", model?.Id, model?.TourName, model?.UserId);
var list = (model == null) ? _tourStorage.GetFullList() : _tourStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public TourViewModel? ReadElement(TourSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Tour id: {Id}, Tour name: {TourName}", model?.Id, model?.TourName);
var element = _tourStorage.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(TourBindingModel model)
{
CheckModel(model);
if (_tourStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TourBindingModel model)
{
CheckModel(model);
if (_tourStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TourBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_tourStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TourBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TourName))
{
throw new ArgumentNullException("Нет названия тура", nameof(model.TourName));
}
if (model.TourDate == DateTime.MinValue)
{
throw new ArgumentNullException("Нет даты тура", nameof(model.TourDate));
}
if (model.Price <= 0)
{
throw new ArgumentNullException("Цена тура должна быть больше 0", nameof(model.Price));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("Tour. Id: {id}, TourName: {TourName}, Price: {Price}, UserId: {UserId}", model.Id, model.TourName, model.Price, model.UserId);
var element = _tourStorage.GetElement(new TourSearchModel
{
TourName = model.TourName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Тур с таким названием уже есть");
}
}
}
}