DAO にメソッド追加

This commit is contained in:
2024-02-08 01:02:59 +09:00
parent 34859e55b3
commit 837b3aaab5
3 changed files with 101 additions and 4 deletions
+99
View File
@@ -0,0 +1,99 @@
<?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);
}
}
+2 -2
View File
@@ -7,7 +7,7 @@ class
Thread Thread
{ {
public static function public static function
fetch_threads ( fetch_all (
SQLite3 $db) SQLite3 $db)
: array : array
{ {
@@ -35,7 +35,7 @@ Thread
$threads = []; $threads = [];
while (($row = $threads -> fetchArray (SQLITE3_ASSOC)) !== false) while (($row = $result -> fetchArray (SQLITE3_ASSOC)) !== false)
{ {
$thread = new Dto\Thread; $thread = new Dto\Thread;
-2
View File
@@ -17,7 +17,5 @@ Response
public ?string $pass; public ?string $pass;
public int $good; public int $good;
public int $bad; public int $bad;
public Thread $thread;
} }