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

63 lines
1.5 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 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);
}
}
}