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

120 lines
4.1 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 TripLogic : ITripLogic
{
private readonly ILogger _logger;
private readonly ITripStorage _tripStorage;
public TripLogic(ILogger<TripLogic> logger, ITripStorage tripStorage)
{
_logger = logger;
_tripStorage = tripStorage;
}
public List<TripViewModel>? ReadList(TripSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, TripName: {TripName}, GuideId: {GuideId}.", model?.Id, model?.TripName, model?.GuideId);
var list = (model == null) ? _tripStorage.GetFullList() : _tripStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public TripViewModel? ReadElement(TripSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. Trip id: {Id}, Trip name: {TripName}", model?.Id, model?.TripName);
var element = _tripStorage.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(TripBindingModel model)
{
CheckModel(model);
if (_tripStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(TripBindingModel model)
{
CheckModel(model);
if (_tripStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(TripBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_tripStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(TripBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.TripName))
{
throw new ArgumentNullException("Нет названия поездки", nameof(model.TripName));
}
if (model.TripDate == DateTime.MinValue)
{
throw new ArgumentNullException("Нет даты поездки", nameof(model.TripDate));
}
if (model.GuideId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор гида", nameof(model.GuideId));
}
_logger.LogInformation("Trip. Id: {id}, TripName: {TripName}, GuideId: {GuideId}", model.Id, model.TripName, model.GuideId);
var element = _tripStorage.GetElement(new TripSearchModel
{
TripName = model.TripName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Поездка с таким названием уже есть");
}
}
}
}