96 lines
3.3 KiB
C#
96 lines
3.3 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 VisitLogic : IVisitLogic
|
|
{
|
|
private readonly ILogger _logger;
|
|
private readonly IVisitStorage _visitStorage;
|
|
public VisitLogic(ILogger<VisitLogic> logger, IVisitStorage visitStorage)
|
|
{
|
|
_logger = logger;
|
|
_visitStorage = visitStorage;
|
|
}
|
|
public List<VisitViewModel>? ReadList(VisitSearchModel? model)
|
|
{
|
|
_logger.LogInformation("ReadList. VisitId:{VisitId}.", model?.Id);
|
|
var list = model == null ? _visitStorage.GetFullList() : _visitStorage.GetFilteredList(model);
|
|
if (list == null)
|
|
{
|
|
_logger.LogWarning("ReadList return null list");
|
|
return null;
|
|
}
|
|
_logger.LogInformation("ReadList. Count:{Count}", list.Count);
|
|
return list;
|
|
}
|
|
public VisitViewModel? ReadElement(VisitSearchModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
_logger.LogInformation("ReadElement. Id:{ Id}", model.Id);
|
|
var element = _visitStorage.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(VisitBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_visitStorage.Insert(model) == null)
|
|
{
|
|
_logger.LogWarning("Insert operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public bool Update(VisitBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
if (_visitStorage.Update(model) == null)
|
|
{
|
|
_logger.LogWarning("Update operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
public bool Delete(VisitBindingModel model)
|
|
{
|
|
CheckModel(model);
|
|
_logger.LogInformation("Delete. Id:{Id}", model.Id);
|
|
if (_visitStorage.Delete(model) == null)
|
|
{
|
|
_logger.LogWarning("Delete operation failed");
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
private bool CheckModel(VisitBindingModel model)
|
|
{
|
|
if (model == null)
|
|
{
|
|
throw new ArgumentNullException(nameof(model));
|
|
}
|
|
if (model.Count <= 0)
|
|
{
|
|
throw new ArgumentNullException("Количество услуг должно быть больше 0", nameof(model.Count));
|
|
}
|
|
if (model.Sum <= 0)
|
|
{
|
|
throw new ArgumentNullException("Суммарная стоимость должна быть больше 0", nameof(model.Sum));
|
|
}
|
|
return true;
|
|
}
|
|
}
|
|
}
|