SUBD/CarRentBusinessLogic/BusinessLogics/CarLogic.cs

129 lines
3.2 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 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 CarLogic : ICarLogic
{
private readonly ICarStorage _carStorage;
public CarLogic(ICarStorage CarStorage)
{
_carStorage = CarStorage;
}
public List<CarViewModel>? ReadList(CarSearchModel? model)
{
var list = model == null ? _carStorage.GetFullList() :
_carStorage.GetFilteredList(model);
if (list == null)
{
return null;
}
return list;
}
public CarViewModel? ReadElement(CarSearchModel model)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
var element = _carStorage.GetElement(model);
if (element == null)
{
return null;
}
return element;
}
public bool Create(CarBindingModel model)
{
CheckModel(model);
if (_carStorage.Insert(model) == null)
{
return false;
}
return true;
}
public bool Update(CarBindingModel model)
{
CheckModel(model);
if (_carStorage.Update(model) == null)
{
return false;
}
return true;
}
public bool Delete(CarBindingModel model)
{
CheckModel(model, false);
if (_carStorage.Delete(model) == null)
{
return false;
}
return true;
}
private void CheckModel(CarBindingModel model, bool withParams = true)
{
if (model == null)
{
throw new ArgumentNullException(nameof(model));
}
if (!withParams)
{
return;
}
if (string.IsNullOrEmpty(model.Brand))
{
throw new ArgumentNullException("Нет производителя авто",
nameof(model.Brand));
}
if (string.IsNullOrEmpty(model.Model))
{
throw new ArgumentNullException("Нет адреса филиала",
nameof(model.Model));
}
if (string.IsNullOrEmpty(model.Color))
{
throw new ArgumentNullException("Нет номера телефона филиала",
nameof(model.Color));
}
if (string.IsNullOrEmpty(model.LicensePlate))
{
throw new ArgumentNullException("Нет номера телефона филиала",
nameof(model.LicensePlate));
}
if(model.RentalCostPerHour < 0)
{
throw new ArgumentNullException("Цена аренды должна быть больше 0",
nameof(model.RentalCostPerHour));
}
if(model.YearOfManuf > DateTime.SpecifyKind(DateTime.Now, DateTimeKind.Utc))
{
throw new ArgumentNullException("Неверно указана дата выпуска автомобиля",
nameof(model.YearOfManuf));
}
if (model.BranchId < 0)
{
throw new ArgumentNullException("Укажите филиал",
nameof(model.BranchId));
}
var element = _carStorage.GetElement(
new CarSearchModel
{
LicensePlate = model.LicensePlate
}
);
if (element != null && element.Id != model.Id)
{
throw new InvalidOperationException("Автомобиль с таким номером уже есть");
}
}
}
}