FaqsController.php 2.93 KB
Newer Older
cygnet's avatar
cygnet committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Resources\FaqsResource;
use App\Models\Faqs\Faq;
use App\Repositories\Backend\Faqs\FaqsRepository;
use Illuminate\Http\Request;
use Validator;

class FaqsController extends APIController
{
    protected $repository;

    /**
     * __construct.
     *
     * @param $repository
     */
    public function __construct(FaqsRepository $repository)
    {
        $this->repository = $repository;
    }

    /**
26
     * Return the faqs.
cygnet's avatar
cygnet committed
27
     *
cygnet's avatar
cygnet committed
28
     * @return \Illuminate\Http\JsonResponse
cygnet's avatar
cygnet committed
29 30 31
     */
    public function index(Request $request)
    {
32 33 34
        $limit = $request->get('paginate') ? $request->get('paginate') : 25;
        $orderBy = $request->get('orderBy') ? $request->get('orderBy') : 'ASC';
        $sortBy = $request->get('sortBy') ? $request->get('sortBy') : 'created_at';
35

cygnet's avatar
cygnet committed
36
        return FaqsResource::collection(
37
            $this->repository->getForDataTable()->orderBy($sortBy, $orderBy)->paginate($limit)
cygnet's avatar
cygnet committed
38 39 40 41 42 43
        );
    }

    /**
     * Return the specified resource.
     *
44
     * @param Faq $faq
cygnet's avatar
cygnet committed
45
     *
cygnet's avatar
cygnet committed
46
     * @return \Illuminate\Http\JsonResponse
cygnet's avatar
cygnet committed
47 48 49 50 51 52 53 54 55 56 57
     */
    public function show(Faq $faq)
    {
        return new FaqsResource($faq);
    }

    /**
     * Creates the Resource for Faq.
     *
     * @param Request $request
     *
cygnet's avatar
cygnet committed
58
     * @return \Illuminate\Http\JsonResponse
cygnet's avatar
cygnet committed
59 60 61
     */
    public function store(Request $request)
    {
62
        $validation = $this->validateFaq($request);
cygnet's avatar
cygnet committed
63 64 65 66 67 68 69 70 71 72
        if ($validation->fails()) {
            return $this->throwValidation($validation->messages()->first());
        }

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

        return new FaqsResource(Faq::orderBy('created_at', 'desc')->first());
    }

    /**
73 74 75 76
     * Update Faq.
     *
     * @param Faq     $faq
     * @param Request $request
cygnet's avatar
cygnet committed
77
     *
cygnet's avatar
cygnet committed
78
     * @return \Illuminate\Http\JsonResponse
cygnet's avatar
cygnet committed
79 80 81
     */
    public function update(Request $request, Faq $faq)
    {
82
        $validation = $this->validateFaq($request);
cygnet's avatar
cygnet committed
83 84 85 86 87 88 89 90 91 92 93 94 95

        if ($validation->fails()) {
            return $this->throwValidation($validation->messages()->first());
        }

        $this->repository->update($faq, $request->all());

        $faq = Faq::findOrfail($faq->id);

        return new FaqsResource($faq);
    }

    /**
96 97 98 99
     * Delete Faq.
     *
     * @param Faq     $faq
     * @param Request $request
cygnet's avatar
cygnet committed
100
     *
cygnet's avatar
cygnet committed
101
     * @return \Illuminate\Http\JsonResponse
cygnet's avatar
cygnet committed
102 103 104 105 106
     */
    public function destroy(Faq $faq, Request $request)
    {
        $this->repository->delete($faq);

107 108 109 110 111
        return $this->respond([
            'message' => trans('alerts.backend.faqs.deleted'),
        ]);
    }

cygnet's avatar
cygnet committed
112
    /**
cygnet's avatar
cygnet committed
113
     * validate Faq.
cygnet's avatar
cygnet committed
114 115 116 117 118
     *
     * @param $request
     *
     * @return \Illuminate\Http\JsonResponse
     */
119 120 121 122
    public function validateFaq(Request $request)
    {
        $validation = Validator::make($request->all(), [
            'question' => 'required|max:191',
123
            'answer'   => 'required',
124 125 126
        ]);

        return $validation;
cygnet's avatar
cygnet committed
127 128
    }
}