169 lines
5.4 KiB
PHP
169 lines
5.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Enums\PlaylistContentType;
|
|
use App\Models\File;
|
|
use App\Models\Playlist;
|
|
use App\Models\PlaylistContent;
|
|
use DateInterval;
|
|
use DateTime;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
class PlaylistController extends Controller
|
|
{
|
|
|
|
/**
|
|
* List all playlists
|
|
* @return string
|
|
*/
|
|
public function index(Request $request) {
|
|
if (!isset($request->per_page) || is_null($request)) {
|
|
$pagination = 5;
|
|
} else {
|
|
$pagination = $request->per_page;
|
|
}
|
|
|
|
return Playlist::searchFilter($request)
|
|
->with('creator')
|
|
->orderBy('name')
|
|
->paginate($pagination)
|
|
->toJson();
|
|
}
|
|
|
|
/**
|
|
* Save a playlist and its tracks list to the DB
|
|
* @param Request $request
|
|
* @return mixed
|
|
* @throws \DateMalformedIntervalStringException
|
|
*/
|
|
public function store(Request $request) {
|
|
$user = Auth::user();
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'tracks' => 'required|array'
|
|
]);
|
|
|
|
$totalLength = new DateTime('00:00:00');
|
|
|
|
$dbPlaylist = Playlist::create([
|
|
'name' => $request->get('name'),
|
|
'creator_id' => $user->id,
|
|
'description' => $request->description,
|
|
]);
|
|
|
|
foreach($request->tracks as $file) {
|
|
switch ($file['type']) {
|
|
case 'audioclip':
|
|
$model = File::whereId($file['id'])->first();
|
|
break;
|
|
case 'block':
|
|
//Todo: $model = Block::whereId($file['id'])->first();
|
|
break;
|
|
case 'stream':
|
|
//Todo: $model = Stream::whereId($file['id'])->first();
|
|
break;
|
|
}
|
|
$totalLength->add(new DateInterval($model->length));
|
|
PlaylistContent::create([
|
|
'playlist_id' => $dbPlaylist->id,
|
|
'file_id' => ($file['type'] === 'audioclip') ? $file['id'] : null,
|
|
'block_id' => ($file['type'] === 'block') ? $file['id'] : null,
|
|
'stream_id' => ($file['type'] === 'stream') ? $file['id'] : null,
|
|
'type' => PlaylistContentType::fromName($file['type']),
|
|
'position' => $file['position'],
|
|
'trackoffset' => 0, //ToDo: understand this field
|
|
'cliplength' => $model->length,
|
|
'cuein' => ($model->cuein) ?? 0,
|
|
'cueout' => ($model->cuein) ?? $model->length,
|
|
'fadein' => 0,
|
|
'fadeout' => 0,
|
|
]);
|
|
}
|
|
|
|
$dbPlaylist->update(['length' => $totalLength])->save();
|
|
|
|
return $dbPlaylist->with('tracks')->toJson();
|
|
}
|
|
|
|
/**
|
|
* Return a playlist with its tracks list
|
|
* @param $id
|
|
* @return mixed
|
|
*/
|
|
public function show($id) {
|
|
return Playlist::whereId($id)->with('tracks')->with('tracks.file')->first()->toJson();
|
|
}
|
|
|
|
/**
|
|
* Update a playlist and its tracks list
|
|
* @param Request $request
|
|
* @param $id
|
|
* @return mixed
|
|
* @throws \DateMalformedIntervalStringException
|
|
*/
|
|
public function update(Request $request, $id) {
|
|
$user = Auth::user();
|
|
$request->validate([
|
|
'name' => 'required',
|
|
'tracks' => 'required|array'
|
|
]);
|
|
|
|
$totalLength = new DateTime('00:00:00');
|
|
|
|
$playlist = Playlist::whereId($id)->first();
|
|
$playlist->fill($request->all())->save();
|
|
foreach ($request->tracks as $file) {
|
|
$totalLength->add(new DateInterval($file['length']));
|
|
switch ($file['type']) {
|
|
case 'audioclip':
|
|
$model = File::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'file_id' => $file['id'],
|
|
];
|
|
break;
|
|
case 'block':
|
|
//Todo: $model = Block::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'block_id' => $file['id'],
|
|
];
|
|
break;
|
|
case 'stream':
|
|
//Todo: $model = Stream::whereId($file['id'])->first();
|
|
$data = [
|
|
'playlist_id' => $playlist->id,
|
|
'stream_id' => $file['id'],
|
|
];
|
|
break;
|
|
}
|
|
PlaylistContent::updateOrCreate(
|
|
$data,
|
|
[
|
|
'type' => PlaylistContentType::fromName($file['type']),
|
|
'position' => $file['position'],
|
|
'trackoffset' => 0, //ToDo: understand this field
|
|
'cliplength' => $file['length'],
|
|
'cuein' => $file['cuein'],
|
|
'cueout' => $file['cueout'],
|
|
'fadein' => 0,
|
|
'fadeout' => 0,
|
|
]
|
|
);
|
|
}
|
|
return $playlist->with('tracks')->toJson();
|
|
}
|
|
|
|
/**
|
|
* Delete a playlist and its tracks list
|
|
* @param $id
|
|
* @return true
|
|
*/
|
|
public function destroy($id) {
|
|
PlaylistContent::where('playlist_id', $id)->delete();
|
|
Playlist::destroy($id);
|
|
return true;
|
|
}
|
|
}
|