<?php

namespace Roshan\Roshan\Http\Controllers;

use App\Http\Controllers\PublicApiController as PublicController;
use Roshan\Roshan\Interfaces\RoshanRepositoryInterface;
use Roshan\Roshan\Repositories\Presenter\RoshanItemTransformer;

/**
 * Pubic API controller class.
 */
class RoshanPublicApiController extends PublicController
{
    /**
     * Constructor.
     *
     * @param type \Roshan\Roshan\Interfaces\RoshanRepositoryInterface $roshan
     *
     * @return type
     */
    public function __construct(RoshanRepositoryInterface $roshan)
    {
        $this->repository = $roshan;
        parent::__construct();
    }

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

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

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

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

    }
}