Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require_once("todo_conf.php");
- // Caching Service
- class TodoCache {
- private $cache;
- public function __construct() { $this->cache = array(); }
- public function set($key, $val) { $this->cache[$key] = $val; }
- public function get($key) { return $this->cache[$key]; }
- public function hasEntry($key) { return isset($this->cache[$key]); }
- }
- // Todo_Backend
- class Todo {
- private $list;
- public function __construct() {
- $this->dbConn = $this->getDbConn();
- $this->cache = new TodoCache();
- }
- private function logger($log_msg) {
- echo $log_msg;
- }
- private function getDbConn() {
- $db = new PDO("pgsql:host=".DBHOST.";port=".DBPORT."; dbname='".DBNAME."'; user=".DBUSER." password=".DBPASS);
- $db->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
- return $db;
- }
- private function getOwnerId($owner) {
- $stmt = $this->dbConn->prepare("SELECT id FROM owners WHERE owner = :owner");
- $stmt->execute(array(':owner'=>$owner));
- return $stmt->fetchColumn(\PDO::FETCH_ASSOC);
- }
- private function addItem($owner_id, $item) {
- $this->logger("Adding $item for owner with id $owner_id\n");
- $stmt = $this->dbConn->prepare("INSERT INTO todo (owner_id, item) VALUES (:owner_id,
- :item)");
- $stmt->execute(array(':owner_id'=>$owner_id, ':item'=>$item));
- return $stmt->fetch(\PDO::FETCH_ASSOC);
- }
- private function createOwnerAddItem($owner, $item) {
- // entire body done in a transaction of course
- $this->logger("Add $owner to the owner's list\n");
- $stmt = $this->dbConn->prepare("INSERT INTO owners (owner) VALUES (:owner) RETURNING id");
- $stmt->execute(array(':owner'=>$owner));
- $res = $stmt->fetch(\PDO::FETCH_ASSOC);
- $owner_id = $res['id'];
- $this->addItem($owner_id, $item);
- }
- public function add($owner, $item) {
- $owner_id = $this->getOwnerId($owner);
- if ($owner_id === false || is_null($owner_id)) {
- $this->createOwnerAddItem($owner, $item);
- } else {
- $this->addItem($owner_id, $item);
- }
- }
- private function getOwnersItems($owner) {
- if ($this->cache->hasEntry($owner)) {
- return $this->cache->get($owner);
- }
- $stmt = $this->dbConn->prepare("SELECT item FROM todo t
- JOIN owners o ON o.id = t.owner_id WHERE o.owner = :owner");
- $stmt->execute(array(':owner'=>$owner));
- $res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
- $this->cache->set($owner, $res);
- return $res;
- }
- public function display($owner) {
- $rows = $this->getOwnersItems($owner);
- foreach ($rows as $row) {
- echo "$owner -> ${row['item']}\n";
- }
- }
- }
- // Consumer, Frontend/Controller
- $c = new Todo();
- $c->add('Adam', 'Buy bread');
- $c->add('Adam', 'Buy butter');
- $c->add('Jane', 'Visit Paris');
- $c->add('Jane', 'Fix car');
- $c->add('Adam', 'Fix Bike');
- $c->display('Jane');
- $c->display('Adam');
- $c->add('Adam', 'Yoga');
- $c->display('Adam');
- class TodoRestAPI {
- public function POST_add($owner, $item) { }
- public function GET_items($owner) { }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement