<?php

class GatewayChapter
{
    private $con;

    public function __construct($con)
    {
        $this->con = $con;
    }

    public function addChapter($chapter)
    {
        $query = "insert into chapters(name) values (:name);";
        $this->con->executeQuery(
            $query,
            array(
                ':name' => array($chapter->getName(), PDO::PARAM_STR)
            )
        );
    }

    public function getChapters()
    {
        $query = "SELECT * FROM chapters";
        $this->con->executeQuery($query);
        $results = $this->con->getResults();
                
        return $results;
    }

    public function getChapterByID(int $id)
    {
        $query = "SELECT * FROM chapters WHERE id = :id;";
        $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
        $results = $this->con->getResults();

        return $results[0];
    }

    public function updateChapter($id,$chapter)
    {
        $query = "UPDATE chapters SET name = :name WHERE id = :id;";
        $this->con->executeQuery(
            $query,
            array(
                ':id' => array($id, PDO::PARAM_INT),
                ':name' => array($chapter->getName(), PDO::PARAM_STR)
            )
        );
    }

    public function deleteChapter($id)
    {
        $query = "DELETE FROM chapters WHERE id = :id;";
        $this->con->executeQuery($query, array(':id' => array($id, PDO::PARAM_INT)));
    }
}
