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 MakeLogic : IMakeLogic { private readonly IMakeStorage _makeStorage; public MakeLogic(IMakeStorage storage) { _makeStorage = storage; } public List? ReadList(MakeSearch? model) { var list = model == null ? _makeStorage.GetFullList() : _makeStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public MakeView? ReadElement(MakeSearch model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _makeStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(MakeDto model) { CheckModel(model); if (_makeStorage.Insert(model) == null) { return false; } return true; } public bool Update(MakeDto model) { CheckModel(model); if (_makeStorage.Update(model) == null) { return false; } return true; } public bool Delete(MakeDto model) { CheckModel(model, false); if (_makeStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(MakeDto model, bool withParams = true) { if (model == null) throw new ArgumentNullException(nameof(model)); if (!withParams) return; if (string.IsNullOrEmpty(model.Name)) throw new InvalidOperationException(); var element = _makeStorage.GetElement(new MakeSearch { Name = model.Name, }); if (element != null && element.Id != model.Id) throw new InvalidOperationException(); } } }