<?php

class FoosController extends BaseController {

    /**
     * Foo Repository
     *
     * @var Foo
     */
    protected $foo;

    public function __construct(Foo $foo)
    {
        $this->foo = $foo;
    }

    /**
     * Display a listing of the resource.
     *
     * @return Response
     */
    public function index()
    {
        $foos = $this->foo->all();

        return View::make('foos.index', compact('foos'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
    public function create()
    {
        return View::make('foos.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function store()
    {
        $input = Input::all();
        $validation = Validator::make($input, Foo::$rules);

        if ($validation->passes())
        {
            $this->foo->create($input);

            return Redirect::route('foos.index');
        }

        return Redirect::route('foos.create')
            ->withInput()
            ->withErrors($validation)
            ->with('flash', 'There were validation errors.');
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function show($id)
    {
        $foo = $this->foo->findOrFail($id);

        return View::make('foos.show', compact('foo'));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function edit($id)
    {
        $foo = $this->foo->find($id);

        if (is_null($foo))
        {
            return Redirect::route('foos.index');
        }

        return View::make('foos.edit', compact('foo'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function update($id)
    {
        $input = array_except(Input::all(), '_method');
        $validation = Validator::make($input, Foo::$rules);

        if ($validation->passes())
        {
            $foo = $this->foo->find($id);
            $foo->update($input);

            return Redirect::route('foos.show', $id);
        }

        return Redirect::route('foos.edit', $id)
            ->withInput()
            ->withErrors($validation)
            ->with('flash', 'There were validation errors.');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function destroy($id)
    {
        $this->foo->find($id)->delete();

        return Redirect::route('foos.index');
    }

}