Skip to content

Commit

Permalink
needed db file
Browse files Browse the repository at this point in the history
  • Loading branch information
techwebb committed Apr 6, 2019
1 parent 876ce3e commit 3687446
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 1 deletion.
86 changes: 86 additions & 0 deletions api/DB.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
namespace App;

//Super Fake Database Class
class DB {

private $table = 'todo';
private $primaryKey = 'id';
private $createFields = [
'text' => '',
'created' => 'timestamp',
'completed' => ''
];
private $updateRules = [
'text' => '',
'completed' => 'timestamp'
];

function __construct(){
//start with one entry
if(!isset($_SESSION[$this->table]))
$_SESSION[$this->table] = [ [
'id' => 0,
'text' => 'Get ye flask',
'created' => date('U'),
'completed' => NULL
] ];
}

public function get($id){
if(isset($_SESSION[$this->table][$id])) return $_SESSION[$this->table][$id];
return false;
}

public function getAll(){
if(!count($_SESSION[$this->table])) return [];
return array_values($_SESSION[$this->table]);
}

public function store($params){
if(isset($_SESSION[$this->table])) $new_id = 1 + max(array_keys($_SESSION[$this->table]));
else $new_id = 1;

foreach($this->createFields as $key=>$default){
$fields[$key] = isset($params[$key]) ? $params[$key] : $this->mapDefaults($this->createFields, $key);
}
$fields[$this->primaryKey] = $new_id;

$_SESSION[$this->table][$new_id] = $fields;
return true;
}

public function update($id, $params){
$old = $this->get($id);
if(!$old) return false;

foreach($this->updateRules as $key=>$default){
if(isset($params[$key]))
$old[$key] = $this->mapDefaults($this->updateRules, $key) ?: $params[$key];
}

$_SESSION[$this->table][$id] = $old;
return true;
}

public function delete($id){
$old = $this->get($id);
if(!$old) return false;

unset($_SESSION[$this->table][$id]);
return true;
}

private function mapDefaults($map, $key){
switch($map[$key]){
case '':
return null;
break;
case 'timestamp':
return date('U');
break;
}
return null;
}

}
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@
});
},
fetchAll(){
axios.get('/api/todo/').then( ({data}) => {
axios.get('/api/todo').then( ({data}) => {
this.todos = data.data;
}).catch( (res) =>{
this.apiResponse = 'We had a problem talking to the api.';
Expand Down

0 comments on commit 3687446

Please sign in to comment.