Commit new state

This commit is contained in:
Julius Haertl
2016-07-02 22:13:32 +02:00
parent dae1a9b3d4
commit 7a9489adf0
31 changed files with 884 additions and 97 deletions

View File

@@ -3,9 +3,8 @@
namespace OCA\Deck\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Board extends Entity implements JsonSerializable {
class Board extends \OCA\Deck\Db\Entity implements JsonSerializable {
public $id;
protected $title;
@@ -13,9 +12,12 @@ class Board extends Entity implements JsonSerializable {
protected $color;
protected $archived;
protected $labels;
public function __construct() {
$this->addType('id','integer');
$this->addRelation('labels');
}
public function jsonSerialize() {
return [
'id' => $this->id,

View File

@@ -2,7 +2,6 @@
namespace OCA\Deck\Db;
use OCP\AppFramework\Db\Entity;
use OCP\IDb;
use OCP\AppFramework\Db\Mapper;
@@ -10,6 +9,11 @@ use OCP\AppFramework\Db\Mapper;
class BoardMapper extends Mapper {
private $labelMapper;
private $_relationMappers = array();
public function addRelationMapper($name, $mapper) {
$this->_relationMappers[$name] = $mapper;
}
public function __construct(IDb $db, LabelMapper $labelMapper) {
parent::__construct($db, 'deck_boards', '\OCA\Deck\Db\Board');
@@ -36,8 +40,9 @@ class BoardMapper extends Mapper {
return $this->findEntities($sql, [$userId], $limit, $offset);
}
public function delete(Entity $entity) {
// FIXME: delete linked elements, because owncloud doesn't support foreign keys for apps
public function delete(\OCP\AppFramework\Db\Entity $entity) {
//$this->deleteRelationalEntities('label', $entity);
return parent::delete($entity);
}
}

View File

@@ -3,7 +3,6 @@
namespace OCA\Deck\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Card extends Entity implements JsonSerializable {

View File

@@ -10,8 +10,11 @@ use OCP\AppFramework\Db\Mapper;
class CardMapper extends Mapper {
public function __construct(IDb $db) {
private $labelMapper;
public function __construct(IDb $db, LabelMapper $labelMapper) {
parent::__construct($db, 'deck_cards', '\OCA\Deck\Db\Card');
$this->labelMapper = $labelMapper;
}
public function insert(Entity $entity) {
@@ -37,7 +40,10 @@ class CardMapper extends Mapper {
public function find($id) {
$sql = 'SELECT * FROM `*PREFIX*deck_cards` ' .
'WHERE `id` = ?';
return $this->findEntity($sql, [$id]);
$card = $this->findEntity($sql, [$id]);
$labels = $this->labelMapper->findAssignedLabelsForCard($card->id);
$card->setLabels($labels);
return $card;
}
public function findAllByBoard($boardId, $limit=null, $offset=null) {
@@ -55,4 +61,20 @@ class CardMapper extends Mapper {
return parent::delete($entity);
}
public function assignLabel($card, $label) {
$sql = 'INSERT INTO `*PREFIX*deck_assigned_labels` (`label_id`,`card_id`) VALUES (?,?)';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $label, \PDO::PARAM_INT);
$stmt->bindParam(2, $card, \PDO::PARAM_INT);
$stmt->execute();
}
public function removeLabel($card, $label) {
$sql = 'DELETE FROM `*PREFIX*deck_assigned_labels` WHERE card_id = ? AND label_id = ?';
$stmt = $this->db->prepare($sql);
$stmt->bindParam(1, $card, \PDO::PARAM_INT);
$stmt->bindParam(2, $label, \PDO::PARAM_INT);
$stmt->execute();
}
}

48
db/entity.php Normal file
View File

@@ -0,0 +1,48 @@
<?php
/**
* Created by PhpStorm.
* User: jus
* Date: 22.06.16
* Time: 13:32
*/
namespace OCA\Deck\Db;
class Entity extends \OCP\AppFramework\Db\Entity {
private $_relations = array();
private $_updatedFields = array();
/**
* Mark a property as relation so it will not get updated using Mapper::update
* @param string $property Name of the property
*/
public function addRelation(string $property) {
if (!in_array($property, $this->_relations)) {
$this->_relations[] = $property;
}
}
/**
* Mark am attribute as updated
* overwritten from \OCP\AppFramework\Db\Entity to avoid writing relational attributes
* @param string $attribute the name of the attribute
* @since 7.0.0
*/
protected function markFieldUpdated($attribute){
if(!in_array($attribute, $this->_relations)) {
$this->_updatedFields[$attribute] = true;
}
}
/**
* overwritten from \OCP\AppFramework\Db\Entity to avoid writing relational attributes
* @return array Array of field's update status
*/
public function getUpdatedFields(){
return $this->_updatedFields;
}
}

View File

@@ -3,7 +3,6 @@
namespace OCA\Deck\Db;
use JsonSerializable;
use OCP\AppFramework\Db\Entity;
class Label extends Entity implements JsonSerializable {

View File

@@ -24,11 +24,11 @@ class LabelMapper extends DeckMapper {
}
public function findAssignedLabelsForCard($cardId) {
$sql = 'SELECT * FROM `*PREFIX*deck_assigned_labels` as al INNER JOIN *PREFIX*deck_labels as l ON l.id = al.label_id WHERE `card_id` = ?';
$sql = 'SELECT l.* FROM `*PREFIX*deck_assigned_labels` as al INNER JOIN *PREFIX*deck_labels as l ON l.id = al.label_id WHERE `card_id` = ?';
return $this->findEntities($sql, [$cardId], $limit, $offset);
}
public function findAssignedLabelsForBoard($boardId, $limit=null, $offset=null) {
$sql = "SELECT c.id as card_id, l.id as id, l.title as title, color FROM oc_deck_cards as c " .
$sql = "SELECT c.id as card_id, l.id as id, l.title as title, l.color as color FROM oc_deck_cards as c " .
" INNER JOIN oc_deck_assigned_labels as al, oc_deck_labels as l ON al.card_id = c.id AND al.label_id = l.id WHERE board_id=?";
$entities = $this->findEntities($sql, [$boardId], $limit, $offset);
return $entities;