<?php

namespace App\Http\Controllers\Resources;

use App\Post;
use App\Http\Controllers\Controller;
use App\Http\Filters\QueryBuilder;
use App\Http\Requests\PostRequest;
use Illuminate\Http\Request;

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

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

    }

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

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

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

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