<?php

namespace Cushbu\Artwork\Http\Controllers;

use App\Http\Controllers\PublicApiController as PublicController;
use Cushbu\Artwork\Interfaces\ArtworkRepositoryInterface;
use Cushbu\Artwork\Repositories\Presenter\ArtworkItemTransformer;

/**
 * Pubic API controller class.
 */
class ArtworkPublicApiController extends PublicController
{
    /**
     * Constructor.
     *
     * @param type \Cushbu\Artwork\Interfaces\ArtworkRepositoryInterface $artwork
     *
     * @return type
     */
    public function __construct(ArtworkRepositoryInterface $artwork)
    {
        $this->repository = $artwork;
        parent::__construct();
    }

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

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

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

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

    }
}