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 ServiceLogic : IServiceLogic { private readonly IServiceStorage _serviceStorage; public ServiceLogic(IServiceStorage storage) { _serviceStorage = storage; } public List? ReadList(ServiceSearch? model) { var list = model == null ? _serviceStorage.GetFullList() : _serviceStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public ServiceView? ReadElement(ServiceSearch model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _serviceStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(ServiceDto model) { CheckModel(model); if (_serviceStorage.Insert(model) == null) { return false; } return true; } public bool Update(ServiceDto model) { CheckModel(model); if (_serviceStorage.Update(model) == null) { return false; } return true; } public bool Delete(ServiceDto model) { CheckModel(model, false); if (_serviceStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(ServiceDto model, bool withParams = true) { if (model == null) throw new ArgumentNullException(nameof(model)); if (!withParams) return; if (string.IsNullOrEmpty(model.Name)) throw new InvalidOperationException(); if (model.Price < 0) throw new InvalidOperationException(); var element = _serviceStorage.GetElement(new ServiceSearch { Name = model.Name, }); if (element != null && element.Id != model.Id) throw new InvalidOperationException(); } } }