Pony.fm/app/controllers/Api/Web/TracksController.php

133 lines
3.4 KiB
PHP
Raw Normal View History

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;
2013-08-01 10:57:08 +02:00
use Entities\Favourite;
2013-07-27 02:15:07 +02:00
use Entities\Image;
2013-08-19 05:39:29 +02:00
use Entities\ResourceLogItem;
use Entities\ResourceUser;
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;
2013-08-21 04:40:11 +02:00
use Illuminate\Support\Facades\Log;
2013-07-26 12:29:58 +02:00
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()));
}
public function getShow($id) {
$track = Track::userDetails()->withComments()->find($id);
if (!$track || !$track->canView(Auth::user()))
return $this->notFound('Track not found!');
2013-08-19 05:39:29 +02:00
if (Input::get('log')) {
ResourceLogItem::logItem('track', $id, ResourceLogItem::VIEW);
$track->view_count++;
}
return Response::json(['track' => Track::mapPublicTrackShow($track)], 200);
}
2013-07-31 13:47:16 +02:00
public function getIndex() {
$page = 1;
2013-08-19 05:39:29 +02:00
$perPage = 45;
2013-07-31 13:47:16 +02:00
if (Input::has('page'))
$page = Input::get('page');
2013-08-01 10:57:08 +02:00
$query = Track::summary()
->userDetails()
->explicitFilter()
->published()
2013-08-21 04:40:11 +02:00
->with('user', 'genre', 'cover', 'album', 'album.user');
2013-08-01 10:57:08 +02:00
2013-07-31 13:47:16 +02:00
$this->applyFilters($query);
$totalCount = $query->count();
2013-08-19 05:39:29 +02:00
$query->take($perPage)->skip($perPage * ($page - 1));
2013-07-31 13:47:16 +02:00
$tracks = [];
2013-08-01 10:57:08 +02:00
$ids = [];
foreach ($query->get(['tracks.*']) as $track) {
$tracks[] = Track::mapPublicTrackSummary($track);
2013-08-01 10:57:08 +02:00
$ids[] = $track->id;
}
2013-07-31 13:47:16 +02:00
2013-08-16 01:49:20 +02:00
return Response::json(["tracks" => $tracks, "current_page" => $page, "total_pages" => ceil($totalCount / $perPage)], 200);
2013-07-31 13:47:16 +02:00
}
2013-07-25 23:33:04 +02:00
public function getOwned() {
2013-09-01 09:58:27 +02:00
$query = Track::summary()->where('user_id', \Auth::user()->id)->orderBy('created_at', 'desc');
2013-07-25 23:33:04 +02:00
$tracks = [];
foreach ($query->get() as $track)
$tracks[] = Track::mapPrivateTrackSummary($track);
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();
return Response::json(Track::mapPrivateTrackShow($track), 200);
2013-07-25 23:33:04 +02:00
}
2013-07-31 13:47:16 +02:00
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')) {
2013-08-10 00:57:30 +02:00
$query->join('show_song_track', function($join) {
$join->on('tracks.id', '=', 'show_song_track.track_id');
});
$query->whereIn('show_song_track.show_song_id', Input::get('songs'));
2013-07-31 13:47:16 +02:00
}
return $query;
}
2013-07-25 23:33:04 +02:00
}