From 2b5eab09f1cac2b70976c8794242e0d0525018b5 Mon Sep 17 00:00:00 2001 From: sundowndev Date: Thu, 12 Jul 2018 18:08:31 +0200 Subject: [PATCH] [Feature:Tasks] Create and update methods --- server/src/Repository/TaskRepository.php | 60 ++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 4 deletions(-) diff --git a/server/src/Repository/TaskRepository.php b/server/src/Repository/TaskRepository.php index 0ea9969..77077e6 100644 --- a/server/src/Repository/TaskRepository.php +++ b/server/src/Repository/TaskRepository.php @@ -1,20 +1,36 @@ db = $db; $this->tableName = 'Task'; } + /** + * @return mixed + */ public function findAll() { $stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' ORDER BY id DESC'); @@ -23,6 +39,10 @@ class TaskRepository return $stmt->fetchAll(\PDO::FETCH_ASSOC); } + /** + * @param $id + * @return null + */ public function findOneById($id) { $stmt = $this->db->getConnection()->prepare('SELECT * FROM ' . $this->tableName . ' WHERE id = :id'); @@ -38,18 +58,50 @@ class TaskRepository } } + /** + * @param $data + * @return mixed + */ public function create($data) { - // + $stmt = $this->db->getConnection()->prepare('INSERT INTO ' . $this->tableName . ' (user_id, title, description, creation_date, status) VALUES(:user_id, :title, :description, :creation_date, :status)'); + $stmt->bindParam(':user_id', $data['user_id'], \PDO::PARAM_INT); + $stmt->bindParam(':title', $data['title'], \PDO::PARAM_STR); + $stmt->bindParam(':description', $data['description'], \PDO::PARAM_STR); + $stmt->bindParam(':creation_date', $data['creation_date']); + $stmt->bindParam(':status', $data['status'], \PDO::PARAM_INT); + $stmt->execute(); + + return $data; } + /** + * @param $id + * @param $data + * @return mixed + */ public function updateById($id, $data) { - // + $task = $this->findOneById($id); + + $stmt = $this->db->getConnection()->prepare('UPDATE ' . $this->tableName . ' SET user_id = :user_id, title = :title, description = :description, creation_date = :creation_date, status = :status'); + $stmt->bindParam(':user_id', $data['user_id'] ?? $task['user_id'], \PDO::PARAM_INT); + $stmt->bindParam(':title', $data['title'] ?? $task['title'], \PDO::PARAM_STR); + $stmt->bindParam(':description', $data['description'] ?? $task['description'], \PDO::PARAM_STR); + $stmt->bindParam(':creation_date', $data['creation_date'] ?? $task['creation_date']); + $stmt->bindParam(':status', $data['status'] ?? $task['status'], \PDO::PARAM_INT); + $stmt->execute(); + + return $data; } + /** + * @param $id + */ public function deleteById($id) { - // + $stmt = $this->db->getConnection()->prepare('DELETE FROM ' . $this->tableName . ' WHERE id = :id'); + $stmt->bindParam(':id', $id, \PDO::PARAM_INT); + $stmt->execute(); } } \ No newline at end of file