resource = $resource; $this->labFactory = $labFactory; $this->labCollectionFactory = $labCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->collectionProcessor = $collectionProcessor; $this->searchCriteriaBuilder = $searchCriteriaBuilder; } /** * @inheritDoc */ public function save(LabInterface $lab) { try { $this->resource->save($lab); } catch (\Exception $exception) { throw new CouldNotSaveException(__( 'Could not save the lab: %1', $exception->getMessage() )); } return $lab; } /** * @inheritDoc */ public function get($labId) { $lab = $this->labFactory->create(); $this->resource->load($lab, $labId); if (!$lab->getId()) { throw new NoSuchEntityException(__('Lab with id "%1" does not exist.', $labId)); } return $lab; } /** * @inheritDoc */ public function getList( \Magento\Framework\Api\SearchCriteriaInterface $criteria ) { $collection = $this->labCollectionFactory->create(); $this->collectionProcessor->process($criteria, $collection); $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($criteria); $items = []; foreach ($collection as $model) { $items[] = $model; } $searchResults->setItems($items); $searchResults->setTotalCount($collection->getSize()); return $searchResults; } /** * @inheritDoc */ public function delete(LabInterface $lab) { try { $labModel = $this->labFactory->create(); $this->resource->load($labModel, $lab->getId()); $this->resource->delete($labModel); } catch (\Exception $exception) { throw new CouldNotDeleteException(__( 'Could not delete the Lab: %1', $exception->getMessage() )); } return true; } /** * @inheritDoc */ public function deleteById($labId) { return $this->delete($this->get($labId)); } /** * @inheirtDoc */ public function getByDealerId(int $customerId): ResourceLab\Collection { $collection = $this->labCollectionFactory->create(); $collection->addFieldToFilter('main_table.customer_id', $customerId) ->getSelect() ->joinLeft('customer_entity', 'customer_entity.entity_id=main_table.customer_id', ['customer_name'=>'firstname']) ->joinLeft('salesperson', 'salesperson.salesperson_id=main_table.salesperson_id', ['salesperson_name'=>'salesperson.name']) ->query(); return $collection; } /** * @inheritDoc */ public function getByLabCode($labCode) { $lab = $this->labFactory->create(); $this->resource->load($lab, $labCode, 'lab_code'); if (!$lab->getId()) { throw new NoSuchEntityException(__('Lab with id "%1" does not exist.', $labCode)); } return $lab; } }