2013-07-25 23:33:04 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Api\Web;
|
|
|
|
|
|
|
|
use Commands\DeleteTrackCommand;
|
|
|
|
use Commands\EditTrackCommand;
|
|
|
|
use Commands\UploadTrackCommand;
|
2013-07-27 02:15:07 +02:00
|
|
|
use Cover;
|
|
|
|
use Entities\Image;
|
2013-07-25 23:33:04 +02:00
|
|
|
use Entities\Track;
|
2013-07-26 12:29:58 +02:00
|
|
|
use Illuminate\Support\Facades\Auth;
|
|
|
|
use Illuminate\Support\Facades\Input;
|
|
|
|
use Illuminate\Support\Facades\Response;
|
2013-07-25 23:33:04 +02:00
|
|
|
|
|
|
|
class TracksController extends \ApiControllerBase {
|
|
|
|
public function postUpload() {
|
|
|
|
session_write_close();
|
|
|
|
return $this->execute(new UploadTrackCommand());
|
|
|
|
}
|
|
|
|
|
2013-07-28 19:45:21 +02:00
|
|
|
public function postDelete($id) {
|
|
|
|
return $this->execute(new DeleteTrackCommand($id));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function postEdit($id) {
|
|
|
|
return $this->execute(new EditTrackCommand($id, Input::all()));
|
|
|
|
}
|
|
|
|
|
2013-07-28 23:51:35 +02:00
|
|
|
public function getRecent() {
|
|
|
|
$query = Track::summary()->with(['genre', 'user', 'cover'])->whereNotNull('published_at')->orderBy('published_at', 'desc')->take(15);
|
|
|
|
if (!Auth::check() || !Auth::user()->can_see_explicit_content)
|
|
|
|
$query->whereIsExplicit(false);
|
|
|
|
|
|
|
|
$tracks = [];
|
|
|
|
|
|
|
|
foreach ($query->get() as $track) {
|
2013-07-31 13:47:16 +02:00
|
|
|
$tracks[] = $this->mapPublicTrack($track);
|
2013-07-28 23:51:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json($tracks, 200);
|
|
|
|
}
|
|
|
|
|
2013-07-31 13:47:16 +02:00
|
|
|
public function getIndex() {
|
|
|
|
$page = 1;
|
|
|
|
|
|
|
|
if (Input::has('page'))
|
|
|
|
$page = Input::get('page');
|
|
|
|
|
|
|
|
$query = Track::summary()->whereNotNull('published_at');
|
|
|
|
$this->applyFilters($query);
|
|
|
|
|
|
|
|
$totalCount = $query->count();
|
|
|
|
$query->take(30)->skip(30 * ($page - 1));
|
|
|
|
$tracks = [];
|
|
|
|
|
|
|
|
foreach ($query->get() as $track) {
|
|
|
|
$tracks[] = $this->mapPublicTrack($track);
|
|
|
|
}
|
|
|
|
|
|
|
|
return Response::json(["tracks" => $tracks, "current_page" => $page, "total_pages" => ceil($totalCount / 30)], 200);
|
|
|
|
}
|
|
|
|
|
2013-07-25 23:33:04 +02:00
|
|
|
public function getOwned() {
|
2013-07-28 23:51:35 +02:00
|
|
|
$query = Track::summary()->where('user_id', \Auth::user()->id);
|
2013-07-25 23:33:04 +02:00
|
|
|
|
2013-07-26 12:29:58 +02:00
|
|
|
if (Input::has('published')) {
|
2013-07-25 23:33:04 +02:00
|
|
|
$published = \Input::get('published');
|
|
|
|
if ($published)
|
|
|
|
$query->whereNotNull('published_at');
|
|
|
|
else
|
|
|
|
$query->whereNull('published_at');
|
|
|
|
}
|
|
|
|
|
2013-07-31 13:47:16 +02:00
|
|
|
$this->applyFilters($query);
|
2013-07-25 23:33:04 +02:00
|
|
|
|
|
|
|
$dbTracks = $query->get();
|
|
|
|
$tracks = [];
|
|
|
|
|
|
|
|
foreach ($dbTracks as $track) {
|
|
|
|
$tracks[] = [
|
|
|
|
'id' => $track->id,
|
|
|
|
'title' => $track->title,
|
|
|
|
'user_id' => $track->user_id,
|
|
|
|
'slug' => $track->slug,
|
|
|
|
'is_vocal' => $track->is_vocal,
|
|
|
|
'is_explicit' => $track->is_explicit,
|
|
|
|
'is_downloadable' => $track->is_downloadable,
|
2013-07-27 02:15:07 +02:00
|
|
|
'is_published' => $track->isPublished(),
|
2013-07-25 23:33:04 +02:00
|
|
|
'created_at' => $track->created_at,
|
|
|
|
'published_at' => $track->published_at,
|
|
|
|
'duration' => $track->duration,
|
|
|
|
'genre_id' => $track->genre_id,
|
|
|
|
'track_type_id' => $track->track_type_id,
|
2013-07-27 02:15:07 +02:00
|
|
|
'cover_url' => $track->getCoverUrl(Image::SMALL)
|
2013-07-25 23:33:04 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:29:58 +02:00
|
|
|
return Response::json($tracks, 200);
|
2013-07-25 23:33:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getEdit($id) {
|
2013-07-28 08:07:25 +02:00
|
|
|
$track = Track::with('showSongs')->find($id);
|
2013-07-25 23:33:04 +02:00
|
|
|
if (!$track)
|
|
|
|
return $this->notFound('Track ' . $id . ' not found!');
|
|
|
|
|
2013-07-26 12:29:58 +02:00
|
|
|
if ($track->user_id != Auth::user()->id)
|
2013-07-25 23:33:04 +02:00
|
|
|
return $this->notAuthorized();
|
|
|
|
|
2013-07-28 08:07:25 +02:00
|
|
|
$showSongs = [];
|
|
|
|
foreach ($track->showSongs as $showSong) {
|
|
|
|
$showSongs[] = ['id' => $showSong->id, 'title' => $showSong->title];
|
|
|
|
}
|
|
|
|
|
2013-07-26 12:29:58 +02:00
|
|
|
return Response::json([
|
2013-07-25 23:33:04 +02:00
|
|
|
'id' => $track->id,
|
|
|
|
'title' => $track->title,
|
|
|
|
'user_id' => $track->user_id,
|
|
|
|
'slug' => $track->slug,
|
|
|
|
'is_vocal' => (bool)$track->is_vocal,
|
|
|
|
'is_explicit' => (bool)$track->is_explicit,
|
2013-07-27 02:15:07 +02:00
|
|
|
'is_downloadable' => !$track->isPublished() ? true : (bool)$track->is_downloadable,
|
2013-07-25 23:33:04 +02:00
|
|
|
'is_published' => $track->published_at != null,
|
|
|
|
'created_at' => $track->created_at,
|
|
|
|
'published_at' => $track->published_at,
|
|
|
|
'duration' => $track->duration,
|
|
|
|
'genre_id' => $track->genre_id,
|
|
|
|
'track_type_id' => $track->track_type_id,
|
|
|
|
'license_id' => $track->license_id != null ? $track->license_id : 3,
|
|
|
|
'description' => $track->description,
|
|
|
|
'lyrics' => $track->lyrics,
|
2013-07-27 02:15:07 +02:00
|
|
|
'released_at' => $track->released_at,
|
2013-07-27 05:00:45 +02:00
|
|
|
'cover_url' => $track->hasCover() ? $track->getCoverUrl(Image::NORMAL) : null,
|
2013-07-28 08:07:25 +02:00
|
|
|
'real_cover_url' => $track->getCoverUrl(Image::NORMAL),
|
2013-07-28 19:45:21 +02:00
|
|
|
'show_songs' => $showSongs,
|
|
|
|
'album_id' => $track->album_id
|
2013-07-25 23:33:04 +02:00
|
|
|
], 200);
|
|
|
|
}
|
2013-07-31 13:47:16 +02:00
|
|
|
|
|
|
|
private function mapPublicTrack($track) {
|
|
|
|
return [
|
|
|
|
'id' => $track->id,
|
|
|
|
'title' => $track->title,
|
|
|
|
'user' => [
|
|
|
|
'id' => $track->user->id,
|
|
|
|
'name' => $track->user->display_name,
|
|
|
|
'url' => $track->user->url
|
|
|
|
],
|
|
|
|
'url' => $track->url,
|
|
|
|
'slug' => $track->slug,
|
|
|
|
'is_vocal' => $track->is_vocal,
|
|
|
|
'is_explicit' => $track->is_explicit,
|
|
|
|
'is_downloadable' => $track->is_downloadable,
|
|
|
|
'is_published' => $track->isPublished(),
|
|
|
|
'published_at' => $track->published_at,
|
|
|
|
'duration' => $track->duration,
|
|
|
|
'genre' => $track->genre != null
|
|
|
|
?
|
|
|
|
[
|
|
|
|
'id' => $track->genre->id,
|
|
|
|
'slug' => $track->genre->slug,
|
|
|
|
'name' => $track->genre->name
|
|
|
|
] : null,
|
|
|
|
'track_type_id' => $track->track_type_id,
|
|
|
|
'covers' => [
|
|
|
|
'thumbnail' => $track->getCoverUrl(Image::THUMBNAIL),
|
|
|
|
'small' => $track->getCoverUrl(Image::SMALL),
|
|
|
|
'normal' => $track->getCoverUrl(Image::NORMAL)
|
|
|
|
]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
private function applyFilters($query) {
|
|
|
|
if (Input::has('order')) {
|
|
|
|
$order = \Input::get('order');
|
|
|
|
$parts = explode(',', $order);
|
|
|
|
$query->orderBy($parts[0], $parts[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('is_vocal')) {
|
|
|
|
$isVocal = \Input::get('is_vocal');
|
|
|
|
if ($isVocal == 'true')
|
|
|
|
$query->whereIsVocal(true);
|
|
|
|
else
|
|
|
|
$query->whereIsVocal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('in_album')) {
|
|
|
|
if (Input::get('in_album') == 'true')
|
|
|
|
$query->whereNotNull('album_id');
|
|
|
|
else
|
|
|
|
$query->whereNull('album_id');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Input::has('genres'))
|
|
|
|
$query->whereIn('genre_id', Input::get('genres'));
|
|
|
|
|
|
|
|
if (Input::has('types'))
|
|
|
|
$query->whereIn('track_type_id', Input::get('types'));
|
|
|
|
|
|
|
|
if (Input::has('songs')) {
|
|
|
|
$query->join('show_song_track', 'tracks.id', '=', 'show_song_track.track_id')
|
|
|
|
->whereIn('show_song_track.show_song_id', Input::get('songs'));
|
|
|
|
|
|
|
|
$query->select('tracks.*');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $query;
|
|
|
|
}
|
2013-07-25 23:33:04 +02:00
|
|
|
}
|