. */ namespace Poniverse\Ponyfm\Http\Controllers\Api\Web; use Illuminate\Database\Eloquent\ModelNotFoundException; use Poniverse\Ponyfm\Http\Controllers\ApiControllerBase; use Poniverse\Ponyfm\Models\Track; use Auth; use DB; use Response; class StatsController extends ApiControllerBase { public function getTrackStatsHourly($id) { // Get track to check if it exists // and if we are allowed to view it. // In the future we could do something // with this data, not sure. try { $track = Track::findOrFail($id); } catch (ModelNotFoundException $e) { return $this->notFound('Track not found!'); } // Do we have permission to view this track? if (!$track->canView(Auth::user())) return $this->notFound('Track not found!'); // I know, raw SQL ugh. I'm not used to laravel's query system $query = DB::select(DB::raw(" SELECT HOUR(created_at) AS `hour`, COUNT(1) AS `plays` FROM `resource_log_items` WHERE `track_id` = :id AND `log_type` = 3 AND `created_at` > now() - INTERVAL 7 DAY GROUP BY HOUR(created_at);"), array( 'id' => $id, )); return Response::json(['stats' => $query], 200); } }