diff options
author | dartcafe <github@dartcafe.de> | 2020-08-31 09:16:45 +0300 |
---|---|---|
committer | dartcafe <github@dartcafe.de> | 2020-08-31 09:16:45 +0300 |
commit | cc56251f43c1c2c925e8d54c2b153c3eb138a3d2 (patch) | |
tree | 08c99cdd2254edff0e9be872fd17cca46d4801c0 /lib | |
parent | b0c49ec853c04688b762acb33a393352d00fc6ee (diff) |
Added user preferences backend and UI
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Controller/PreferencesController.php | 145 | ||||
-rw-r--r-- | lib/Db/Preferences.php | 61 | ||||
-rw-r--r-- | lib/Db/PreferencesMapper.php | 74 | ||||
-rw-r--r-- | lib/Migration/Version0105Date20200523142076.php | 89 |
4 files changed, 369 insertions, 0 deletions
diff --git a/lib/Controller/PreferencesController.php b/lib/Controller/PreferencesController.php new file mode 100644 index 00000000..904e1a19 --- /dev/null +++ b/lib/Controller/PreferencesController.php @@ -0,0 +1,145 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Controller; + +use Exception; +use OCP\AppFramework\Db\DoesNotExistException; + + +use OCP\IRequest; +use OCP\ILogger; +use OCP\AppFramework\Controller; +use OCP\AppFramework\Http; +use OCP\AppFramework\Http\DataResponse; + +use OCP\IGroupManager; + +use OCA\Polls\Db\Preferences; +use OCA\Polls\Db\PreferencesMapper; + +class PreferencesController extends Controller { + + private $userId; + private $preferencesMapper; + private $logger; + + private $groupManager; + private $pollMapper; + private $anonymizer; + private $acl; + + /** + * PreferencesController constructor. + * @param string $appName + * @param $UserId + * @param PreferencesMapper $preferencesMapper + */ + + public function __construct( + string $appName, + $userId, + IRequest $request, + ILogger $logger, + PreferencesMapper $preferencesMapper + ) { + parent::__construct($appName, $request); + $this->userId = $userId; + $this->preferencesMapper = $preferencesMapper; + } + + + /** + * get + * Read all preferences + * @NoAdminRequired + * @NoCSRFRequired + * @param integer $pollId + * @return DataResponse + */ + public function get() { + + try { + return new DataResponse($this->preferencesMapper->find($this->userId), Http::STATUS_OK); + } catch (DoesNotExistException $e) { + return new DataResponse($e, Http::STATUS_NOT_FOUND); + } + + } + + /** + * write + * Write a new comment to the db and returns the new comment as array + * @NoAdminRequired + * @NoCSRFRequired + * @param int $pollId + * @param string $userId + * @param string $message + * @return DataResponse + */ + public function write($settings) { + if (!\OC::$server->getUserSession()->isLoggedIn()) { + return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + } + + try { + $preferences = $this->preferencesMapper->find($this->userId); + $preferences->setPreferences(json_encode($settings)); + $preferences->setTimestamp(time()); + $preferences = $this->preferencesMapper->update($preferences); + } catch (\Exception $e) { + $preferences = new Preferences(); + $preferences->setUserId($this->userId); + $preferences->setPreferences(json_encode($settings)); + $preferences->setTimestamp(time()); + $preferences = $this->preferencesMapper->insert($preferences); + } + + return new DataResponse($preferences, Http::STATUS_OK); + + } + + // /** + // * delete + // * Delete Preferences + // * @NoAdminRequired + // * @param int $pollId + // * @param string $message + // * @return DataResponse + // */ + // public function delete($userId) { + // if (!\OC::$server->getUserSession()->isLoggedIn()) { + // return new DataResponse(null, Http::STATUS_UNAUTHORIZED); + // } + // + // try { + // $this->preferencesMapper->delete($userId); + // } catch (\Exception $e) { + // return new DataResponse($e, Http::STATUS_CONFLICT); + // } + // + // return new DataResponse(['deleted' => $userId], Http::STATUS_OK); + // + // } + +} diff --git a/lib/Db/Preferences.php b/lib/Db/Preferences.php new file mode 100644 index 00000000..db15d529 --- /dev/null +++ b/lib/Db/Preferences.php @@ -0,0 +1,61 @@ +<?php +/** + * @copyright Copyright (c) 2020 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Db; + +use JsonSerializable; + +use OCP\IUser; +use OCP\AppFramework\Db\Entity; + +/** + * @method integer getId() + * @method void setId(integer $value) + * @method string getUserId() + * @method void setUserId(string $value) + * @method string getTimestamp() + * @method void setTimestamp(integer $value) + * @method string getPreferences() + * @method void setPreferences(string $value) + */ +class Preferences extends Entity implements JsonSerializable { + + /** @var string $userId */ + protected $userId; + + /** @var integer $timestamp */ + protected $timestamp; + + /** @var string $preferences */ + protected $preferences; + + public function jsonSerialize() { + + return [ + 'id' => intval($this->id), + 'userId' => $this->userId, + 'timestamp' => intval($this->timestamp), + 'preferences' => $this->preferences, + ]; + } +} diff --git a/lib/Db/PreferencesMapper.php b/lib/Db/PreferencesMapper.php new file mode 100644 index 00000000..b4d81a98 --- /dev/null +++ b/lib/Db/PreferencesMapper.php @@ -0,0 +1,74 @@ +<?php +/** + * @copyright Copyright (c) 2017 Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * + * @author Vinzenz Rosenkranz <vinzenz.rosenkranz@gmail.com> + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Db; + +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IDBConnection; +use OCP\AppFramework\Db\QBMapper; + +class PreferencesMapper extends QBMapper { + + /** + * PreferencesMapper constructor. + * @param IDBConnection $db + */ + public function __construct(IDBConnection $db) { + parent::__construct($db, 'polls_preferences', '\OCA\Polls\Db\Preferences'); + } + + /** + * @param int $id + * @throws \OCP\AppFramework\Db\DoesNotExistException if not found + * @throws \OCP\AppFramework\Db\MultipleObjectsReturnedException if more than one result + * @return Preferences + */ + + public function find($userId) { + $qb = $this->db->getQueryBuilder(); + + $qb->select('*') + ->from($this->getTableName()) + ->where( + $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_INT)) + ); + + return $this->findEntity($qb); + } + + // /** + // * @param int $userId + // */ + // public function delete($userId) { + // $qb = $this->db->getQueryBuilder(); + // + // $qb->delete($this->getTableName()) + // ->where( + // $qb->expr()->eq('user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_INT)) + // ); + // + // $qb->execute(); + // } + +} diff --git a/lib/Migration/Version0105Date20200523142076.php b/lib/Migration/Version0105Date20200523142076.php new file mode 100644 index 00000000..87da0be3 --- /dev/null +++ b/lib/Migration/Version0105Date20200523142076.php @@ -0,0 +1,89 @@ +<?php +/** + * @copyright Copyright (c) 2017 René Gieling <github@dartcafe.de> + * + * @author René Gieling <github@dartcafe.de> + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace OCA\Polls\Migration; + +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +/** + * Installation class for the polls app. + * Initial db creation + */ +class Version0105Date20200523142076 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + /** @var IConfig */ + protected $config; + + /** + * @param IDBConnection $connection + * @param IConfig $config + */ + public function __construct(IDBConnection $connection, IConfig $config) { + $this->connection = $connection; + $this->config = $config; + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + if (!$schema->hasTable('polls_preferences')) { + $table = $schema->createTable('polls_preferences'); + + $table->addColumn('id', Type::INTEGER, [ + 'autoincrement' => true, + 'notnull' => true, + ]); + $table->addColumn('user_id', Type::STRING, [ + 'notnull' => true, + 'length' => 64, + ]); + $table->addColumn('timestamp', Type::INTEGER, [ + 'length' => 11, + 'notnull' => true, + 'default' => 0 + ]); + $table->addColumn('preferences', Type::TEXT, [ + 'notnull' => true, + 'default' => '', + ]); + $table->setPrimaryKey(['id']); + } + return $schema; + } +} |