Add change database helper
Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
91
lib/Db/ChangeHelper.php
Normal file
91
lib/Db/ChangeHelper.php
Normal file
@@ -0,0 +1,91 @@
|
||||
<?php
|
||||
/**
|
||||
* @copyright Copyright (c) 2018 Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @author Julius Härtl <jus@bitgrid.net>
|
||||
*
|
||||
* @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\Deck\Db;
|
||||
|
||||
use OCP\AppFramework\Db\DoesNotExistException;
|
||||
use OCP\ICache;
|
||||
use OCP\ICacheFactory;
|
||||
use OCP\IDBConnection;
|
||||
use OCP\IRequest;
|
||||
use OCP\IUserManager;
|
||||
use OCP\IGroupManager;
|
||||
|
||||
class ChangeHelper {
|
||||
|
||||
const TYPE_BOARD = 'boardChanged';
|
||||
const TYPE_CARD = 'cardChanged';
|
||||
|
||||
private $db;
|
||||
|
||||
public function __construct(
|
||||
IDBConnection $db,
|
||||
ICacheFactory $cacheFactory,
|
||||
IRequest $request
|
||||
) {
|
||||
$this->db = $db;
|
||||
$this->cache = $cacheFactory->createDistributed('deck_changes');
|
||||
$this->request = $request;
|
||||
}
|
||||
|
||||
public function boardChanged($boardId) {
|
||||
$time = time();
|
||||
$etag = md5($time . microtime());
|
||||
$this->cache->set(self::TYPE_BOARD . '-' . $boardId, $etag);
|
||||
$sql = 'UPDATE `*PREFIX*deck_boards` SET `last_modified` = ? WHERE `id` = ?';
|
||||
$this->db->executeUpdate($sql, [$time, $boardId]);
|
||||
}
|
||||
|
||||
public function cardChanged($cardId, $updateCard = true) {
|
||||
$time = time();
|
||||
$etag = md5($time . microtime());
|
||||
$this->cache->set(self::TYPE_CARD . '-' .$cardId, $etag);
|
||||
if ($updateCard) {
|
||||
$sql = 'UPDATE `*PREFIX*deck_cards` SET `last_modified` = ? WHERE `id` = ?';
|
||||
$this->db->executeUpdate($sql, [time(), $cardId]);
|
||||
}
|
||||
|
||||
$sql = 'SELECT s.board_id as id FROM oc_deck_stacks as s inner join oc_deck_cards as c ON c.stack_id = s.id WHERE c.id = ?';
|
||||
$result = $this->db->executeQuery($sql, [$cardId]);
|
||||
if ($row = $result->fetch()) {
|
||||
$this->boardChanged($row['id']);
|
||||
}
|
||||
}
|
||||
|
||||
public function checkEtag($type, $id) {
|
||||
$etag = $this->getEtag($type, $id);
|
||||
if ($this->request->getHeader('If-None-Match') === $etag) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public function getEtag($type, $id) {
|
||||
$entry = $this->cache->get($type . '-' .$id);
|
||||
if ($entry === 'null') {
|
||||
return '';
|
||||
}
|
||||
return $entry;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -28,9 +28,9 @@ class Stack extends RelationalEntity {
|
||||
protected $title;
|
||||
protected $boardId;
|
||||
protected $deletedAt = 0;
|
||||
protected $lastModified = 0;
|
||||
protected $cards = array();
|
||||
protected $order;
|
||||
protected $lastModified = 0;
|
||||
|
||||
public function __construct() {
|
||||
$this->addType('id', 'integer');
|
||||
|
||||
Reference in New Issue
Block a user