only(['category', 'tag', 'search', 'author', 'published']); $params = $this->listQuery( $request, ['published_at', 'views', 'likes', 'title'], 'sort_field', 'published_at', 'desc' ); $query = Post::with(['category', 'tags', 'author']) ->filter($filters) ->sort($params['sort'], $params['direction']); $posts = $query->paginate($params['per_page']); return PostResource::collection($posts); } /** * @OA\Get( * path="/blog/posts/{slug}", * tags={"Posts"}, * summary="Get a single post by slug with related posts", * * @OA\Parameter(name="slug", in="path", required=true, @OA\Schema(type="string")), * * @OA\Response(response=200, description="OK") * ) */ public function show($slug) { $post = Post::with(['category', 'tags', 'author', 'relatedPosts.category', 'relatedPosts.tags', 'relatedPosts.author']) ->where('slug', $slug) ->firstOrFail(); return response()->json([ 'post' => new PostResource($post), 'related_posts' => PostResource::collection($post->relatedPosts->sortByDesc('published_at')->take(5)), ], 200); } /** * @OA\Post( * path="/blog/posts", * tags={"Posts"}, * summary="Create a post", * * @OA\RequestBody(@OA\MediaType(mediaType="application/json")), * * @OA\Response(response=201, description="Created") * ) */ public function store(StorePostRequest $request) { $post = Post::create($request->validated()); if ($request->has('tags')) { $post->tags()->attach($request->tags); } return new PostResource($post->load(['category', 'tags', 'author'])); } /** * @OA\Put( * path="/blog/posts/{id}", * tags={"Posts"}, * summary="Update a post", * * @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")), * * @OA\RequestBody(@OA\MediaType(mediaType="application/json")), * * @OA\Response(response=200, description="OK") * ) */ public function update(UpdatePostRequest $request, $id) { $post = Post::findOrFail($id); $post->update($request->validated()); if ($request->has('tags')) { $post->tags()->sync($request->tags); } return new PostResource($post->load(['category', 'tags', 'author'])); } /** * @OA\Delete( * path="/blog/posts/{id}", * tags={"Posts"}, * summary="Delete a post", * * @OA\Parameter(name="id", in="path", required=true, @OA\Schema(type="integer")), * * @OA\Response(response=200, description="OK") * ) */ public function destroy($id) { $post = Post::findOrFail($id); $post->tags()->detach(); $post->relatedPosts()->detach(); $post->delete(); return response()->json(['message' => 'Post deleted successfully'], 200); } }