Skip to content

Commit

Permalink
VKAPI: Add Likes.add, Likes.remove and Likes.isLiked methods
Browse files Browse the repository at this point in the history
  • Loading branch information
veselcraft committed Mar 29, 2022
1 parent 91cf2dc commit f7a2da2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
74 changes: 74 additions & 0 deletions VKAPI/Handlers/Likes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?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();

switch ($type) {
case 'post':
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');

$post->setLike(true, $this->getUser());
return (object)[
"likes" => $post->getLikesCount()
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
}
}

function remove(string $type, int $owner_id, int $item_id): object
{
$this->requireUser();

switch ($type) {
case 'post':
$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');

$post->setLike(false, $this->getUser());
return (object)[
"likes" => $post->getLikesCount()
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
}
}

function isLiked(int $user_id, string $type, int $owner_id, int $item_id): object
{
$this->requireUser();

switch ($type) {
case 'post':
$user = (new UsersRepo)->get($user_id);
if (is_null($user)) return (object)[
"liked" => 0,
"copied" => 0,
"sex" => 0
];

$post = (new PostsRepo)->getPostById($owner_id, $item_id);
if (is_null($post)) $this->fail(100, 'One of the parameters specified was missing or invalid: object not found');

return (object)[
"liked" => (int) $post->hasLikeFrom($user),
"copied" => 0 // TODO: handle this
];
break;
default:
$this->fail(100, 'One of the parameters specified was missing or invalid: incorrect type');
break;
}
}
}
1 change: 0 additions & 1 deletion Web/Models/Repositories/Posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ function getPostById(int $wall, int $post): ?Post
{
$post = $this->posts->where(['wall' => $wall, 'virtual_id' => $post])->fetch();
if(!is_null($post))

return new Post($post);
else
return null;
Expand Down

0 comments on commit f7a2da2

Please sign in to comment.