resource = $resource; $this->speciesFactory = $speciesFactory; $this->speciesCollectionFactory = $speciesCollectionFactory; $this->searchResultsFactory = $searchResultsFactory; $this->collectionProcessor = $collectionProcessor; } /** * @inheritDoc */ public function save(SpeciesInterface $species) { try { $this->resource->save($species); } catch (\Exception $exception) { throw new CouldNotSaveException(__( 'Could not save the species: %1', $exception->getMessage() )); } return $species; } /** * @inheritDoc */ public function get($speciesId) { $species = $this->speciesFactory->create(); $this->resource->load($species, $speciesId); if (!$species->getId()) { throw new NoSuchEntityException(__('Species with id "%1" does not exist.', $speciesId)); } return $species; } /** * @inheritDoc */ public function getList( \Magento\Framework\Api\SearchCriteriaInterface $criteria ) { $collection = $this->speciesCollectionFactory->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(SpeciesInterface $species) { try { $speciesModel = $this->speciesFactory->create(); $this->resource->load($speciesModel, $species->getSpeciesId()); $this->resource->delete($speciesModel); } catch (\Exception $exception) { throw new CouldNotDeleteException(__( 'Could not delete the Species: %1', $exception->getMessage() )); } return true; } /** * @inheritDoc */ public function deleteById($speciesId) { return $this->delete($this->get($speciesId)); } }