Deploy Backend / deploy (push) Successful in 2m51s
- Add FilterableByLanguage scope (posts/categories/tags) keyed off the -fa slug convention; every public blog list endpoint honors ?lang=fa|en, feeds/sitemaps keep all languages when omitted - Homepage (latest/most-viewed/random posts + categories) filters per language; post detail shows same-language related posts only - Reseed content to a strict English/Persian mirror: 6 existing international topics + 6 new Iran-market topics (technical SEO for Persian sites, Iranian dev freelancing, AI & Iranian dev careers, web design for Iranian businesses, React job market in Iran, WordPress-to-Next migration), 12 posts per language, zero posts in only one language - Fix 2 legacy FA slugs to follow the EN-slug + -fa convention so twin redirects and mirror assertions hold - Update seeder test to assert balanced languages, full twin coverage and same-language related posts; add lang-filter coverage
159 lines
4.7 KiB
PHP
159 lines
4.7 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);
|
|
|
|
return response()->json([
|
|
'post' => new PostResource($post),
|
|
'related_posts' => PostResource::collection($relatedPosts),
|
|
], 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);
|
|
}
|
|
}
|