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']) ->byLanguage($request->input('lang')) ->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(); $postLang = str_ends_with($post->slug, '-fa') ? 'fa' : 'en'; $relatedPosts = $post->relatedPosts ->filter( fn ($related) => str_ends_with($related->slug, '-fa') === ($postLang === 'fa') ) ->sortByDesc('published_at') ->take(5); // Plainly named page-turn links, always inside the reader's language so // the previous/next navigation never crosses the EN/FA mirror. $siblings = Post::with(['category', 'tags', 'author']) ->byLanguage($postLang) ->where('id', '!=', $post->id); $previousPost = (clone $siblings) ->where( fn ($q) => $q->where('published_at', '>', $post->published_at) ->orWhere( fn ($q2) => $q2 ->where('published_at', '=', $post->published_at) ->where('id', '>', $post->id) ) ) ->orderByDesc('published_at') ->orderByDesc('id') ->first(); $nextPost = (clone $siblings) ->where( fn ($q) => $q->where('published_at', '<', $post->published_at) ->orWhere( fn ($q2) => $q2 ->where('published_at', '=', $post->published_at) ->where('id', '<', $post->id) ) ) ->orderBy('published_at') ->orderBy('id') ->first(); return response()->json([ 'post' => new PostResource($post), 'related_posts' => PostResource::collection($relatedPosts), 'previous_post' => $previousPost ? new PostResource($previousPost) : null, 'next_post' => $nextPost ? new PostResource($nextPost) : null, ], 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); } }