This repository has been archived by the owner on Nov 2, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Favour): Add full favour support
- Loading branch information
Showing
16 changed files
with
226 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/3/16 | ||
* Time: 10:39 | ||
*/ | ||
|
||
namespace apps\controllers\api\v1; | ||
|
||
use apps\models\api\v1\form\TorrentsBookmarkForm; | ||
use Rid\Http\ApiController; | ||
|
||
class TorrentsController extends ApiController | ||
{ | ||
public function actionBookmark() { | ||
if ($this->checkMethod('POST')) { | ||
$bookmark = new TorrentsBookmarkForm(); | ||
$bookmark->setData(app()->request->post()); | ||
$success = $bookmark->validate(); | ||
if (!$success) { | ||
return [ | ||
'success' => false, | ||
'errors' => $bookmark->getErrors() | ||
]; | ||
} else { | ||
$ret = $bookmark->updateRecord(); | ||
return array_merge( | ||
['success' => true], | ||
$ret | ||
); | ||
} | ||
} else { | ||
return $this->buildMethodFailMsg('POST'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/3/16 | ||
* Time: 11:30 | ||
*/ | ||
|
||
namespace apps\models\api\v1\form; | ||
|
||
|
||
use Rid\Validators\Validator; | ||
|
||
class TorrentsBookmarkForm extends Validator | ||
{ | ||
public $tid; | ||
|
||
public static function inputRules() | ||
{ | ||
return [ | ||
'tid' => 'required | Integer' | ||
]; | ||
} | ||
|
||
public static function callbackRules() { | ||
return ['isExistTorrent']; | ||
} | ||
|
||
protected function isExistTorrent() { | ||
$torrent_exist = app()->pdo->createCommand('SELECT COUNT(`id`) FROM `torrents` WHERE `id` = :tid')->bindParams([ | ||
'tid' => $this->tid | ||
])->queryScalar(); | ||
if ($torrent_exist == 0) { | ||
$this->buildCallbackFailMsg('Torrent', 'The torrent id ('. $this->tid. ') is not exist in our database'); | ||
} | ||
} | ||
|
||
public function updateRecord() { | ||
$bookmark_exist = app()->pdo->createCommand('SELECT `id` FROM `bookmarks` WHERE `uid` = :uid AND `tid` = :tid ')->bindParams([ | ||
'uid' => app()->user->getId(), | ||
'tid' => $this->tid | ||
])->queryScalar() ?: 0; | ||
if ($bookmark_exist > 0) { // Delete the exist record | ||
app()->pdo->createCommand('DELETE FROM `bookmarks` WHERE `id` = :bid')->bindParams([ | ||
'bid' => $bookmark_exist | ||
])->execute(); | ||
app()->redis->del('User:' . app()->user->getId() . ':bookmark_array'); | ||
|
||
return ['msg' => 'Delete Old Bookmark Success', 'result' => 'deleted']; | ||
} else { // Add new record | ||
app()->pdo->createCommand('INSERT INTO `bookmarks` (`uid`, `tid`) VALUES (:uid, :tid)')->bindParams([ | ||
'uid' => app()->user->getId(), | ||
'tid' => $this->tid | ||
])->execute(); | ||
app()->redis->del('User:' . app()->user->getId() . ':bookmark_array'); | ||
|
||
return ['msg' => 'Add New Bookmark Success', 'result' => 'added']; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,25 @@ | ||
;layui.use(['layer', 'form','element','laypage','jquery'], function(){ | ||
let $=layui.jquery; | ||
let $ = layui.jquery; | ||
let layer = layui.layer; | ||
let api_point = '/api/v1'; | ||
|
||
// Add favour action | ||
$('.torrent-favour > i').click(function () { | ||
let star = $(this); | ||
// TODO Do ajax to api, if success then change the star | ||
let old_is_stared = star.hasClass('fas'); | ||
star.toggleClass('fas',!old_is_stared).toggleClass('far',old_is_stared); | ||
new NoticeJs({ | ||
text: 'Torrent add/remove from your favour', | ||
position: 'bottomRight', | ||
}).show(); | ||
// TODO Notice user | ||
}) | ||
// Add/Remove favour action | ||
$('.torrent-favour').click(function () { | ||
let that = $(this); | ||
let tid = that.attr('data-tid'); | ||
let star = that.find(' > i'); | ||
|
||
$.post(api_point + '/torrents/bookmark', {'tid': tid}, function (res) { | ||
if (res.success) { | ||
let old_is_stared = star.hasClass('fas'); | ||
star.toggleClass('fas', !old_is_stared).toggleClass('far', old_is_stared); | ||
layer.msg(`Torrent(${tid}) ${res.result} from your favour successfully`, { | ||
icon: 6, | ||
offset: 'rb', | ||
}); | ||
} else { | ||
layer.alert(res.errors.join(', '), {icon: 2}); | ||
} | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Created by PhpStorm. | ||
* User: Rhilip | ||
* Date: 2019/3/16 | ||
* Time: 10:41 | ||
*/ | ||
|
||
namespace Rid\Http; | ||
|
||
|
||
class ApiController extends Controller | ||
{ | ||
/** | ||
* @param array|string $methods | ||
* @return bool | ||
*/ | ||
protected function checkMethod($methods) { | ||
if (is_string($methods)) $methods = [$methods]; | ||
|
||
foreach ($methods as $method) { | ||
if (strtolower(app()->request->method()) == strtolower($method)) | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
protected function buildMethodFailMsg($want_methods) | ||
{ | ||
if (is_array($want_methods)) $want_methods = implode(',', $want_methods); | ||
|
||
app()->response->setStatusCode(405); | ||
$method = app()->request->method(); | ||
|
||
return [ | ||
'error' => 'Method Not Allowed', | ||
'detail' => [ | ||
'method' => "The method `$method` is not allowed, You should use `$want_methods` in this action." | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.