SUBD/CarRentBusinessLogic/BusinessLogics/ClientLogic.cs

120 lines
2.9 KiB
C#
Raw Normal View History

using CarRentContracts.BindingModels;
using CarRentContracts.BusinessLogicContracts;
using CarRentContracts.SearchModels;
using CarRentContracts.StoragesContracts;
using CarRentContracts.ViewModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ClientRentBusinessLogic.BusinessLogics
{
public class ClientLogic : IClientLogic
{
private readonly IClientStorage _ClientStorage;
public ClientLogic(IClientStorage ClientStorage)
{
_ClientStorage = ClientStorage;
}
public List<ClientViewModel>? ReadList(ClientSearchModel? model)
{
var list = model == null ? _ClientStorage.GetFullList() :
_ClientStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
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 bool Create(ClientBindingModel model)
{
CheckModel(model);
if (_ClientStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(ClientBindingModel model)
{
CheckModel(model);
if (_ClientStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(ClientBindingModel model)
{
CheckModel(model, false);
if (_ClientStorage.Delete(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.Name))
{
throw new ArgumentNullException("Нет имени клиента",
nameof(model.Name));
}
if (string.IsNullOrEmpty(model.Surname))
{
throw new ArgumentNullException("Нет фамилии клиента",
nameof(model.Surname));
}
if (string.IsNullOrEmpty(model.PhoneNumber))
{
throw new ArgumentNullException("Нет номера телефона клиента",
nameof(model.PhoneNumber));
}
if (string.IsNullOrEmpty(model.Email))
{
throw new ArgumentNullException("Нет адреса электронной почты клиента",
nameof(model.Email));
}
if (string.IsNullOrEmpty(model.DriveLicenseNumber))
{
throw new ArgumentNullException("Нет водительских прав клиента",
nameof(model.DriveLicenseNumber));
}
var element = _ClientStorage.GetElement(
new ClientSearchModel
{
DriveLicenseNumber = model.DriveLicenseNumber
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Клиент с такими водительскими правами уже есть");
}
}
}
}