108 lines
4.0 KiB
C#
108 lines
4.0 KiB
C#
using BeautySalonContracts.BindingModels;
|
||
using BeautySalonContracts.BusinessLogicsContracts;
|
||
using BeautySalonContracts.SearchModels;
|
||
using BeautySalonContracts.StoragesContracts;
|
||
using BeautySalonContracts.ViewModels;
|
||
using Microsoft.Extensions.Logging;
|
||
|
||
namespace BeautySalonBusinessLogic.BusinessLogics
|
||
{
|
||
public class MasterLogic : IMasterLogic
|
||
{
|
||
private readonly ILogger _logger;
|
||
private readonly IMasterStorage _masterStorage;
|
||
public MasterLogic(ILogger<MasterLogic> logger, IMasterStorage masterStorage)
|
||
{
|
||
_logger = logger;
|
||
_masterStorage = masterStorage;
|
||
}
|
||
public List<MasterViewModel>? ReadList(MasterSearchModel? model)
|
||
{
|
||
_logger.LogInformation("ReadList. MasterFIO:{MasterFIO}. Id:{ Id}", model?.MasterFIO, model?.Id);
|
||
var list = model == null ? _masterStorage.GetFullList() : _masterStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
_logger.LogWarning("ReadList return null list");
|
||
return null;
|
||
}
|
||
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
||
return list;
|
||
}
|
||
public MasterViewModel? ReadElement(MasterSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
_logger.LogInformation("ReadElement. MasterFIO:{MasterFIO}. Id:{ Id}", model.MasterFIO, model.Id);
|
||
var element = _masterStorage.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(MasterBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_masterStorage.Insert(model) == null)
|
||
{
|
||
_logger.LogWarning("Insert operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Update(MasterBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_masterStorage.Update(model) == null)
|
||
{
|
||
_logger.LogWarning("Update operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
public bool Delete(MasterBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
||
if (_masterStorage.Delete(model) == null)
|
||
{
|
||
_logger.LogWarning("Delete operation failed");
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
private void CheckModel(MasterBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.MasterFIO))
|
||
{
|
||
throw new ArgumentNullException("Нет ФИО мастера", nameof(model.MasterFIO));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Specialization))
|
||
{
|
||
throw new ArgumentNullException("Нет специализации мастера", nameof(model.MasterFIO));
|
||
}
|
||
_logger.LogInformation("Master. MasterFIO:{MasterFIO}. Specialization:{ Specialization}. Id: { Id} ", model.MasterFIO, model.Specialization, model.Id);
|
||
var element = _masterStorage.GetElement(new MasterSearchModel
|
||
{
|
||
MasterFIO = model.MasterFIO
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Мастер с таким ФИО уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|