PIbd-32_Turner_I.A._COP_10/COP/ClientBusinessLogic/BusinessLogics/ClientLogic.cs

63 lines
1.5 KiB
C#
Raw Normal View History

using ClientsContracts.BindingModels;
using ClientsContracts.BusinessLogicContracts;
using ClientsContracts.StorageContracts;
using ClientsContracts.ViewModels;
namespace ClientBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly IClientStorage _clientStorage;
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);
}
}
}