<?php

namespace App\Http\Controllers;

use App\Repositories\CategoriaRepository;
use App\Models\Categoria;
use App\Http\Requests\CategoriaRequest;

class CategoriaController extends Controller
{
    protected $categoriaRepository;

    public function __construct(CategoriaRepository $categoriaRepository) {
        $this->categoriaRepository = $categoriaRepository;
    }

    public function getIndex()
    {
    	$categorias = $this->categoriaRepository->all();
    	return view('categoria.index', compact('categorias'));
    }

    public function getCreate()
    {
    	return view('categoria.create');
    }

    public function postCreate(CategoriaRequest $request)
    {
        $categorias = $this->categoriaRepository->create($request->all());

        if($categorias) {
            return redirect()->route('categoria.index');
        }

        return redirect()->back();
    }

    public function getEdit($id)
    {
        $categoria = $this->categoriaRepository->find($id);

        if(is_null($categoria)) {
            return redirect()->back();
        }

    	return view('categoria.edit', compact('categoria'));
    }

    public function putEdit(CategoriaRequest $request)
    {
        $categoria = $this->categoriaRepository->update($request->all(), $request->id, 'id');

		if($categoria) {
            return redirect()->route('categoria.index');
        }
    }

    public function getDelete($id)
    {
        $categoria = $this->categoriaRepository->delete($id);

        if(!$categoria) {
            return redirect()->back();
        }

        return redirect()->route('categoria.index');
    }
}
