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

78 lines
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;
2013-07-28 09:09:10 +02:00
use Commands\DeleteTrackCommand;
2013-07-28 19:45:21 +02:00
use Commands\EditAlbumCommand;
2013-07-28 09:09:10 +02:00
use Commands\EditTrackCommand;
use Cover;
use Entities\Album;
use Entities\Image;
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-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);
}
}