<?php

namespace App\Post;

use PDO;

use App\Core\AbstractRepository;
//use App\Post\PostAdminController;

class CommentsRepository extends AbstractRepository
{

  public function getTableName()
  {
    return "blog_comments";
  }

  public function getModelName()
  {
    return "App\\Post\\CommentModel";
  }

  public function insertForPost($postId, $content)
  {
      $table = $this->getTableName();
      $stmt = $this->pdo->prepare(
        "INSERT INTO `$table` (`content`, `post_id`) VALUES (:content, :postId)"
      );
      $stmt->execute([
        'content' => $content,
        'postId' => $postId
      ]);
  }

  public function update($postId, $content, $id, $org) {
    echo "here";
    $table = $this->getTableName();
    $model = $this->getModelName();
    $stmt = $this->pdo->prepare(
      "UPDATE `{$table}` SET `content` = :content, `post_id` = :postId, `original` = :original
        WHERE `id` = :id");
      $stmt->execute([
        'content' => $content,
        'postId' => $postId,
        'id' => $id,
        'original' => $org
      ]);
    }

  public function allByPost($id) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE post_id = :id");
    $stmt->execute(['id' => $id]);

    $comments = $stmt->fetchAll(PDO::FETCH_CLASS, $model);
    return $comments;
  }

  public function allCommentsByID($id) {
    $table = $this->getTableName();
    $model = $this->getModelName();

    $stmt = $this->pdo->prepare("SELECT * FROM `$table` WHERE post_id = :id");
    $stmt->execute(['id' => $id]);

    $comments = $stmt->fetchAll(PDO::FETCH_CLASS, $model);
    return $comments;
  }

}
 ?>
