113 lines
3.3 KiB
C#
113 lines
3.3 KiB
C#
using RestaurantContracts.BindingModels;
|
||
using RestaurantContracts.BusinessLogicsContracts;
|
||
using RestaurantContracts.SearchModels;
|
||
using RestaurantContracts.StoragesContracts;
|
||
using RestaurantContracts.ViewModels;
|
||
using System;
|
||
using System.Collections.Generic;
|
||
using System.Linq;
|
||
using System.Text;
|
||
using System.Threading.Tasks;
|
||
|
||
namespace RestaurantBusinessLogic.BusinessLogics
|
||
{
|
||
public class ClientLogic : IClientLogic
|
||
{
|
||
|
||
private readonly IClientStorage _clientStorage;
|
||
|
||
public ClientLogic(IClientStorage clientStorage)
|
||
{
|
||
_clientStorage = clientStorage;
|
||
}
|
||
|
||
public bool Create(ClientBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_clientStorage.Insert(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public bool Delete(ClientBindingModel model)
|
||
{
|
||
CheckModel(model, false);
|
||
if (_clientStorage.Delete(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
public ClientViewModel? ReadElement(ClientSearchModel model)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
var element = _clientStorage.GetElement(model);
|
||
if (element == null)
|
||
{
|
||
return null;
|
||
}
|
||
return element;
|
||
}
|
||
|
||
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
|
||
{
|
||
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
|
||
if (list == null)
|
||
{
|
||
return null;
|
||
}
|
||
return list;
|
||
}
|
||
|
||
public bool Update(ClientBindingModel model)
|
||
{
|
||
CheckModel(model);
|
||
if (_clientStorage.Update(model) == null)
|
||
{
|
||
return false;
|
||
}
|
||
return true;
|
||
}
|
||
|
||
private void CheckModel(ClientBindingModel model, bool withParams = true)
|
||
{
|
||
if (model == null)
|
||
{
|
||
throw new ArgumentNullException(nameof(model));
|
||
}
|
||
if (!withParams)
|
||
{
|
||
return;
|
||
}
|
||
if (string.IsNullOrEmpty(model.FirstName))
|
||
{
|
||
throw new ArgumentNullException("Нет имени клиента", nameof(model.FirstName));
|
||
}
|
||
if (string.IsNullOrEmpty(model.LastName))
|
||
{
|
||
throw new ArgumentNullException("Нет фамилии клиента", nameof(model.LastName));
|
||
}
|
||
if (string.IsNullOrEmpty(model.Number))
|
||
{
|
||
throw new ArgumentNullException("Нет номера клиента", nameof(model.Number));
|
||
}
|
||
var element = _clientStorage.GetElement(new ClientSearchModel
|
||
{
|
||
FirstName = model.FirstName,
|
||
LastName = model.LastName,
|
||
Number = model.Number,
|
||
});
|
||
if (element != null && element.Id != model.Id)
|
||
{
|
||
throw new InvalidOperationException("Клиент с такими параметрами уже есть");
|
||
}
|
||
}
|
||
}
|
||
}
|