SUBD/CarRentBusinessLogic/BusinessLogics/RentalLogic.cs

104 lines
2.3 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 CarRentBusinessLogic.BusinessLogics
{
public class RentalLogic : IRentalLogic
{
private readonly IRentalStorage _rentalStorage;
public RentalLogic(IRentalStorage RentalStorage)
{
_rentalStorage = RentalStorage;
}
public string TestInsertList(int num,
List<ClientViewModel> clients,
List<CarViewModel> cars)
{
return _rentalStorage.TestInsertList(num, clients, cars);
}
public string TestReadList(int num)
{
return _rentalStorage.TestReadList(num);
}
public List<RentalViewModel>? ReadList(RentalSearchModel? model)
{
var list = model == null ? _rentalStorage.GetFullList() :
_rentalStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public RentalViewModel? ReadElement(RentalSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _rentalStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(RentalBindingModel model)
{
CheckModel(model);
if (_rentalStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(RentalBindingModel model)
{
CheckModel(model);
if (_rentalStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(RentalBindingModel model)
{
CheckModel(model, false);
if (_rentalStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(RentalBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (model.RentalCost < 0)
{
throw new ArgumentNullException("Цена аренды должна быть больше 0",
nameof(model.RentalCost));
}
if (model.StartDate > DateTime.Now)
{
throw new ArgumentNullException("Дата указана неверно",
nameof(model.StartDate));
}
}
}
}