51 lines
1.4 KiB
PHP
51 lines
1.4 KiB
PHP
<?php
|
|
|
|
use MintyPHP\Http\SessionStoreInterface;
|
|
use MintyPHP\Module\Bookmarks\BookmarksAuthorizationPolicy;
|
|
use MintyPHP\Module\Bookmarks\Service\BookmarkService;
|
|
use MintyPHP\Router;
|
|
use MintyPHP\Session;
|
|
use MintyPHP\Support\Guard;
|
|
|
|
Guard::requireLogin();
|
|
Guard::requireAbilityOrForbidden(BookmarksAuthorizationPolicy::ABILITY_USE);
|
|
|
|
if (requestInput()->method() !== 'POST') {
|
|
http_response_code(405);
|
|
Router::json(['ok' => false, 'error' => 'method_not_allowed']);
|
|
return;
|
|
}
|
|
|
|
if (!Session::checkCsrfToken()) {
|
|
http_response_code(403);
|
|
Router::json(['ok' => false, 'error' => 'csrf']);
|
|
return;
|
|
}
|
|
|
|
$session = app(SessionStoreInterface::class)->all();
|
|
$userId = (int) ($session['user']['id'] ?? 0);
|
|
if ($userId <= 0) {
|
|
http_response_code(401);
|
|
Router::json(['ok' => false, 'error' => 'unauthorized']);
|
|
return;
|
|
}
|
|
|
|
$bookmarkId = (int) requestInput()->body('id');
|
|
if ($bookmarkId <= 0) {
|
|
http_response_code(400);
|
|
Router::json(['ok' => false, 'error' => 'invalid_bookmark_id']);
|
|
return;
|
|
}
|
|
|
|
$service = app(BookmarkService::class);
|
|
$deleted = $service->deleteBookmark($userId, $bookmarkId);
|
|
|
|
if ($deleted) {
|
|
app(SessionStoreInterface::class)->set('module.bookmarks.grouped', $service->listGroupedForUser($userId));
|
|
Router::json(['ok' => true]);
|
|
return;
|
|
}
|
|
|
|
http_response_code(404);
|
|
Router::json(['ok' => false, 'error' => 'not_found']);
|