Files
UNIT3D-Community-Edition/app/Http/Controllers/PlaylistTorrentController.php
HDVinnie 910f64a35c fix: playlist torrent controller
- this adds proper validation to make sure the torrent_id and playlist_id are unique within a entry like the migration.
2020-05-15 13:00:16 -04:00

74 lines
2.5 KiB
PHP

<?php
/**
* NOTICE OF LICENSE.
*
* UNIT3D Community Edition is open-sourced software licensed under the GNU Affero General Public License v3.0
* The details is bundled with this project in the file LICENSE.txt.
*
* @project UNIT3D Community Edition
*
* @author HDVinnie <hdinnovations@protonmail.com>
* @license https://www.gnu.org/licenses/agpl-3.0.en.html/ GNU Affero General Public License v3.0
*/
namespace App\Http\Controllers;
use App\Models\Playlist;
use App\Models\PlaylistTorrent;
use Illuminate\Http\Request;
class PlaylistTorrentController extends Controller
{
/**
* Attach A Torrent To A Playlist.
*
* @param \Illuminate\Http\Request $request
*
* @return \Illuminate\Http\RedirectResponse
*/
public function store(Request $request)
{
$user = auth()->user();
$playlist = Playlist::findOrFail($request->input('playlist_id'));
abort_unless($user->id === $playlist->user_id, 403);
$playlist_torrent = new PlaylistTorrent();
$playlist_torrent->playlist_id = $playlist->id;
$playlist_torrent->torrent_id = $request->input('torrent_id');
$v = validator($playlist_torrent->toArray(), [
'playlist_id' => 'required|numeric|exists:playlists,id|unique:playlist_torrents,playlist_id,NULL,NULL,torrent_id,'.$request->input('torrent_id'),
'torrent_id' => 'required|numeric|exists:torrents,id|unique:playlist_torrents,torrent_id,NULL,NULL,playlist_id,'.$request->input('playlist_id'),
]);
if ($v->fails()) {
return redirect()->route('playlists.show', ['id' => $playlist->id])
->withErrors($v->errors());
}
$playlist_torrent->save();
return redirect()->route('playlists.show', ['id' => $playlist->id])
->withSuccess('Torrent Has Successfully Been Attached To Your Playlist.');
}
/**
* Detach A Torrent From A Playlist.
*
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy($id)
{
$user = auth()->user();
$playlist_torrent = PlaylistTorrent::findOrFail($id);
abort_unless($user->group->is_modo || $user->id === $playlist_torrent->playlist->user_id, 403);
$playlist_torrent->delete();
return redirect()->route('playlists.show', ['id' => $playlist_torrent->playlist->id])
->withSuccess('Torrent Has Successfully Been Detached From Your Playlist.');
}
}