[Feature:Tasks] Create and update methods

pull/8/head
sundowndev 2018-07-12 18:08:31 +02:00
parent c19c3a52b2
commit 2b5eab09f1
1 changed files with 56 additions and 4 deletions

View File

@ -1,20 +1,36 @@
<?php
namespace App\Repository;
use App\Service\Database;
/**
* Class TaskRepository
* @package App\Repository
*/
class TaskRepository
{
/**
* @var Database
*/
private $db;
/**
* @var
*/
private $tableName;
/**
* TaskRepository constructor.
* @param $db
*/
public function __construct($db)
{
$this->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();
}
}