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

180 lines
4.5 KiB
PHP
Raw Normal View History

2013-08-01 04:01:41 +02:00
<?php
namespace Api\Web;
use Commands\CreateAlbumCommand;
use Commands\DeleteAlbumCommand;
use Commands\DeleteTrackCommand;
use Commands\EditAlbumCommand;
use Commands\EditTrackCommand;
use Cover;
use Entities\Album;
2013-08-01 10:57:08 +02:00
use Entities\Comment;
use Entities\Favourite;
2013-08-01 04:01:41 +02:00
use Entities\Image;
use Entities\Track;
use Entities\User;
2013-09-01 23:54:33 +02:00
use Illuminate\Support\Facades\App;
2013-08-01 04:01:41 +02:00
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Response;
class ArtistsController extends \ApiControllerBase {
2013-08-01 10:57:08 +02:00
public function getFavourites($slug) {
$user = User::whereSlug($slug)->first();
if (!$user)
App::abort(404);
$favs = Favourite::whereUserId($user->id)->with([
2013-08-21 04:40:11 +02:00
'track.genre',
'track.cover',
'track.user',
'album.cover',
'album.user',
'track' => function($query) { $query->userDetails(); },
'album' => function($query) { $query->userDetails(); }])->get();
2013-08-01 10:57:08 +02:00
$tracks = [];
$albums = [];
foreach ($favs as $fav) {
if ($fav->type == 'Entities\Track') {
$tracks[] = Track::mapPublicTrackSummary($fav->track);
}
else if ($fav->type == 'Entities\Album') {
$albums[] = Album::mapPublicAlbumSummary($fav->album);
}
}
return Response::json([
'tracks' => $tracks,
'albums' => $albums
], 200);
}
2013-08-01 04:47:21 +02:00
public function getContent($slug) {
$user = User::whereSlug($slug)->first();
if (!$user)
App::abort(404);
2013-09-24 08:27:13 +02:00
$query = Track::summary()->published()->listed()->explicitFilter()->with('genre', 'cover', 'user')->userDetails()->whereUserId($user->id)->whereNotNull('published_at');
2013-08-01 04:47:21 +02:00
$tracks = [];
$singles = [];
foreach ($query->get() as $track) {
if ($track->album_id != null)
$tracks[] = Track::mapPublicTrackSummary($track);
else
$singles[] = Track::mapPublicTrackSummary($track);
}
$query = Album::summary()
->with('user')
2013-08-01 04:47:21 +02:00
->orderBy('created_at', 'desc')
->where('track_count', '>', 0)
2013-08-01 04:47:21 +02:00
->whereUserId($user->id);
$albums = [];
foreach ($query->get() as $album) {
$albums[] = Album::mapPublicAlbumSummary($album);
}
return Response::json(['singles' => $singles, 'albumTracks' => $tracks, 'albums' => $albums], 200);
}
2013-08-01 04:01:41 +02:00
public function getShow($slug) {
2015-05-21 04:45:46 +02:00
$user = User::whereSlug($slug)
->userDetails()
->with(['comments' => function ($query) { $query->with('user'); }])
->first();
2013-08-01 04:01:41 +02:00
if (!$user)
App::abort(404);
2015-05-21 04:45:46 +02:00
$trackQuery = Track::summary()
->published()
->explicitFilter()
->listed()
->with('genre', 'cover', 'user')
->userDetails()
->whereUserId($user->id)
->whereNotNull('published_at')
->orderBy('created_at', 'desc')
->take(20);
2013-08-01 04:47:21 +02:00
2015-05-21 04:45:46 +02:00
$latestTracks = [];
2013-08-01 04:47:21 +02:00
foreach ($trackQuery->get() as $track) {
$latestTracks[] = Track::mapPublicTrackSummary($track);
}
2013-08-01 10:57:08 +02:00
$comments = [];
foreach ($user->comments as $comment) {
$comments[] = Comment::mapPublic($comment);
}
$userData = [
'is_following' => false
];
if ($user->users->count()) {
$userRow = $user->users[0];
$userData = [
'is_following' => $userRow->is_followed
];
}
2013-08-01 04:01:41 +02:00
return Response::json([
'artist' => [
'id' => $user->id,
'name' => $user->display_name,
'slug' => $user->slug,
2015-05-21 04:45:46 +02:00
'is_archived' => $user->is_archived,
2013-08-01 04:01:41 +02:00
'avatars' => [
'small' => $user->getAvatarUrl(Image::SMALL),
'normal' => $user->getAvatarUrl(Image::NORMAL)
],
2013-08-01 04:47:21 +02:00
'created_at' => $user->created_at,
'followers' => [],
'following' => [],
'latest_tracks' => $latestTracks,
'comments' => $comments,
2013-08-01 04:47:21 +02:00
'bio' => $user->bio,
'mlpforums_username' => $user->mlpforums_name,
'message_url' => $user->message_url,
'user_data' => $userData
2013-08-01 04:01:41 +02:00
]
], 200);
}
public function getIndex() {
$page = 1;
if (Input::has('page'))
$page = Input::get('page');
$query = User::orderBy('created_at', 'desc')
->where('track_count', '>', 0);
2013-08-01 04:01:41 +02:00
$count = $query->count();
$perPage = 40;
2013-08-01 04:01:41 +02:00
$query->skip(($page - 1) * $perPage)->take($perPage);
$users = [];
foreach ($query->get() as $user) {
$users[] = [
'id' => $user->id,
'name' => $user->display_name,
'slug' => $user->slug,
'url' => $user->url,
2015-05-21 04:45:46 +02:00
'is_archived' => $user->is_archived,
2013-08-01 04:01:41 +02:00
'avatars' => [
'small' => $user->getAvatarUrl(Image::SMALL),
'normal' => $user->getAvatarUrl(Image::NORMAL)
],
'created_at' => $user->created_at
];
}
return Response::json(["artists" => $users, "current_page" => $page, "total_pages" => ceil($count / $perPage)], 200);
}
}