<?php

namespace App\Http\Controllers\Resources;

use App\Page;
use App\Http\Controllers\Controller;
use App\Http\Filters\QueryBuilder;
use App\Http\Requests\PageRequest;
use Illuminate\Http\Request;

class PageController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(PageRequest $request)
    {
        $queryBuilder = new QueryBuilder(new Page, $request);

        return response()->json($queryBuilder->build()->paginate(20),200);

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  App\Http\Requests\PageRequest  $request
     * @return \Illuminate\Http\Response
     */
    public function store(PageRequest $request)
    {
      return response()->json(tap(new Page($request->all())), 201);
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function show(Page $page)
    {
      return response()->json(['status'=>'ok','data'=>$page],200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  App\Http\Requests\PageRequest  $requests
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function update(PagePageRequest $request, Page $page)
    {
        return tap($page)->update($request->all());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Page  $page
     * @return \Illuminate\Http\Response
     */
    public function destroy(Page $page)
    {
        $page->delete();
        return response()->json(['code'=>204,'message'=>__('strings.deleted_successfully')],204);
    }
}
