public function getQuizzes(Request $request)
 {
     $from = (int) $request->input('from', null);
     $limit = (int) $request->input('limit', 10);
     $fromPrev = $from - $limit > 0 ? $from - $limit : null;
     $quizzes = Quiz::take($limit)->orderBy('date_created')->with('user')->with('media');
     if ($from > 0) {
         $quizzes->skip($from);
     }
     $quizzes = $quizzes->get();
     $api = new ApiHandler();
     $api->setCollection($quizzes)->isCollection()->enableSideloading();
     $api->addLink('self', route('get_quizzes', ['from' => $fromPrev, 'limit' => $limit]));
     if ($quizzes->count() === $limit) {
         $api->addLink('next', route('get_quizzes', ['from' => $quizzes->last()->getKey(), 'limit' => $limit]));
     }
     if ($from) {
         $api->addLink('prev', route('get_quizzes', ['from' => $fromPrev, 'limit' => $limit]));
     }
     return $api->send();
 }
Example #2
0
use App\Library\Api\Handler as ApiHandler;
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Credentials: true');
Route::get('/', function () {
    $api = new ApiHandler('V1');
    $api->addLink('quizzes', route('get_quizzes'));
    return $api->send();
});
//************************************************************* Quizzes
Route::get('/quizzes', ['as' => 'get_quizzes', 'uses' => 'QuizzesController@getQuizzes']);
# get home
Route::get('/quizzes/{id}', ['as' => 'get_quiz', 'uses' => 'QuizzesController@getQuiz']);
# get one
Route::post('/quizzes/{id}', ['as' => 'post_quiz_response', 'uses' => 'QuizzesController@postQuiz']);
# send response
Route::get('/quizzes/{id}/likes', ['as' => 'get_quiz_likes', 'uses' => 'LikesController@getQuizLikes']);
# get comments
// Route::get('/quizzes/{id}/likes/'); # get all likes for a quizz
// Route::post('/quizzes/{id}/likes/'); # like a quizz
// Route::delete('/quizzes/{id}/likes/'); # unlike a quizz
Route::get('/quizzes/{id}/comments', ['as' => 'get_quiz_comments', 'uses' => 'CommentsController@getQuizComments']);