<?php

namespace Dao;


class
Response
{
  public static function
  fetch_all (
      SQLite3 $db,
      int     $thread_id)
  : array
  {
    $sql = "
        SELECT
          thread_id,
          response_id,
          name,
          message,
          date,
          image,
          held,
          deleted,
          pass,
          good,
          bad
        FROM
          responses
        WHERE
          id = $thread_id";

    $result = $db -> query ($sql);

    $responses = [];

    while (($row = $result -> fetchArray (SQLITE_ASSOC)) !== false)
      {
        $response = new Dto\Response;

        $response -> thread_id = $row['thread_id'];
        $response -> response_id = $row['response_id'];
        $response -> name = $row['name'];
        $response -> message = $row['message'];
        $response -> date = $row['date'];
        $response -> image = $row['image'];
        $response -> held = $row['held'];
        $response -> deleted = $row['deleted'];
        $response -> pass = $row['pass'];
        $response -> good = $row['good'];
        $response -> bad = $row['bad'];

        $responses[] = $response;
      }

    return $responses;
  }


  public static function
  like (
      SQLite3 $db,
      int     $thread_id,
      int     $response_id)
  : void
  {
    $sql = "
        UPDATE
          responses
        SET
          good = good + 1
        WHERE
          (thread_id = $thread_id)
          AND (response_id = $response_id)";

    $db -> query ($sql);
  }


  public static function
  dislike (
      SQLite3 $db,
      int     $thread_id,
      int     $response_id)
  : void
  {
    $sql = "
        UPDATE
          responses
        SET
          bad = bad + 1
        WHERE
          (thread_id = $thread_id)
          AND (response_id = $response_id)";

    $db -> query ($sql);
  }
}

