Refactor API
This commit is contained in:
parent
1c3c11b75c
commit
8cd0fe80b8
10 changed files with 133 additions and 124 deletions
|
@ -18,6 +18,13 @@
|
||||||
|
|
||||||
# Blog Post
|
# Blog Post
|
||||||
RewriteRule ^blog/([a-z\-0-9]+)$ /?page=post&slug=$1 [QSA,L]
|
RewriteRule ^blog/([a-z\-0-9]+)$ /?page=post&slug=$1 [QSA,L]
|
||||||
|
|
||||||
|
# API
|
||||||
|
RewriteRule ^api/([a-zA-Z\-0-9=]+)$ /?page=api&module=$1 [QSA,L]
|
||||||
|
|
||||||
|
RewriteRule ^api/statistics/([a-zA-Z0-9=]+)$ /?page=api&module=statistics&table=$1 [QSA,L]
|
||||||
|
RewriteRule ^api/url/([a-zA-Z0-9=]+)$ /?page=api&module=url&url=$1 [QSA,L]
|
||||||
|
RewriteRule ^api/wishlists/([0-9]+)$ /?page=api&module=wishlists&wishlist_id=$1 [QSA,L]
|
||||||
</IfModule>
|
</IfModule>
|
||||||
|
|
||||||
##-- When caching of gzipped JS and CSS files is used, enable this setting
|
##-- When caching of gzipped JS and CSS files is used, enable this setting
|
||||||
|
|
|
@ -10,11 +10,6 @@ namespace wishthis;
|
||||||
|
|
||||||
global $page, $database;
|
global $page, $database;
|
||||||
|
|
||||||
if (!isset($page)) {
|
|
||||||
http_response_code(403);
|
|
||||||
die('Direct access to this location is not allowed.');
|
|
||||||
}
|
|
||||||
|
|
||||||
switch ($_SERVER['REQUEST_METHOD']) {
|
switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
case 'POST':
|
case 'POST':
|
||||||
if (isset($_POST['wishlist-name'], $_SESSION['user']->id)) {
|
if (isset($_POST['wishlist-name'], $_SESSION['user']->id)) {
|
||||||
|
@ -124,7 +119,16 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
*/
|
*/
|
||||||
$wishlist = new Wishlist($_GET['wishlist_id']);
|
$wishlist = new Wishlist($_GET['wishlist_id']);
|
||||||
|
|
||||||
$response['results'] = $wishlist;
|
if ($wishlist->exists) {
|
||||||
|
/** Determine if user is allowed to access wishlist */
|
||||||
|
if ($_SESSION['user']->isLoggedIn() && $_SESSION['user']->id === $wishlist->user) {
|
||||||
|
$response['results'] = $wishlist;
|
||||||
|
} else {
|
||||||
|
http_response_code(403);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
http_response_code(404);
|
||||||
|
}
|
||||||
} elseif (isset($_GET['userid']) || isset($_SESSION['user']->id)) {
|
} elseif (isset($_GET['userid']) || isset($_SESSION['user']->id)) {
|
||||||
/**
|
/**
|
||||||
* Get user wishlists
|
* Get user wishlists
|
||||||
|
@ -165,7 +169,7 @@ switch ($_SERVER['REQUEST_METHOD']) {
|
||||||
|
|
||||||
$database->query(
|
$database->query(
|
||||||
'DELETE FROM `wishlists`
|
'DELETE FROM `wishlists`
|
||||||
WHERE `id` = ' . Sanitiser::getNumber($_DELETE['wishlistID']) . ';'
|
WHERE `id` = ' . Sanitiser::getNumber($_DELETE['wishlist_id']) . ';'
|
||||||
);
|
);
|
||||||
|
|
||||||
$response['success'] = true;
|
$response['success'] = true;
|
||||||
|
|
|
@ -16,14 +16,6 @@ $(function() {
|
||||||
/**
|
/**
|
||||||
* Fomantic UI
|
* Fomantic UI
|
||||||
*/
|
*/
|
||||||
/** API */
|
|
||||||
$.fn.api.settings.api = {
|
|
||||||
'get wishlists' : '/?page=api&module=wishlists',
|
|
||||||
'get wishlists by priority' : '/?page=api&module=wishlists&style={style}&wishlist_id={wishlistid}&priority={priority}',
|
|
||||||
'delete wishlist' : '/?page=api&module=wishlists',
|
|
||||||
'update wish status' : '/?page=api&module=wishes',
|
|
||||||
'delete wish' : '/?page=api&module=wishes',
|
|
||||||
};
|
|
||||||
|
|
||||||
/** Default callbacks */
|
/** Default callbacks */
|
||||||
$.fn.api.settings.onResponse = function(response) {
|
$.fn.api.settings.onResponse = function(response) {
|
||||||
|
@ -210,6 +202,7 @@ function handleFetchError(response) {
|
||||||
console.log(response);
|
console.log(response);
|
||||||
|
|
||||||
showError(response.statusText);
|
showError(response.statusText);
|
||||||
|
|
||||||
throw Error(response.statusText);
|
throw Error(response.statusText);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -2,16 +2,7 @@ $(function() {
|
||||||
/**
|
/**
|
||||||
* Statistics
|
* Statistics
|
||||||
*/
|
*/
|
||||||
const params_statistics = new URLSearchParams(
|
fetch('/api/statistics/all', {
|
||||||
{
|
|
||||||
'module' : 'statistics',
|
|
||||||
'page' : 'api',
|
|
||||||
|
|
||||||
'table' : 'all',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
fetch('/?' + params_statistics, {
|
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
})
|
})
|
||||||
.then(handleFetchError)
|
.then(handleFetchError)
|
||||||
|
@ -65,14 +56,7 @@ $(function() {
|
||||||
/**
|
/**
|
||||||
* News
|
* News
|
||||||
*/
|
*/
|
||||||
const params_news = new URLSearchParams(
|
fetch('/api/blog', {
|
||||||
{
|
|
||||||
'module' : 'blog',
|
|
||||||
'page' : 'api',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
fetch('/?' + params_news, {
|
|
||||||
method: 'GET'
|
method: 'GET'
|
||||||
})
|
})
|
||||||
.then(handleFetchError)
|
.then(handleFetchError)
|
||||||
|
|
|
@ -20,7 +20,7 @@ window.addEventListener("load", (event) => {
|
||||||
data.append('page', wishthis.$_GET.page);
|
data.append('page', wishthis.$_GET.page);
|
||||||
|
|
||||||
/** Save page preview */
|
/** Save page preview */
|
||||||
fetch('/?page=api&module=save-preview', {
|
fetch('/api/save-preview', {
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : data
|
body : data
|
||||||
})
|
})
|
||||||
|
|
|
@ -148,4 +148,32 @@ global $options;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Fomantic UI
|
||||||
|
*/
|
||||||
|
<?php
|
||||||
|
$api_urls = array(
|
||||||
|
'get wishlists' => '/?page=api&module=wishlists',
|
||||||
|
'get wishlists by priority' => '/?page=api&module=wishlists&style={style}&wishlist_id={wishlistid}&priority={priority}',
|
||||||
|
'delete wishlist' => '/?page=api&module=wishlists',
|
||||||
|
'update wish status' => '/?page=api&module=wishes',
|
||||||
|
'delete wish' => '/?page=api&module=wishes',
|
||||||
|
);
|
||||||
|
?>
|
||||||
|
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
/** API */
|
||||||
|
$.fn.api.settings.api = {
|
||||||
|
<?php
|
||||||
|
foreach ($api_urls as $action => $url) {
|
||||||
|
$url = new URL($url);
|
||||||
|
$pretty = $url->getPretty();
|
||||||
|
|
||||||
|
echo '\'' . $action . '\' : \'' . $pretty . '\',' . PHP_EOL;
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
|
@ -9,7 +9,7 @@ $(function() {
|
||||||
|
|
||||||
form.addClass('loading');
|
form.addClass('loading');
|
||||||
|
|
||||||
fetch('/?page=api&module=database-test', {
|
fetch('/api/database-test', {
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : formDatabase
|
body : formDatabase
|
||||||
})
|
})
|
||||||
|
|
|
@ -28,17 +28,7 @@ $(function () {
|
||||||
dropdown_wishlists.dropdown('setup menu', dropdown_values);
|
dropdown_wishlists.dropdown('setup menu', dropdown_values);
|
||||||
|
|
||||||
/** Select a dropdown item */
|
/** Select a dropdown item */
|
||||||
if (!dropdown_wishlists.dropdown('get value')) {
|
setDropdownWishlistsSelection();
|
||||||
if (wishthis.$_GET.id) {
|
|
||||||
dropdown_wishlists.dropdown('set selected', wishthis.$_GET.id);
|
|
||||||
} else {
|
|
||||||
if (Object.keys(wishlists).length >= 1) {
|
|
||||||
var first_wishlist_id = Object.keys(wishlists)[0];
|
|
||||||
|
|
||||||
dropdown_wishlists.dropdown('set selected', first_wishlist_id);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Open add wish modal */
|
/** Open add wish modal */
|
||||||
if (wishthis.$_GET.wish_add) {
|
if (wishthis.$_GET.wish_add) {
|
||||||
|
@ -63,15 +53,7 @@ $(function () {
|
||||||
|
|
||||||
if (wishlist_id) {
|
if (wishlist_id) {
|
||||||
/** Get wishlist */
|
/** Get wishlist */
|
||||||
const get_wishlist = new URLSearchParams(
|
fetch('/api/wishlists/' + wishlist_id, { method: 'GET' })
|
||||||
{
|
|
||||||
'module' : 'wishlists',
|
|
||||||
'page' : 'api',
|
|
||||||
|
|
||||||
'wishlist_id' : wishlist_id,
|
|
||||||
}
|
|
||||||
);
|
|
||||||
fetch('/?' + get_wishlist, { method: 'GET' })
|
|
||||||
.then(handleFetchError)
|
.then(handleFetchError)
|
||||||
.then(handleFetchResponse)
|
.then(handleFetchResponse)
|
||||||
.then(function(response) {
|
.then(function(response) {
|
||||||
|
@ -81,7 +63,7 @@ $(function () {
|
||||||
wishlist = response.results;
|
wishlist = response.results;
|
||||||
|
|
||||||
/** Set share link */
|
/** Set share link */
|
||||||
$('.wishlist-share').attr('href', '/?page=wishlist&hash=' + wishlist.hash);
|
$('.wishlist-share').attr('href', '/wishlist/' + wishlist.hash);
|
||||||
|
|
||||||
/** Enable wishlist options buttons */
|
/** Enable wishlist options buttons */
|
||||||
$('.button.wishlist-wish-add').removeClass('disabled');
|
$('.button.wishlist-wish-add').removeClass('disabled');
|
||||||
|
@ -93,22 +75,7 @@ $(function () {
|
||||||
/** Update URL */
|
/** Update URL */
|
||||||
urlParams.set('id', wishlist_id);
|
urlParams.set('id', wishlist_id);
|
||||||
|
|
||||||
const params_url = new URLSearchParams(
|
updateURL();
|
||||||
{
|
|
||||||
'module' : 'url',
|
|
||||||
'page' : 'api',
|
|
||||||
|
|
||||||
'url' : window.btoa(urlParams.toString()),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
fetch('/?' + params_url, {
|
|
||||||
method: 'GET'
|
|
||||||
})
|
|
||||||
.then(handleFetchError)
|
|
||||||
.then(handleFetchResponse)
|
|
||||||
.then(function(response) {
|
|
||||||
window.history.pushState(null, document.title, response.data.url_pretty);
|
|
||||||
});
|
|
||||||
|
|
||||||
/** Get wishlist cards/wishes */
|
/** Get wishlist cards/wishes */
|
||||||
|
|
||||||
|
@ -308,7 +275,7 @@ $(function () {
|
||||||
var formData = new URLSearchParams(new FormData(formRename[0]));
|
var formData = new URLSearchParams(new FormData(formRename[0]));
|
||||||
formData.append('wishlist_id', wishthis.$_GET.id);
|
formData.append('wishlist_id', wishthis.$_GET.id);
|
||||||
|
|
||||||
fetch('/?page=api&module=wishlists', {
|
fetch('/api/wishlists', {
|
||||||
method : 'PUT',
|
method : 'PUT',
|
||||||
body : formData,
|
body : formData,
|
||||||
})
|
})
|
||||||
|
@ -360,61 +327,61 @@ $(function () {
|
||||||
/**
|
/**
|
||||||
* Delete Wishlist
|
* Delete Wishlist
|
||||||
*/
|
*/
|
||||||
$(document).on('click', '.options .wishlist-delete', function () {
|
$(document).on('click', '.wishlist-options .wishlist-delete', function () {
|
||||||
var wishlist_id = $('.ui.dropdown.wishlists').dropdown('get value');
|
var wishlist_id = wishthis.$_GET.id;
|
||||||
|
|
||||||
if (wishlist_id) {
|
if (wishlist_id) {
|
||||||
var modalDefault = $('.ui.modal.default');
|
var modalDefault = $('.ui.modal.default');
|
||||||
|
|
||||||
modalDefault
|
modalDefault
|
||||||
.modal({
|
.modal({
|
||||||
title : wishthis.strings.wishlist.delete.title,
|
'title' : wishthis.strings.modal.wishlist.delete.title,
|
||||||
class : 'tiny',
|
'class' : 'tiny',
|
||||||
content : wishthis.strings.wishlist.delete.content.replace('WISHLIST_NAME', $('.ui.dropdown.wishlists').dropdown('get text')),
|
'content' : wishthis.strings.modal.wishlist.delete.content.replace('WISHLIST_NAME', $('.ui.dropdown.wishlists').dropdown('get text')),
|
||||||
actions : [
|
'actions' : [
|
||||||
{
|
{
|
||||||
text : wishthis.strings.wishlist.delete.approve,
|
text : wishthis.strings.modal.wishlist.delete.approve,
|
||||||
class: 'approve red'
|
class: 'approve red'
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
text : wishthis.strings.wishlist.delete.deny,
|
text : wishthis.strings.modal.wishlist.delete.deny,
|
||||||
class: 'deny'
|
class: 'deny'
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
autoShow : true,
|
'autoShow' : true,
|
||||||
onApprove: function (buttonApprove) {
|
'onApprove': function (buttonApprove) {
|
||||||
buttonApprove.addClass('loading');
|
buttonApprove.addClass('loading');
|
||||||
|
|
||||||
$('.ui.dropdown.wishlists').api({
|
var delete_wishlist = new URLSearchParams(
|
||||||
action: 'delete wishlist',
|
{
|
||||||
method: 'DELETE',
|
'wishlist_id' : wishlist_id,
|
||||||
data: {
|
|
||||||
'wishlistID' : wishlist_id
|
|
||||||
},
|
|
||||||
on: 'now',
|
|
||||||
onSuccess: function (response, wishlists) {
|
|
||||||
$('.wishlist-cards .column').fadeOut(800);
|
|
||||||
|
|
||||||
wishlists.dropdown('clear');
|
|
||||||
|
|
||||||
urlParams.delete('id');
|
|
||||||
|
|
||||||
$('body').toast({ message : wishthis.strings.toast.wishlist.delete });
|
|
||||||
|
|
||||||
modalDefault.modal('hide');
|
|
||||||
|
|
||||||
setTimeout(() => {
|
|
||||||
$('.ui.dropdown.wishlists').api('query');
|
|
||||||
}, 200);
|
|
||||||
}
|
}
|
||||||
|
);
|
||||||
|
fetch('/api/wishlists', {
|
||||||
|
'method' : 'DELETE',
|
||||||
|
'body' : delete_wishlist,
|
||||||
|
})
|
||||||
|
.then(handleFetchError)
|
||||||
|
.then(handleFetchResponse)
|
||||||
|
.then(function(response) {
|
||||||
|
$('.wishlist-cards .column').fadeOut(800);
|
||||||
|
|
||||||
|
urlParams.delete('id');
|
||||||
|
wishthis.$_GET.id = null;
|
||||||
|
updateURL();
|
||||||
|
setDropdownWishlistsSelection();
|
||||||
|
|
||||||
|
$('.ui.dropdown.wishlists').api('query');
|
||||||
|
|
||||||
|
modalDefault.modal('hide');
|
||||||
|
|
||||||
|
$('body').toast({ message : wishthis.strings.toast.wishlist.delete });
|
||||||
|
})
|
||||||
|
.catch(handleFetchCatch)
|
||||||
|
.finally(function() {
|
||||||
|
buttonApprove.removeClass('loading');
|
||||||
});
|
});
|
||||||
|
|
||||||
/**
|
|
||||||
* Return false is currently not working.
|
|
||||||
*
|
|
||||||
* @version 2.8.8
|
|
||||||
* @see https://github.com/fomantic/Fomantic-UI/issues/2105
|
|
||||||
*/
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -664,7 +631,7 @@ $(function () {
|
||||||
|
|
||||||
var formData = new URLSearchParams(new FormData(formWishlistCreate[0]));
|
var formData = new URLSearchParams(new FormData(formWishlistCreate[0]));
|
||||||
|
|
||||||
fetch('/?page=api&module=wishlists', {
|
fetch('/api/wishlists', {
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : formData
|
body : formData
|
||||||
})
|
})
|
||||||
|
@ -677,7 +644,9 @@ $(function () {
|
||||||
|
|
||||||
$('body').toast({ message: wishthis.strings.toast.wish.create });
|
$('body').toast({ message: wishthis.strings.toast.wish.create });
|
||||||
|
|
||||||
$('.ui.dropdown.wishlists').api('query');
|
$('.ui.dropdown.wishlists')
|
||||||
|
.api('query')
|
||||||
|
.dropdown('set value', response.data.lastInsertId);
|
||||||
})
|
})
|
||||||
.finally(() => {
|
.finally(() => {
|
||||||
formWishlistCreate.removeClass('loading');
|
formWishlistCreate.removeClass('loading');
|
||||||
|
@ -846,5 +815,34 @@ $(function () {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update URL
|
||||||
|
*/
|
||||||
|
function updateURL() {
|
||||||
|
fetch('/api/url/' + window.btoa('/?' + urlParams.toString()), { method: 'GET' })
|
||||||
|
.then(handleFetchError)
|
||||||
|
.then(handleFetchResponse)
|
||||||
|
.then(function(response) {
|
||||||
|
window.history.pushState(null, document.title, response.data.url_pretty);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set dropdown wishlists seelction
|
||||||
|
*/
|
||||||
|
function setDropdownWishlistsSelection() {
|
||||||
|
var dropdown_wishlists = $('.ui.dropdown.wishlists');
|
||||||
|
|
||||||
|
if (!dropdown_wishlists.dropdown('get value')) {
|
||||||
|
if (wishthis.$_GET.id) {
|
||||||
|
dropdown_wishlists.dropdown('set selected', wishthis.$_GET.id);
|
||||||
|
} else {
|
||||||
|
if (Object.keys(wishlists).length >= 1) {
|
||||||
|
var first_wishlist_id = Object.keys(wishlists)[0];
|
||||||
|
|
||||||
|
dropdown_wishlists.dropdown('set selected', first_wishlist_id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -90,7 +90,7 @@ $(function() {
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
fetch('/?page=api&module=wishlists-saved', {
|
fetch('/api/wishlists-saved', {
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : formData
|
body : formData
|
||||||
})
|
})
|
||||||
|
@ -113,14 +113,7 @@ $(function() {
|
||||||
});
|
});
|
||||||
|
|
||||||
/** Determine if list is saved */
|
/** Determine if list is saved */
|
||||||
const params_ws_saved = new URLSearchParams(
|
fetch('/api/wishlists-saved', {
|
||||||
{
|
|
||||||
'module' : 'wishlists-saved',
|
|
||||||
'page' : 'api',
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
fetch('/?' + params_ws_saved, {
|
|
||||||
method : 'GET',
|
method : 'GET',
|
||||||
})
|
})
|
||||||
.then(handleFetchError)
|
.then(handleFetchError)
|
||||||
|
@ -168,7 +161,7 @@ $(function() {
|
||||||
|
|
||||||
buttonRequest.addClass('disabled loading');
|
buttonRequest.addClass('disabled loading');
|
||||||
|
|
||||||
fetch('/?page=api&module=wishlists', {
|
fetch('/api/wishlists', {
|
||||||
method : 'POST',
|
method : 'POST',
|
||||||
body : formData
|
body : formData
|
||||||
})
|
})
|
||||||
|
|
|
@ -72,9 +72,11 @@ class URL
|
||||||
*/
|
*/
|
||||||
public function isPretty(): bool
|
public function isPretty(): bool
|
||||||
{
|
{
|
||||||
$isPretty = 1 === preg_match('/^\/[a-z0-9\/\-]+$/', $this->url);
|
if ('/?' === substr($this->url, 0, 2)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
return $isPretty;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue