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

116 lines
3.2 KiB
PHP
Raw Normal View History

2013-07-28 09:09:10 +02:00
<?php
namespace Api\Web;
2013-07-28 10:35:31 +02:00
use Commands\CreateAlbumCommand;
2013-07-28 19:45:21 +02:00
use Commands\DeleteAlbumCommand;
use Commands\EditAlbumCommand;
2013-07-28 09:09:10 +02:00
use Entities\Album;
2013-08-01 10:57:08 +02:00
use Entities\Comment;
2013-07-28 09:09:10 +02:00
use Entities\Image;
2013-08-19 05:39:29 +02:00
use Entities\ResourceLogItem;
2013-07-28 09:09:10 +02:00
use Entities\Track;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
class AlbumsController extends \ApiControllerBase {
2013-07-28 19:45:21 +02:00
public function postCreate() {
return $this->execute(new CreateAlbumCommand(Input::all()));
}
public function postEdit($id) {
return $this->execute(new EditAlbumCommand($id, Input::all()));
}
public function postDelete($id) {
return $this->execute(new DeleteAlbumCommand($id));
}
2013-08-01 04:01:41 +02:00
public function getShow($id) {
2013-08-19 05:39:29 +02:00
$album = Album::with(['tracks' => function($query) { $query->details(); }, 'user', 'comments' => function($query) { $query->with('user'); }])->details()->find($id);
2013-08-01 04:01:41 +02:00
if (!$album)
App::abort(404);
2013-08-19 05:39:29 +02:00
if (Input::get('log')) {
ResourceLogItem::logItem('album', $id, ResourceLogItem::VIEW);
$album->view_count++;
}
2013-08-01 04:01:41 +02:00
return Response::json([
2013-08-10 00:57:30 +02:00
'album' => Album::mapPublicAlbumShow($album)
2013-08-01 04:01:41 +02:00
], 200);
}
public function getIndex() {
$page = 1;
if (Input::has('page'))
$page = Input::get('page');
$query = Album::summary()
2013-08-01 10:57:08 +02:00
->with(['tracks' => function($query) { $query->details(); }, 'user'])
->details()
2013-08-01 04:01:41 +02:00
->orderBy('created_at', 'desc')
->whereRaw('(SELECT COUNT(id) FROM tracks WHERE tracks.album_id = albums.id) > 0');
$count = $query->count();
$perPage = 15;
$query->skip(($page - 1) * $perPage)->take($perPage);
$albums = [];
foreach ($query->get() as $album) {
2013-08-01 04:47:21 +02:00
$albums[] = Album::mapPublicAlbumSummary($album);
2013-08-01 04:01:41 +02:00
}
return Response::json(["albums" => $albums, "current_page" => $page, "total_pages" => ceil($count / $perPage)], 200);
}
2013-07-28 09:09:10 +02:00
public function getOwned() {
2013-07-28 19:45:21 +02:00
$query = Album::summary()->where('user_id', \Auth::user()->id)->orderBy('created_at', 'desc')->get();
2013-07-28 10:35:31 +02:00
$albums = [];
foreach ($query as $album) {
$albums[] = [
'id' => $album->id,
'title' => $album->title,
'slug' => $album->slug,
'created_at' => $album->created_at,
2013-07-28 19:45:21 +02:00
'covers' => [
'small' => $album->getCoverUrl(Image::SMALL),
'normal' => $album->getCoverUrl(Image::NORMAL)
]
2013-07-28 10:35:31 +02:00
];
}
return Response::json($albums, 200);
}
2013-07-28 09:09:10 +02:00
public function getEdit($id) {
2013-07-28 19:45:21 +02:00
$album = Album::with('tracks')->find($id);
2013-07-28 10:35:31 +02:00
if (!$album)
return $this->notFound('Album ' . $id . ' not found!');
2013-07-28 09:09:10 +02:00
2013-07-28 10:35:31 +02:00
if ($album->user_id != Auth::user()->id)
2013-07-28 09:09:10 +02:00
return $this->notAuthorized();
2013-07-28 19:45:21 +02:00
$tracks = [];
foreach ($album->tracks as $track) {
$tracks[] = [
'id' => $track->id,
'title' => $track->title
];
}
2013-07-28 09:09:10 +02:00
return Response::json([
2013-07-28 10:35:31 +02:00
'id' => $album->id,
'title' => $album->title,
'user_id' => $album->user_id,
'slug' => $album->slug,
'created_at' => $album->created_at,
'published_at' => $album->published_at,
'description' => $album->description,
'cover_url' => $album->hasCover() ? $album->getCoverUrl(Image::NORMAL) : null,
2013-07-28 19:45:21 +02:00
'real_cover_url' => $album->getCoverUrl(Image::NORMAL),
'tracks' => $tracks
2013-07-28 09:09:10 +02:00
], 200);
}
}