SUBD_PIbd-21_Balberova_D.N./BeautySaloon/BeautySaloonBusinessLogic/ServiceLogic.cs
2023-03-28 17:32:55 +04:00

96 lines
2.8 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 BeautySaloonContracts.BindingModels;
using BeautySaloonContracts.BusinessLogicsContracts;
using BeautySaloonContracts.SearchModels;
using BeautySaloonContracts.StoragesContracts;
using BeautySaloonContracts.ViewModels;
namespace BeautySaloonBusinessLogic
{
public class ServiceLogic : IServiceLogic
{
private readonly IServiceStorage _serviceStorage;
public ServiceLogic(IServiceStorage serviceStorage)
{
_serviceStorage = serviceStorage;
}
public bool Create(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Delete(ServiceBindingModel model)
{
CheckModel(model, false);
if (_serviceStorage.Delete(model) == null)
{
return false;
}
return true;
}
public ServiceViewModel? ReadElement(ServiceSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _serviceStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public List<ServiceViewModel>? ReadList(ServiceSearchModel? model)
{
var list = model == null ? _serviceStorage.GetFullList() :
_serviceStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public bool Update(ServiceBindingModel model)
{
CheckModel(model);
if (_serviceStorage.Update(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ServiceBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Name))
{
throw new ArgumentNullException("Нет названия у услуги", nameof(model.Name));
}
var element = _serviceStorage.GetElement(new ServiceSearchModel
{
Name = model.Name
});
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Услуга с таким названием уже есть");
}
}
}
}