2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Repositories;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
use openvk\Web\Models\Entities\Note;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use Nette\Database\Table\ActiveRow;
|
|
|
|
|
|
|
|
class Notes
|
|
|
|
{
|
|
|
|
private $context;
|
|
|
|
private $notes;
|
|
|
|
|
|
|
|
function __construct()
|
|
|
|
{
|
|
|
|
$this->context = DatabaseConnection::i()->getContext();
|
|
|
|
$this->notes = $this->context->table("notes");
|
|
|
|
}
|
|
|
|
|
|
|
|
private function toNote(?ActiveRow $ar): ?Note
|
|
|
|
{
|
|
|
|
return is_null($ar) ? NULL : new Note($ar);
|
|
|
|
}
|
|
|
|
|
|
|
|
function get(int $id): ?Note
|
|
|
|
{
|
|
|
|
return $this->toNote($this->notes->get($id));
|
|
|
|
}
|
|
|
|
|
2023-07-28 00:44:05 +03:00
|
|
|
function getUserNotes(User $user, int $page = 1, ?int $perPage = NULL, string $sort = "DESC"): \Traversable
|
2020-06-07 19:04:43 +03:00
|
|
|
{
|
|
|
|
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE;
|
2023-07-28 00:44:05 +03:00
|
|
|
foreach($this->notes->where("owner", $user->getId())->where("deleted", 0)->order("created $sort")->page($page, $perPage) as $album)
|
2020-06-07 19:04:43 +03:00
|
|
|
yield new Note($album);
|
|
|
|
}
|
2021-11-15 14:00:49 +03:00
|
|
|
|
|
|
|
function getNoteById(int $owner, int $note): ?Note
|
|
|
|
{
|
|
|
|
$note = $this->notes->where(['owner' => $owner, 'virtual_id' => $note])->fetch();
|
|
|
|
if(!is_null($note))
|
|
|
|
return new Note($note);
|
|
|
|
else
|
2022-05-08 13:06:26 +03:00
|
|
|
return NULL;
|
2021-11-15 14:00:49 +03:00
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function getUserNotesCount(User $user): int
|
|
|
|
{
|
2020-06-24 23:20:25 +03:00
|
|
|
return sizeof($this->notes->where("owner", $user->getId())->where("deleted", 0));
|
2020-06-07 19:04:43 +03:00
|
|
|
}
|
|
|
|
}
|