<?php

namespace Mahasiswa\Mahasiswa\Http\Controllers\Api;

use App\Http\Controllers\Api\PublicController as BaseController;
use Mahasiswa\Mahasiswa\Interfaces\MahasiswaRepositoryInterface;
use Mahasiswa\Mahasiswa\Repositories\Presenter\MahasiswaItemTransformer;

/**
 * Pubic API controller class.
 */
class MahasiswaPublicController extends BaseController
{
    /**
     * Constructor.
     *
     * @param type \Mahasiswa\Mahasiswa\Interfaces\MahasiswaRepositoryInterface $mahasiswa
     *
     * @return type
     */
    public function __construct(MahasiswaRepositoryInterface $mahasiswa)
    {
        $this->repository = $mahasiswa;
        parent::__construct();
    }

    /**
     * Show mahasiswa's list.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function index()
    {
        $mahasiswas = $this->repository
            ->setPresenter('\\Mahasiswa\\Mahasiswa\\Repositories\\Presenter\\MahasiswaListPresenter')
            ->scopeQuery(function($query){
                return $query->orderBy('id','DESC');
            })->paginate();

        $mahasiswas['code'] = 2000;
        return response()->json($mahasiswas)
                ->setStatusCode(200, 'INDEX_SUCCESS');
    }

    /**
     * Show mahasiswa.
     *
     * @param string $slug
     *
     * @return response
     */
    protected function show($slug)
    {
        $mahasiswa = $this->repository
            ->scopeQuery(function($query) use ($slug) {
            return $query->orderBy('id','DESC')
                         ->where('slug', $slug);
        })->first(['*']);

        if (!is_null($mahasiswa)) {
            $mahasiswa         = $this->itemPresenter($module, new MahasiswaItemTransformer);
            $mahasiswa['code'] = 2001;
            return response()->json($mahasiswa)
                ->setStatusCode(200, 'SHOW_SUCCESS');
        } else {
            return response()->json([])
                ->setStatusCode(400, 'SHOW_ERROR');
        }

    }
}