We are no longer offering accounts on this server. Consider https://gitlab.freedesktop.org/ as a place to host projects.

Commit 8638a301 authored by Jonas Haraldsson's avatar Jonas Haraldsson

Clean up love/unlove

Moved sql from api/TrackXML.php functions into data/Track.php functions.
love/unlove calls now only return True if a row is added/deleted from
Loved_Tracks table, False in any other case.
parent 2bbef339
......@@ -167,13 +167,16 @@ class TrackXML {
public static function love($artist, $name, $userid) {
try {
$track = new Track($name, $artist);
$track->love($userid);
$res = $track->love($userid);
} catch (Exception $e) {
return(XML::error('failed', '7', 'Invalid resource specified'));
return XML::error('failed', '7', 'Invalid resource specified');
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
if(!$res) {
$xml = XML::error('failed', '7', 'Invalid resource specified');
} else {
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
}
return $xml;
}
......@@ -193,13 +196,16 @@ class TrackXML {
public static function unlove($artist, $name, $userid) {
try {
$track = new Track($name, $artist);
$track->unlove($userid);
$res = $track->unlove($userid);
} catch (Exception $e) {
return(XML::error('failed', '7', 'Invalid resource specified'));
return XML::error('failed', '7', 'Invalid resource specified');
}
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
if(!$res) {
$xml = XML::error('failed', '7', 'Invalid resource specified');
} else {
$xml = new SimpleXMLElement('<lfm status="ok"></lfm>');
}
return $xml;
}
......
......@@ -46,6 +46,8 @@ class Track {
*
* @param string $name The name of the track to load
* @param string $artist The name of the artist who recorded this track
*
* @todo Should we call Track::create() instead of throwing "No such track" exception?
*/
function __construct($name, $artist) {
global $adodb;
......@@ -350,33 +352,45 @@ class Track {
/**
* Love a track
*
* @param int $userid The user loving this track
* @param int $userid The user loving this track.
* @return bool True on success, False on fail.
*
*/
function love($userid) {
global $adodb;
$query = 'INSERT INTO Loved_Tracks (userid, track, artist, time) VALUES(?,?,?,?)';
$params = array((int) $userid, $this->name, $this->artist_name, time());
try {
$adodb->Execute('INSERT INTO Loved_Tracks VALUES ('
. $userid . ', '
. $adodb->qstr($this->name) . ', '
. $adodb->qstr($this->artist_name) . ', '
. time() . ')');
} catch (Exception $e) {}
$adodb->Execute($query, $params);
$res = $adodb->Affected_Rows();
} catch (Exception $e) {
reportError($e->GetMessage(), $e->GetTraceAsString());
return False;
}
return (bool) $res;
}
/**
* Unlove a track
*
* @param int $userid The user unloving this track
* @param int $userid The user unloving this track.
* @return bool True on success, False on fail.
*/
function unlove($userid) {
global $adodb;
$query = 'DELETE FROM Loved_Tracks WHERE userid=? AND track=? AND artist=?';
$params = array((int) $userid, $this->name, $this->artist_name);
try {
$adodb->Execute('DELETE FROM Loved_Tracks WHERE userid=' . $userid
. ' AND track=' . $adodb->qstr($this->name)
. ' AND artist=' . $adodb->qstr($this->artist_name));
} catch (Exception $e) {}
$adodb->Execute($query, $params);
$res = $adodb->Affected_Rows();
} catch (Exception $e) {
reportError($e->GetMessage(), $e->GetTraceAsString());
return False;
}
return (bool) $res;
}
/**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment