Refactor TorrentController

This commit is contained in:
Hyleus
2017-12-30 23:36:24 +01:00
parent f11c8e873f
commit c7c363367a
3 changed files with 10 additions and 7 deletions
+5 -6
View File
@@ -134,15 +134,14 @@ class TorrentController extends Controller
* @param Torrent $torrent
* @return Response
*/
public function Bookmark($id)
public function bookmark($id)
{
$torrent = Torrent::withAnyStatus()->findOrFail($id);
$bookmarks = Auth::user()->bookmarks();
if ($bookmarks->where('torrent_id', '=', $torrent->id)->first() === null) {
$bookmarks->attach($torrent->id);
return back()->with(Toastr::success('Torrent Has Been Bookmarked Successfully!', 'Yay', ['options']));
} else {
if (Auth::user()->hasBookmarked($torrent->id)) {
return back()->with(Toastr::error('Torrent has already been bookmarked.', 'Error', ['options']));
} else {
Auth::user()->bookmarks()->attach($torrent->id);
return back()->with(Toastr::success('Torrent Has Been Bookmarked Successfully!', 'Yay', ['options']));
}
}
+4
View File
@@ -239,6 +239,10 @@ class User extends Authenticatable
return $this->belongsToMany(\App\Torrent::class, 'bookmarks', 'user_id', 'torrent_id')->withTimeStamps();
}
public function hasBookmarked($torrent_id) {
return $this->bookmarks()->where('torrent_id', '=', $torrent_id)->first() !== null;
}
/**
* Get all of follows for the user.
*/
+1 -1
View File
@@ -87,7 +87,7 @@ Route::group(['middleware' => 'auth'], function () {
// Bookmarks
Route::get('/bookmarks', 'BookmarkController@bookmarks')->name('bookmarks');
Route::any('/torrents/bookmark/{id}', 'TorrentController@Bookmark')->name('bookmark');
Route::any('/torrents/bookmark/{id}', 'TorrentController@bookmark')->name('bookmark');
Route::any('/torrents/unbookmark/{id}', 'BookmarkController@unBookmark')->name('unbookmark');
// User/Torrent Report