Deploy Backend / deploy (push) Successful in 2m35s
- Six new topic pairs (EN+FA): TypeScript advanced patterns, SQL indexes, Git branching/reviews, TDD, clean architecture in Laravel, browser page load - Every existing post gains Key takeaways + FAQ sections in both languages - New engineering-practices category pair and database/git/testing tag pairs - Single-post API now returns same-language previous_post/next_post links
193 lines
6.1 KiB
PHP
193 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Blog;
|
|
|
|
use App\Http\Controllers\Concerns\ValidatesListQuery;
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Blog\StorePostRequest;
|
|
use App\Http\Requests\Blog\UpdatePostRequest;
|
|
use App\Http\Resources\PostResource;
|
|
use App\Models\Blog\Post;
|
|
use Illuminate\Http\Request;
|
|
|
|
/**
|
|
* @OA\Tag(name="Posts", description="Blog post management")
|
|
*/
|
|
class PostController extends Controller
|
|
{
|
|
use ValidatesListQuery;
|
|
|
|
/**
|
|
* @OA\Get(
|
|
* path="/blog/posts",
|
|
* tags={"Posts"},
|
|
* summary="Paginated list of posts",
|
|
*
|
|
* @OA\Parameter(name="per_page", in="query", @OA\Schema(type="integer")),
|
|
* @OA\Parameter(name="category", in="query", @OA\Schema(type="string")),
|
|
* @OA\Parameter(name="tag", in="query", @OA\Schema(type="string")),
|
|
* @OA\Parameter(name="search", in="query", @OA\Schema(type="string")),
|
|
* @OA\Parameter(name="sort_field", in="query", @OA\Schema(type="string")),
|
|
* @OA\Parameter(name="sort_direction", in="query", @OA\Schema(type="string", enum={"asc","desc"})),
|
|
*
|
|
* @OA\Response(response=200, description="OK")
|
|
* )
|
|
*/
|
|
public function index(Request $request)
|
|
{
|
|
$filters = $request->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);
|
|
}
|
|
}
|