API call to add/remove favorites (#810)

add API methods pwg.users.addFavorite and pwg.users.removeFavorite
This commit is contained in:
Dave Anderson 2019-07-15 10:58:23 -04:00 committed by Pierrick Le Gall
parent d406a12d45
commit 1d113c002b
2 changed files with 69 additions and 0 deletions

View file

@ -632,6 +632,55 @@ SELECT
));
}
/**
* API method
* Adds a favorite image for the current user
* @param mixed[] $params
*/
function ws_addFavorite($params, &$service)
{
global $user;
if (is_a_guest())
{
return new PwgError(403, 'User must be logged in.');
}
$query = '
INSERT IGNORE INTO '.FAVORITES_TABLE.'
(image_id,user_id)
VALUES
('.$params['image_id'].','.$user['id'].')
;';
pwg_query($query);
return true;
}
/**
* API method
* Removes a favorite image for the current user
* @param mixed[] $params
*/
function ws_removeFavorite($params, &$service)
{
global $user;
if (is_a_guest())
{
return new PwgError(403, 'User must be logged in.');
}
$query = '
DELETE FROM '.FAVORITES_TABLE.'
WHERE user_id = '.$user['id'].'
AND image_id = '.$params['image_id'].'
;';
pwg_query($query);
return true;
}
/**
* API method
* Returns the favorite images of the current user

20
ws.php
View file

@ -1077,6 +1077,26 @@ enabled_high, registration_date, registration_date_string, registration_date_sin
array('admin_only'=>true, 'post_only'=>true)
);
$service->addMethod(
'pwg.users.addFavorite',
'ws_addFavorite',
array(
'image_id' => array('type'=>WS_TYPE_ID)
),
'Adds the indicated image to the current user\'s favorite images.',
$ws_functions_root . 'pwg.users.php'
);
$service->addMethod(
'pwg.users.removeFavorite',
'ws_removeFavorite',
array(
'image_id' => array('type'=>WS_TYPE_ID)
),
'Removes the indicated image from the current user\'s favorite images.',
$ws_functions_root . 'pwg.users.php'
);
$service->addMethod(
'pwg.users.getFavorites',
'ws_getFavorites',