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 CarLogic : ICarLogic { private readonly ICarStorage _carStorage; public CarLogic(ICarStorage storage) { _carStorage = storage; } public List? ReadList(CarSearch? model) { var list = model == null ? _carStorage.GetFullList() : _carStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public CarView? ReadElement(CarSearch model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _carStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(CarDto model) { model.IsSaled = false; CheckModel(model); if (_carStorage.Insert(model) == null) { return false; } return true; } public bool Update(CarDto model) { CheckModel(model); if (_carStorage.Update(model) == null) { return false; } return true; } public bool Delete(CarDto model) { CheckModel(model, false); if (_carStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(CarDto model, bool withParams = true) { if (model == null) throw new ArgumentNullException(nameof(model)); if (!withParams) return; if (model.ModelId < 0) throw new InvalidOperationException(); if (string.IsNullOrEmpty(model.Color)) throw new InvalidOperationException(); } } }