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 EmployeeLogic : IEmployeeLogic { private readonly IEmployeeStorage _employeeStorage; public EmployeeLogic(IEmployeeStorage storage) { _employeeStorage = storage; } public List? ReadList(EmployeeSearch? model) { var list = model == null ? _employeeStorage.GetFullList() : _employeeStorage.GetFilteredList(model); if (list == null) { return null; } return list; } public EmployeeView? ReadElement(EmployeeSearch model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } var element = _employeeStorage.GetElement(model); if (element == null) { return null; } return element; } public bool Create(EmployeeDto model) { CheckModel(model); if (_employeeStorage.Insert(model) == null) { return false; } return true; } public bool Update(EmployeeDto model) { CheckModel(model); if (_employeeStorage.Update(model) == null) { return false; } return true; } public bool Delete(EmployeeDto model) { CheckModel(model, false); if (_employeeStorage.Delete(model) == null) { return false; } return true; } private void CheckModel(EmployeeDto model, bool withParams = true) { if (model == null) throw new ArgumentNullException(nameof(model)); if (!withParams) return; if (string.IsNullOrEmpty(model.Name)) throw new InvalidOperationException(); if (string.IsNullOrEmpty(model.Email)) throw new InvalidOperationException(); if (string.IsNullOrEmpty(model.Password)) throw new InvalidOperationException(); var element = _employeeStorage.GetElement(new EmployeeSearch { Email = model.Email, }); if (element != null && element.Id != model.Id) throw new InvalidOperationException(); } } }