. * */ namespace OCA\News\BusinessLayer; use \OCA\AppFramework\Core\API; use \OCA\News\Db\Folder; use \OCA\News\Db\FolderMapper; class FolderBusinessLayer extends BusinessLayer { private $api; public function __construct(FolderMapper $folderMapper, API $api){ parent::__construct($folderMapper); $this->api = $api; } public function findAll($userId) { return $this->mapper->findAllFromUser($userId); } private function allowNoNameTwice($folderName, $userId){ $existingFolders = $this->mapper->findByName($folderName, $userId); if(count($existingFolders) > 0){ throw new BusinessLayerExistsException( $this->api->getTrans()->t('Can not add folder: Exists already')); } } /** * @throws BusinessLayerExistsException if name exists already */ public function create($folderName, $userId, $parentId=0) { $this->allowNoNameTwice($folderName, $userId); $folder = new Folder(); $folder->setName($folderName); $folder->setUserId($userId); $folder->setParentId($parentId); $folder->setOpened(true); return $this->mapper->insert($folder); } /** * @throws BusinessLayerException if the folder does not exist */ public function open($folderId, $opened, $userId){ $folder = $this->find($folderId, $userId); $folder->setOpened($opened); $this->mapper->update($folder); } /** * @throws BusinessLayerExistsException if name exists already * @throws BusinessLayerException if the folder does not exist */ public function rename($folderId, $folderName, $userId){ $this->allowNoNameTwice($folderName, $userId); $folder = $this->find($folderId, $userId); $folder->setName($folderName); $this->mapper->update($folder); } }