SUBD_PIbd-23_ZakharovRA/CarShowroom/CarShowroomBusinessLogic/BusinessLogic/ClientLogic.cs

96 lines
2.6 KiB
C#

using CarShowroomContracts.BusinessLogic;
using CarShowroomContracts.StorageContracts;
using CarShowroomDataModels.Dtos;
using CarShowroomDataModels.SearchModel;
using CarShowroomDataModels.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CarShowroomBusinessLogic.BusinessLogic
{
public class ClientLogic : IClientLogic
{
private readonly IClientStorage _clientStorage;
public ClientLogic(IClientStorage storage)
{
_clientStorage = storage;
}
public List<ClientView>? ReadList(ClientSearch? model)
{
var list = model == null ? _clientStorage.GetFullList() : _clientStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public ClientView? ReadElement(ClientSearch model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _clientStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(ClientDto model)
{
CheckModel(model);
if (_clientStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ClientDto model)
{
CheckModel(model);
if (_clientStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ClientDto model)
{
CheckModel(model, false);
if (_clientStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(ClientDto model, bool withParams = true)
{
if (model == null)
throw new ArgumentNullException(nameof(model));
if (!withParams)
return;
if (string.IsNullOrEmpty(model.Name))
throw new InvalidOperationException();
if (string.IsNullOrEmpty(model.PhoneNumber))
throw new InvalidOperationException();
var element = _clientStorage.GetElement(new ClientSearch
{
PhoneNumber = model.PhoneNumber,
});
if (element != null && element.Id != model.Id)
throw new InvalidOperationException();
}
}
}