Files
baodev0 520f21cb11 feat: add GeneratesUniqueSlug class for unique slug generation
feat: create PostCategoryFactory for generating post category data

feat: create PostFactory for generating post data

feat: add retry_requested_at column to services table

feat: add vcenter provisioning fields to products table

feat: add module_host_id to os table

feat: create email_notification_templates table for email templates

feat: add scenario and format fields to template table

feat: add admin fields to transactions table

fix: update ticket table columns and types

feat: add uuid to ticket table and backfill existing records

fix: make ticket_reply user_id nullable

feat: create notifications table for user notifications

fix: make ticket uuid nullable

feat: create post_categories table for blog categories

feat: create posts table for blog posts

feat: add invoice email template for PDF generation
2026-05-25 15:39:42 +07:00

101 lines
3.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api\Client\Docs;
use App\Http\Controllers\Controller;
use App\Http\Resources\Api\PostResource;
use App\Models\Post;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
class PostController extends Controller
{
public function index(Request $request): JsonResponse
{
$perPage = min((int) $request->get('per_page', 12), 50);
$query = Post::query()
->published()
->with('category')
->whereHas('category', fn ($q) => $q->where('status', 'active'))
->when(
$request->filled('keyword'),
fn ($q) => $q->where(function ($subQuery) use ($request) {
$keyword = $request->string('keyword')->toString();
$subQuery
->where('title', 'like', "%{$keyword}%")
->orWhere('excerpt', 'like', "%{$keyword}%")
->orWhere('content', 'like', "%{$keyword}%");
})
)
->when(
$request->filled('category_slug'),
fn ($q) => $q->whereHas('category', fn ($subQuery) => $subQuery->where('slug', $request->string('category_slug')->toString()))
)
->orderByDesc('is_featured')
->orderByDesc('published_at')
->orderByDesc('id');
$paginator = $query->paginate($perPage);
return response()->json([
'error' => false,
'data' => [
'items' => PostResource::collection($paginator->items()),
'total' => $paginator->total(),
'per_page' => $paginator->perPage(),
'current_page' => $paginator->currentPage(),
'last_page' => $paginator->lastPage(),
],
]);
}
public function featured(): JsonResponse
{
$posts = Post::query()
->published()
->with('category')
->where('is_featured', true)
->whereHas('category', fn ($q) => $q->where('status', 'active'))
->orderByDesc('published_at')
->limit(6)
->get();
return response()->json([
'error' => false,
'data' => PostResource::collection($posts),
]);
}
public function show(string $slug): JsonResponse
{
$post = Post::query()
->published()
->with('category')
->where('slug', $slug)
->whereHas('category', fn ($q) => $q->where('status', 'active'))
->first();
if (!$post) {
return response()->json(['error' => true, 'message' => 'Không tìm thấy bài viết.'], 404);
}
$post->increment('view_count');
$post->refresh();
$post->setRelation('related_posts', Post::query()
->published()
->with('category')
->where('post_category_id', $post->post_category_id)
->where('id', '!=', $post->id)
->orderByDesc('published_at')
->limit(4)
->get());
return response()->json([
'error' => false,
'data' => new PostResource($post),
]);
}
}