Commit 8c72d8d7 authored by cygnet's avatar cygnet

changes done for the FAQ apis

parent ca885d91
<?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;
}
/**
* Return the users.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request)
{
$limit = $request->get('paginate') ? $request->get('paginate') : 25;
return FaqsResource::collection(
$this->repository->getForDataTable()->paginate($limit)
);
}
/**
* Return the specified resource.
*
* @param User $user
*
* @return \Illuminate\Http\Response
*/
public function show(Faq $faq)
{
return new FaqsResource($faq);
}
/**
* Creates the Resource for Faq.
*
* @param Request $request
*
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$validation = $this->validatingRequest($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
$this->repository->create($request->all());
return new FaqsResource(Faq::orderBy('created_at', 'desc')->first());
}
/**
* @param Faq $faq
* @param UpdateFaqRequest $request
*
* @return mixed
*/
public function update(Request $request, Faq $faq)
{
$validation = $this->validatingRequest($request);
if ($validation->fails()) {
return $this->throwValidation($validation->messages()->first());
}
$this->repository->update($faq, $request->all());
$faq = Faq::findOrfail($faq->id);
return new FaqsResource($faq);
}
public function validatingRequest(Request $request)
{
$validation = Validator::make($request->all(), [
'question' => 'required|max:191',
'answer' => 'required',
]);
return $validation;
}
/**
* @param Faq $faq
* @param DeleteFaqRequest $request
*
* @return mixed
*/
public function destroy(Faq $faq, Request $request)
{
$this->repository->delete($faq);
return ['message'=>'success'];
}
}
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\Resource;
class FaqsResource extends Resource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
*
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'question' => $this->question,
'answer' => $this->answer,
'status' => $this->status,
'created_at' => $this->created_at->toIso8601String(),
];
}
}
...@@ -42,5 +42,8 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio ...@@ -42,5 +42,8 @@ Route::group(['namespace' => 'Api\V1', 'prefix' => 'v1', 'as' => 'v1.'], functio
// Page // Page
Route::resource('pages', 'PagesController'); Route::resource('pages', 'PagesController');
// Faqs
Route::resource('faqs', 'FaqsController');
}); });
}); });
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment