CourseWork_Hotel/Hotel/HotelBusinessLogic/BusinessLogics/ConferenceLogic.cs

121 lines
3.6 KiB
C#
Raw Normal View History

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
2023-04-06 18:27:12 +04:00
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
2023-04-06 18:27:12 +04:00
using Microsoft.Extensions.Logging;
namespace HotelBusinessLogic.BusinessLogics
{
public class ConferenceLogic : IConferenceLogic
{
2023-04-06 18:27:12 +04:00
private readonly ILogger _logger;
private readonly IConferenceStorage _conferenceStorage;
public ConferenceLogic(ILogger<ConferenceLogic> logger, IConferenceStorage conferenceStorage)
{
_logger = logger;
_conferenceStorage = conferenceStorage;
}
public bool Create(ConferenceBindingModel model)
{
2023-04-06 18:27:12 +04:00
CheckModel(model);
if (_conferenceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert operation failed");
return false;
}
return true;
}
public bool Delete(ConferenceBindingModel model)
{
2023-04-06 18:27:12 +04:00
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_conferenceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete operation failed");
return false;
}
return true;
}
public ConferenceViewModel? ReadElement(ConferenceSearchModel model)
{
2023-04-06 18:27:12 +04:00
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement. ConferenceName:{ConferenceName}.Id:{Id}", model.ConferenceName, model.Id);
var element = _conferenceStorage.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<ConferenceViewModel>? ReadList(ConferenceSearchModel? model)
{
2023-04-06 18:27:12 +04:00
_logger.LogInformation("ReadList. ConferenceName:{ConferenceName}.Id:{ Id}", model?.ConferenceName, model?.Id);
var list = model == null ? _conferenceStorage.GetFullList() : _conferenceStorage.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(ConferenceBindingModel model)
{
2023-04-06 18:27:12 +04:00
CheckModel(model);
if (_conferenceStorage.Update(model) == null)
{
_logger.LogWarning("Update operation failed");
return false;
}
return true;
}
private void CheckModel(ConferenceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.ConferenceName))
{
throw new ArgumentNullException("Нет названия конференции", nameof(model.ConferenceName));
}
_logger.LogInformation("Conference. ConferenceName:{ConferenceName}.StartDate:{ StartDate}. Id: { Id}", model.ConferenceName, model.StartDate, model.Id);
}
}
}