diff --git a/database/yggtracker.mwb b/database/yggtracker.mwb index 75b7bf5..fd1d697 100644 Binary files a/database/yggtracker.mwb and b/database/yggtracker.mwb differ diff --git a/src/library/database.php b/src/library/database.php index a6e986b..05bf6b9 100644 --- a/src/library/database.php +++ b/src/library/database.php @@ -1344,28 +1344,17 @@ class Database { } // Magnet star - public function addMagnetStar(int $magnetId, int $userId, int $timeAdded) : int { + public function addMagnetStar(int $magnetId, int $userId, bool $value, int $timeAdded) : int { $this->_debug->query->insert->total++; - $query = $this->_db->prepare('INSERT INTO `magnetStar` SET `magnetId` = ?, `userId` = ?, `timeAdded` = ?'); + $query = $this->_db->prepare('INSERT INTO `magnetStar` SET `magnetId` = ?, `userId` = ?, `value` = ?, `timeAdded` = ?'); - $query->execute([$magnetId, $userId, $timeAdded]); + $query->execute([$magnetId, $userId, (int) $value, $timeAdded]); return $this->_db->lastInsertId(); } - public function deleteMagnetStarByUserId(int $magnetId, int $userId) : int { - - $this->_debug->query->delete->total++; - - $query = $this->_db->prepare('DELETE FROM `magnetStar` WHERE `magnetId` = ? AND `userId` = ?'); - - $query->execute([$magnetId, $userId]); - - return $query->rowCount(); - } - public function findMagnetStarsTotalByMagnetId(int $magnetId) : int { $this->_debug->query->select->total++; @@ -1388,26 +1377,15 @@ class Database { return $query->fetch()->result; } - public function findMagnetStarsTotal(int $magnetId, int $userId) : int { + public function findLastMagnetStarValue(int $magnetId, int $userId) : bool { $this->_debug->query->select->total++; - $query = $this->_db->prepare('SELECT COUNT(*) AS `result` FROM `magnetStar` WHERE `magnetId` = ? AND `userId` = ?'); + $query = $this->_db->prepare('SELECT * FROM `magnetStar` WHERE `magnetId` = ? AND `userId` = ? ORDER BY `magnetStarId` DESC'); $query->execute([$magnetId, $userId]); - return $query->fetch()->result; - } - - public function findMagnetStarByUserId(int $magnetId, int $userId) : int { - - $this->_debug->query->select->total++; - - $query = $this->_db->prepare('SELECT * FROM `magnetStar` WHERE `magnetId` = ? AND `userId` = ?'); - - $query->execute([$magnetId, $userId]); - - return $query->fetch(); + return $query->rowCount() ? (bool) $query->fetch()->value : false; } // Magnet download diff --git a/src/public/action.php b/src/public/action.php index 833a222..818eb22 100644 --- a/src/public/action.php +++ b/src/public/action.php @@ -420,16 +420,13 @@ switch (isset($_GET['target']) ? urldecode($_GET['target']) : false) // Request valid else { - // Star exists, trigger delete - if ($db->findMagnetStarsTotal($magnet->magnetId, $userId)) - { - $db->deleteMagnetStarByUserId($magnet->magnetId, $userId); - } - else - { - // Star not exists, trigger add - $db->addMagnetStar($magnet->magnetId, $userId, time()); - } + // Save value + $db->addMagnetStar( + $magnet->magnetId, + $userId, + !$db->findLastMagnetStarValue($magnet->magnetId, $userId), + time() + ); // Redirect to edit page header( diff --git a/src/public/index.php b/src/public/index.php index 27d14bc..9a87120 100644 --- a/src/public/index.php +++ b/src/public/index.php @@ -144,7 +144,7 @@ else 'star' => (object) [ 'total' => $db->findMagnetStarsTotalByMagnetId($magnet->magnetId), - 'status' => $db->findMagnetStarsTotal($magnet->magnetId, $userId), + 'status' => $db->findLastMagnetStarValue($magnet->magnetId, $userId), ], 'access' => (object) [ @@ -253,14 +253,6 @@ echo '' . PHP_EOL ?> - - metaDescription) { ?> @@ -319,7 +311,7 @@ echo '' . PHP_EOL ?> - + star->status) { ?> diff --git a/src/public/magnet.php b/src/public/magnet.php index c725625..3c1118e 100644 --- a/src/public/magnet.php +++ b/src/public/magnet.php @@ -132,7 +132,7 @@ else 'star' => (object) [ 'total' => $db->findMagnetStarsTotalByMagnetId($magnet->magnetId), - 'status' => $db->findMagnetStarsTotal($magnet->magnetId, $userId), + 'status' => $db->findLastMagnetStarValue($magnet->magnetId, $userId), ], 'access' => (object) [