2023-10-24 22:46:56 +04:00
|
|
|
|
using ClientsContracts.BindingModels;
|
|
|
|
|
using ClientsContracts.BusinessLogicContracts;
|
|
|
|
|
using ClientsContracts.StorageContracts;
|
|
|
|
|
using ClientsContracts.ViewModels;
|
|
|
|
|
|
|
|
|
|
namespace ClientBusinessLogic.BusinessLogics
|
|
|
|
|
{
|
|
|
|
|
public class ClientLogic : IClientLogic
|
|
|
|
|
{
|
|
|
|
|
private readonly IClientStorage _clientStorage;
|
2023-10-31 22:44:35 +04:00
|
|
|
|
|
2023-10-24 22:46:56 +04:00
|
|
|
|
public ClientLogic(IClientStorage clientStorage)
|
|
|
|
|
{
|
|
|
|
|
_clientStorage = clientStorage;
|
|
|
|
|
}
|
|
|
|
|
public void CreateOrUpdate(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var element = _clientStorage.GetElement(
|
|
|
|
|
new ClientBindingModel
|
|
|
|
|
{
|
|
|
|
|
Reviews = model.Reviews,
|
|
|
|
|
Name = model.Name,
|
|
|
|
|
Status = model.Status,
|
|
|
|
|
Amount = model.Amount
|
|
|
|
|
});
|
|
|
|
|
if (element != null && element.Id != model.Id)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Клиент с таким именем уже существует");
|
|
|
|
|
}
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
_clientStorage.Update(model);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_clientStorage.Insert(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Delete(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
var element = _clientStorage.GetElement(new ClientBindingModel { Id = model.Id });
|
|
|
|
|
if (element == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception("Клиент не найден");
|
|
|
|
|
}
|
|
|
|
|
_clientStorage.Delete(model);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public List<ClientViewModel> Read(ClientBindingModel model)
|
|
|
|
|
{
|
|
|
|
|
if (model == null)
|
|
|
|
|
{
|
|
|
|
|
return _clientStorage.GetFullList();
|
|
|
|
|
}
|
|
|
|
|
if (model.Id.HasValue)
|
|
|
|
|
{
|
|
|
|
|
return new List<ClientViewModel> { _clientStorage.GetElement(model) };
|
|
|
|
|
}
|
|
|
|
|
return _clientStorage.GetFilteredList(model);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|