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 SaleLogic : ISaleLogic { private readonly ISaleStorage _saleStorage; public SaleLogic(ISaleStorage storage) { _saleStorage = storage; } public List? ReadList(SaleSearch? model) { var list = model == null ? _saleStorage.GetFullList() : _saleStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public SaleView? ReadElement(SaleSearch model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _saleStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(SaleDto model) { CheckModel(model); model.SaleTime = DateTime.Now; if (_saleStorage.Insert(model) == null) { return false; } return true; } public bool Update(SaleDto model) { CheckModel(model); if (_saleStorage.Update(model) == null) { return false; } return true; } public bool Delete(SaleDto model) { CheckModel(model, false); if (_saleStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(SaleDto model, bool withParams = true) { if (model == null) throw new ArgumentNullException(nameof(model)); if (!withParams) return; if (model.Cost < 0) throw new InvalidOperationException(); if (model.EmployeeId < 0) throw new InvalidOperationException(); if (model.ClientId < 0) throw new InvalidOperationException(); } } }