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

120 lines
4.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 TravelAgencyContracts.BindingModels;
using TravelAgencyContracts.BusinessLogicsContracts;
using TravelAgencyContracts.SearchModels;
using TravelAgencyContracts.StoragesContracts;
using TravelAgencyContracts.ViewModels;
namespace TravelAgencyBusinessLogic.BusinessLogics
{
public class ExcursionGroupLogic : IExcursionGroupLogic
{
private readonly ILogger _logger;
private readonly IExcursionGroupStorage _excursionGroupStorage;
public ExcursionGroupLogic(ILogger<ExcursionGroupLogic> logger, IExcursionGroupStorage excursionGroupStorage)
{
_logger = logger;
_excursionGroupStorage = excursionGroupStorage;
}
public List<ExcursionGroupViewModel>? ReadList(ExcursionGroupSearchModel? model)
{
_logger.LogInformation("ReadList. Id: {Id}, ExcursionGroupName: {ExcursionGroupName}, UserId: {UserId}, GuideId: {GuideId}.", model?.Id, model?.ExcursionGroupName, model?.UserId, model?.GuideId);
var list = (model == null) ? _excursionGroupStorage.GetFullList() : _excursionGroupStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogWarning("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count: {Count}", list.Count);
return list;
}
public ExcursionGroupViewModel? ReadElement(ExcursionGroupSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ExcursionGroup id: {Id}, ExcursionGroup name: {ExcursionGroupName}", model?.Id, model?.ExcursionGroupName);
var element = _excursionGroupStorage.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(ExcursionGroupBindingModel model)
{
CheckModel(model);
if (_excursionGroupStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Update(ExcursionGroupBindingModel model)
{
CheckModel(model);
if (_excursionGroupStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
public bool Delete(ExcursionGroupBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id: {Id}", model.Id);
if (_excursionGroupStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
private void CheckModel(ExcursionGroupBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ExcursionGroupName))
{
throw new ArgumentNullException("Нет названия группы", nameof(model.ExcursionGroupName));
}
if (model.ParticipantsAmount <= 0)
{
throw new ArgumentNullException("Количество участников быть больше 0", nameof(model.ParticipantsAmount));
}
if (model.UserId <= 0)
{
throw new ArgumentNullException("Некорректный идентификатор пользователя", nameof(model.UserId));
}
_logger.LogInformation("ExcursionGroup. Id: {id}, ExcursionGroupName: {ExcursionGroupName}, ParticipantsAmount: {ParticipantsAmount}, UserId: {UserId}", model.Id, model.ExcursionGroupName, model.ParticipantsAmount, model.UserId);
var element = _excursionGroupStorage.GetElement(new ExcursionGroupSearchModel
{
ExcursionGroupName = model.ExcursionGroupName
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Группа с таким названием уже есть");
}
}
}
}