SUBD_Labs/BeautySalonBusinessLogic/BusinessLogics/MasterLogic.cs

119 lines
4.1 KiB
C#
Raw Normal View History

2023-05-13 14:11:00 +04:00
using BeautySalonContracts.BindingModels;
using BeautySalonContracts.BusinessLogicsContracts;
using BeautySalonContracts.SearchModels;
using BeautySalonContracts.StoragesContracts;
using BeautySalonContracts.ViewModels;
using Microsoft.Extensions.Logging;
2023-05-17 03:35:13 +04:00
using System;
2023-05-13 14:11:00 +04:00
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));
}
2023-05-13 23:36:15 +04:00
if (model.Wage <= 0)
2023-05-13 14:11:00 +04:00
{
2023-05-13 23:36:15 +04:00
throw new ArgumentNullException("Зарплата должна быть больше 0", nameof(model.Wage));
2023-05-13 14:11:00 +04:00
}
2023-05-13 23:36:15 +04:00
_logger.LogInformation("Master. MasterFIO:{MasterFIO}. Wage:{ Wage}. Id: { Id} ", model.MasterFIO, model.Wage, model.Id);
2023-05-13 14:11:00 +04:00
var element = _masterStorage.GetElement(new MasterSearchModel
{
MasterFIO = model.MasterFIO
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Мастер с таким ФИО уже есть");
}
}
2023-05-17 03:35:13 +04:00
public string TestInsertList(int v)
{
return _masterStorage.TestInsertList(v);
}
public string TestReadList(int v)
{
return _masterStorage.TestReadList(v);
}
2023-05-13 14:11:00 +04:00
}
}