learning-center/app/Forum/Comment/Models/CommentWriteRepository.php

51 lines
1.2 KiB
PHP
Raw Permalink Normal View History

2024-09-08 13:48:26 +03:00
<?php
declare(strict_types=1);
namespace App\Forum\Comment\Models;
use App\SharedKernel\TimeSorting;
use Phalcon\Mvc\Model\Resultset;
use Ramsey\Uuid\UuidInterface;
final class CommentWriteRepository
{
public function get($id): Comment
{
$comment = Comment::findFirst("id = '$id'");
if ($comment === false) {
throw new CommentNotFound();
}
return $comment;
}
public function countByTopicId(UuidInterface $topicId): int
{
return Comment::count("topic_id = '$topicId'");
}
public function findByTopicId(UuidInterface $topicId, TimeSorting $timeSorting, int $page, int $limit): Resultset
{
$sortingDirection = $timeSorting == TimeSorting::newest() ? 'desc' : 'asc';
return Comment::find([
"topic_id = '$topicId'",
'order' => 'created_at ' . $sortingDirection,
'limit' => $limit,
'offset' => ($page - 1) * $limit,
]);
}
public function findAllByTopicId($topicId): Resultset
{
return Comment::find([
'conditions' => 'topic_id = :topic_id:',
'bind' => [
'topic_id' => $topicId,
],
]);
}
}