andersao/l5-repository

View on GitHub
src/Prettus/Repository/Generators/Stubs/controller/controller.stub

Summary

Maintainability
Test Coverage
<?php

$NAMESPACE$

use Illuminate\Http\Request;

use $APPNAME$Http\Requests;
use Prettus\Validator\Contracts\ValidatorInterface;
use Prettus\Validator\Exceptions\ValidatorException;
use $APPNAME$Http\Requests\$CLASS$CreateRequest;
use $APPNAME$Http\Requests\$CLASS$UpdateRequest;
$REPOSITORY$
$VALIDATOR$

/**
 * Class $CONTROLLER$Controller.
 *
 * @package $NAMESPACE$
 */
class $CONTROLLER$Controller extends Controller
{
    /**
     * @var $CLASS$Repository
     */
    protected $repository;

    /**
     * @var $CLASS$Validator
     */
    protected $validator;

    /**
     * $CONTROLLER$Controller constructor.
     *
     * @param $CLASS$Repository $repository
     * @param $CLASS$Validator $validator
     */
    public function __construct($CLASS$Repository $repository, $CLASS$Validator $validator)
    {
        $this->repository = $repository;
        $this->validator  = $validator;
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $$PLURAL$ = $this->repository->all();

        if (request()->wantsJson()) {

            return response()->json([
                'data' => $$PLURAL$,
            ]);
        }

        return view('$PLURAL$.index', compact('$PLURAL$'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  $CLASS$CreateRequest $request
     *
     * @return \Illuminate\Http\Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function store($CLASS$CreateRequest $request)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_CREATE);

            $$SINGULAR$ = $this->repository->create($request->all());

            $response = [
                'message' => '$CLASS$ created.',
                'data'    => $$SINGULAR$->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {
            if ($request->wantsJson()) {
                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $$SINGULAR$ = $this->repository->find($id);

        if (request()->wantsJson()) {

            return response()->json([
                'data' => $$SINGULAR$,
            ]);
        }

        return view('$PLURAL$.show', compact('$SINGULAR$'));
    }

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

        return view('$PLURAL$.edit', compact('$SINGULAR$'));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  $CLASS$UpdateRequest $request
     * @param  string            $id
     *
     * @return Response
     *
     * @throws \Prettus\Validator\Exceptions\ValidatorException
     */
    public function update($CLASS$UpdateRequest $request, $id)
    {
        try {

            $this->validator->with($request->all())->passesOrFail(ValidatorInterface::RULE_UPDATE);

            $$SINGULAR$ = $this->repository->update($request->all(), $id);

            $response = [
                'message' => '$CLASS$ updated.',
                'data'    => $$SINGULAR$->toArray(),
            ];

            if ($request->wantsJson()) {

                return response()->json($response);
            }

            return redirect()->back()->with('message', $response['message']);
        } catch (ValidatorException $e) {

            if ($request->wantsJson()) {

                return response()->json([
                    'error'   => true,
                    'message' => $e->getMessageBag()
                ]);
            }

            return redirect()->back()->withErrors($e->getMessageBag())->withInput();
        }
    }


    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $deleted = $this->repository->delete($id);

        if (request()->wantsJson()) {

            return response()->json([
                'message' => '$CLASS$ deleted.',
                'deleted' => $deleted,
            ]);
        }

        return redirect()->back()->with('message', '$CLASS$ deleted.');
    }
}