PIbd-22_Bazunov_AI_Hotel/Hotel/HotelBusinessLogic/BusinessLogics/MaitreLogic.cs
2023-04-07 00:11:08 +04:00

88 lines
2.9 KiB
C#

using HotelContracts.BindingModels;
using HotelContracts.BusinessLogicsContracts;
using HotelContracts.SearchModels;
using HotelContracts.StoragesContracts;
using HotelContracts.ViewModels;
using Microsoft.Extensions.Logging;
namespace HotelBusinessLogic.BusinessLogics;
public class MaitreLogic : IMaitreLogic
{
private readonly IMaitreStorage _maitreStorage;
private readonly ILogger _logger;
public MaitreLogic(IMaitreStorage maitreLogic, ILogger<MaitreLogic> logger)
{
_logger = logger;
_maitreStorage = maitreLogic;
}
public List<MaitreViewModel>? ReadList(MaitreSearchModel? model)
{
var list = model == null ? _maitreStorage.GetFullList() : _maitreStorage.GetFilteredList(model);
_logger.LogInformation("ReadList .Count:{Count}", list.Count);
return list;
}
public MaitreViewModel? ReadElement(MaitreSearchModel model)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
_logger.LogInformation("ReadElement .Id:{Id}", model.Id);
var element = _maitreStorage.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(MaitreBindingModel model)
{
CheckModel(model);
if (_maitreStorage.Insert(model) != null) return true;
_logger.LogWarning("Insert operation failed");
return false;
}
public bool Update(MaitreBindingModel model)
{
CheckModel(model);
if (_maitreStorage.Update(model) != null) return true;
_logger.LogWarning("Update operation failed");
return false;
}
public bool Delete(MaitreBindingModel model)
{
CheckModel(model, false);
_logger.LogInformation("Delete .Id:{Id}", model.Id);
if (_maitreStorage.Delete(model) != null) return true;
_logger.LogWarning("Delete operation failed");
return false;
}
private void CheckModel(MaitreBindingModel? model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new ArgumentException("Name must be not null");
if (string.IsNullOrEmpty(model.SecondName))
throw new ArgumentException("Second name must be not null");
if (string.IsNullOrEmpty(model.LastName))
throw new ArgumentException("Last name must be not null");
var Maitre = _maitreStorage.GetElement(new MaitreSearchModel {
Login = model.Login
});
if (Maitre != null && Maitre.Id != model.Id)
{
throw new InvalidOperationException("Login is already exists");
}
}
}