151 lines
3.8 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using HotelDataModels.Models;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
namespace HotelBusinessLogic.BusinessLogic
{
public class ConferenceLogic : IConferenceLogic
{
private readonly ILogger _logger;
private readonly IConferenceStorage _conferenceStorage;
public ConferenceLogic(ILogger<ConferenceLogic> logger, IConferenceStorage conferenceStorage)
{
_logger = logger;
_conferenceStorage = conferenceStorage;
}
public List<ConferenceViewModel>? ReadList(ConferenceSearchModel? model)
{
_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 ConferenceViewModel? ReadElement(ConferenceSearchModel? model)
{
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 fing. Id:{id}", element.Id);
return element;
}
public bool Create(ConferenceBindingModel model)
{
CheckModel(model);
model.ConferenceParticipants = new();
if (_conferenceStorage.Insert(model) == null)
{
_logger.LogWarning("Insert error");
return false;
}
return true;
}
public bool Update(ConferenceBindingModel model)
{
CheckModel(model);
if (_conferenceStorage.Update(model) == null)
{
_logger.LogWarning("Update error");
return false;
}
return true;
}
public bool Delete(ConferenceBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete. Id:{Id}", model.Id);
if (_conferenceStorage.Delete(model) == null)
{
_logger.LogWarning("Delete error");
return false;
}
return true;
}
public bool AddParticipantToConference(ConferenceSearchModel model, IParticipantModel participant)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("AddParticipantToConference. ConferenceName:{ConferenceName}.Id:{ Id}", model.ConferenceName, model.Id);
var element = _conferenceStorage.GetElement(model);
if(element == null)
{
_logger.LogWarning("AddParticipantToConference element not found");
return false;
}
_logger.LogInformation("AddParticipantToConference find. Id:{Id}", element.Id);
element.ConferenceParticipants[participant.Id] = participant;
_conferenceStorage.Update(new()
{
Id = element.Id,
ConferenceName = element.ConferenceName,
Subject = element.Subject,
OrganiserId = element.OrganiserId,
ConferenceParticipants = element.ConferenceParticipants,
});
return true;
}
public 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 ArgumentException("Нет названия конференции", nameof(model.ConferenceName));
_logger.LogInformation("Conference. ConferenceName:{ConferenceName}. Id: { Id}", model.ConferenceName, model.Id);
}
}
}