Skip to content

Commit

Permalink
Merge pull request #3 from GhostMech/nov6
Browse files Browse the repository at this point in the history
Nov6
  • Loading branch information
GhostMech committed Nov 6, 2017
2 parents 2e8e55f + 331a97a commit 16850d1
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 31 deletions.
8 changes: 8 additions & 0 deletions src/DbDeleteException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace GMH;

class DbDeleteException extends \PDOException
{
//
}
2 changes: 1 addition & 1 deletion src/DbInsertException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace GMH;

class DbInsertException extends \Exception
class DbInsertException extends \PDOException
{
//
}
8 changes: 8 additions & 0 deletions src/DbInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace GMH;

interface DbInterface
{
// Future plan for less reliance on PDO; may become StorageInterface instead.
}
8 changes: 8 additions & 0 deletions src/DbSelectException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace GMH;

class DbSelectException extends \PDOException
{
//
}
9 changes: 8 additions & 1 deletion src/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ class Task implements TaskInterface
/**
* The due date of the task.
*
* @var [type]
* @var string
*/
private $dueDate;

public function __get($property)
{
if (property_exists($this, $property)) {
return $this->$property;
}
}

/**
* Set the task name.
* Returns the object for method chaining.
Expand Down
77 changes: 48 additions & 29 deletions src/TaskCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace GMH;

require_once 'Task.php';

require_once 'DBInsertException.php';
use \PDO;
use \PDOException;
use GMH\DbInsertException;
use GMH\DbSelectException;
use GMH\DbDeleteException;
use GMH\TaskInterface;

class TaskCollection implements \Countable
{
Expand All @@ -22,7 +25,7 @@ class TaskCollection implements \Countable
*/
private $pdo;

public function __construct(\PDO $pdo)
public function __construct(PDO $pdo)
{
$this->pdo = $pdo;
}
Expand All @@ -38,7 +41,7 @@ public function count()
}

/**
* Adds a task to the collection.
* Add a task to the collection.
* Returns the object for method chaining.
*
* @param TaskInterface $task
Expand All @@ -57,17 +60,21 @@ public function addTask(TaskInterface $task)
}

/**
* Gets the "last inserted" id number from the database.
* Get the "last inserted" id number from the database.
*
* @return string
*/
public function getIdFromDb()
{
return $this->pdo->lastInsertId();
try {
return $this->pdo->lastInsertId();
} catch (PDOException $e) {
echo $e->getMessage();
}
}

/**
* Remove a task.
* Remove a task from the collection.
*
* @param int $id
*
Expand All @@ -82,7 +89,7 @@ public function removeTask($id)
}

/**
* Removes a task from the database.
* Remove a task from the database.
*
* @param mixed $id
*
Expand All @@ -96,13 +103,11 @@ private function removeTaskFromDb($id)
$stmt1->execute([':id' => $id]);
$removedTask = $stmt1->fetch();

var_dump($removedTask);

$stmt2 = $this->pdo->prepare('DELETE FROM tasks WHERE id=:id');
$stmt2->execute([':id' => $id]);

return $removedTask;
} catch (\PDOException $e) {
} catch (DbDeleteException $e) {
echo $e->getMessage();
}
}
Expand All @@ -114,34 +119,48 @@ private function removeTaskFromDb($id)
*/
public function getAllTasks()
{
if (isset($this->tasks)) {
$this->tasks = [];
}
$sql = 'SELECT id, name, due_date as dueDate FROM tasks';
$stmt = $this->pdo->prepare($sql);
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\GMH\Task');
$stmt->execute();
$this->tasks = $this->dbSelect($fields = ['id', 'name', 'due_date']);
}

while ($task = $stmt->fetch()) {
$this->tasks[] = $task;
private function dbSelect(array $fields)
{
try {
$results = [];
$sql = 'SELECT id, name, due_date as dueDate FROM tasks';
$stmt = $this->pdo->prepare($sql);
$stmt->setFetchMode(\PDO::FETCH_CLASS, '\GMH\Task');
$stmt->execute();

while ($task = $stmt->fetch()) {
$results[] = $task;
}

return $results;
} catch (DbSelectException $e) {
echo $e->getMessage();
}

return $this->tasks;
}

/**
* Inserts a task into the database.
* Insert a task into the database.
*
* @param \GMH\Task $task
*
* @return void
*/
private function dbInsert(Task $task)
private function dbInsert(TaskInterface $task)
{
if (is_null($task->getId())) {
$sql = 'INSERT INTO tasks values(null, :name, :dueDate)';
try {
if (is_null($task->getId())) {
$sql = 'INSERT INTO tasks values(null, :name, :dueDate)';
$stmt = $this->pdo->prepare($sql);
$stmt->execute([
':name' => $task->getName(),
':dueDate' => $task->getDueDate()
]);
}
} catch (DbInsertException $e) {
echo $e->getMessage;
}
$stmt = $this->pdo->prepare($sql);
$stmt->execute([':name' => $task->getName(), ':dueDate' => $task->getDueDate()]);
}
}

0 comments on commit 16850d1

Please sign in to comment.