<?php
/**
 * Created by PhpStorm.
 * User: radko
 * Date: 2019-01-10
 * Time: 15:33
 */

namespace Seka\Vzdelavanie\Http\Controllers\Api;

use Exception;
use Seka\Vzdelavanie\Http\Resources\KategoriaResource;
use Seka\Vzdelavanie\Services\KategoriaServiceInterface;

/**
 * Class KategoriaController
 * @package Seka\Vzdelavanie\Http\Controllers\Api
 */
class KategoriaController extends Controller
{
    /**
     * @var \Seka\Vzdelavanie\Services\KategoriaServiceInterface
     */
    private $kategoriaService;

    /**
     * KategoriaController constructor.
     * @param \Seka\Vzdelavanie\Services\KategoriaServiceInterface $kategoriaService
     */
    public function __construct(KategoriaServiceInterface $kategoriaService)
    {
        $this->kategoriaService = $kategoriaService;
    }

    /**
     * @return \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function index()
    {
        try {
            $kategorie = $this->kategoriaService->vratVsetkySUcebnymiMaterialmi();
        } catch (Exception $exc) {
            return response()->json([
                'error' => $exc->getMessage() ?: class_basename($exc),
            ], 500);
        }

        return KategoriaResource::collection($kategorie);
    }
}
