openvk/VKAPI/Handlers/Likes.php

72 lines
2.6 KiB
PHP
Raw Permalink Normal View History

<?php declare(strict_types=1);
namespace openvk\VKAPI\Handlers;
use openvk\Web\Models\Repositories\Users as UsersRepo;
use openvk\Web\Models\Repositories\Posts as PostsRepo;
final class Likes extends VKAPIRequestHandler
{
function add(string $type, int $owner_id, int $item_id): object
{
$this->requireUser();
2023-02-08 14:20:50 +03:00
$this->willExecuteWriteAction();
2022-07-21 22:13:09 +03:00
switch($type) {
case "post":
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
2022-07-21 22:13:09 +03:00
if(is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
$post->setLike(true, $this->getUser());
2022-07-21 22:13:09 +03:00
return (object) [
"likes" => $post->getLikesCount()
];
default:
2022-07-21 22:13:09 +03:00
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
function delete(string $type, int $owner_id, int $item_id): object
{
$this->requireUser();
2023-02-08 14:20:50 +03:00
$this->willExecuteWriteAction();
2022-07-21 22:13:09 +03:00
switch($type) {
case "post":
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
2022-07-21 22:13:09 +03:00
if (is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
$post->setLike(false, $this->getUser());
2022-07-21 22:13:09 +03:00
return (object) [
"likes" => $post->getLikesCount()
];
default:
2022-07-21 22:13:09 +03:00
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object
{
$this->requireUser();
2022-07-21 22:13:09 +03:00
switch($type) {
case "post":
$user = (new UsersRepo)->get($user_id);
2022-07-21 22:13:09 +03:00
if (is_null($user))
$this->fail(100, "One of the parameters specified was missing or invalid: user not found");
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
2022-07-21 22:13:09 +03:00
if (is_null($post))
$this->fail(100, "One of the parameters specified was missing or invalid: object not found");
2022-07-21 22:13:09 +03:00
return (object) [
"liked" => (int) $post->hasLikeFrom($user),
"copied" => 0 # TODO: handle this
];
default:
2022-07-21 22:13:09 +03:00
$this->fail(100, "One of the parameters specified was missing or invalid: incorrect type");
}
}
}