129 lines
3.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 HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace HotelBusinessLogic.BusinessLogic
{
public class ParticipantLogic : IParticipantLogic
{
private readonly ILogger _logger;
private readonly IParticipantStorage _participantStorage;
public ParticipantLogic(ILogger<ParticipantLogic> logger, IParticipantStorage participantStorage)
{
_logger = logger;
_participantStorage = participantStorage;
}
public List<ParticipantViewModel>? ReadList(ParticipantSearchModel? model)
{
_logger.LogInformation("ReadList. FIO:{FIO}.Id:{Id}", model?.ParticipantFIO, model?.Id);
var list = model == null ? _participantStorage.GetFullList() : _participantStorage.GetFilteredList(model);
if (list == null)
{
_logger.LogInformation("ReadList return null list");
return null;
}
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
return list;
}
public ParticipantViewModel? ReadElement(ParticipantSearchModel? model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
_logger.LogInformation("ReadElement.FIO:{FIO}.Id:{Id}", model.ParticipantFIO, model.Id);
var element = _participantStorage.GetElement(model);
if (element == null)
{
_logger.LogInformation("ReadElement element not found");
return null;
}
_logger.LogInformation("ReadElement find. Id:{Id}", element.Id);
return element;
}
public bool Create(ParticipantBindingModel model)
{
CheckModel(model);
var result = _participantStorage.Insert(model);
if (result == null)
{
_logger.LogWarning("Insert error");
return false;
}
return true;
}
public bool Update(ParticipantBindingModel model)
{
CheckModel(model);
if (_participantStorage.Update(model) == null)
{
_logger.LogWarning("Update error");
return false;
}
return true;
}
public bool Delete(ParticipantBindingModel model)
{
CheckModel(model, false);
if (_participantStorage.Delete(model) == null)
{
_logger.LogWarning("Delete error");
return false;
}
return true;
}
public void CheckModel (ParticipantBindingModel model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.ParticipantFIO))
throw new ArgumentNullException("Нет ФИО участника", nameof(model.ParticipantFIO));
if (string.IsNullOrEmpty (model.Number))
throw new ArgumentNullException("Нет номера участника", nameof(model.Number));
var elementName = _participantStorage.GetElement(new ParticipantSearchModel
{
ParticipantFIO = model.ParticipantFIO
});
if (elementName != null && elementName.Id != model.Id)
{
throw new InvalidOperationException("Участник с таким именем уже есть");
}
_logger.LogInformation("Participant. FIO:{FIO}.Number:{Number}. Id:{Id}", model.ParticipantFIO, model.Number, model.Id);
}
}
}