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
162 lines
5.8 KiB
PHP
162 lines
5.8 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\Admin\Docs;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use App\Http\Requests\Admin\PostStoreRequest;
|
|
use App\Http\Requests\Admin\PostUpdateRequest;
|
|
use App\Http\Resources\Api\PostResource;
|
|
use App\Http\Services\DocsWordpressSyncService;
|
|
use App\Http\Services\NextjsRevalidationService;
|
|
use App\Models\Post;
|
|
use App\Support\GeneratesUniqueSlug;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
|
|
class PostController extends Controller
|
|
{
|
|
public function __construct(
|
|
private readonly NextjsRevalidationService $revalidation,
|
|
private readonly DocsWordpressSyncService $wordpressSync,
|
|
) {}
|
|
|
|
public function index(Request $request): JsonResponse
|
|
{
|
|
$perPage = min((int) $request->get('per_page', 15), 100);
|
|
$sort = $request->string('sort', 'latest')->toString();
|
|
|
|
$query = Post::query()
|
|
->with('category')
|
|
->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_id'), fn ($q) => $q->where('post_category_id', (int) $request->get('category_id')))
|
|
->when($request->filled('status'), fn ($q) => $q->where('status', $request->string('status')->toString()))
|
|
->when($request->filled('is_featured'), fn ($q) => $q->where('is_featured', $request->boolean('is_featured')));
|
|
|
|
match ($sort) {
|
|
'title_asc' => $query->orderBy('title'),
|
|
'title_desc' => $query->orderByDesc('title'),
|
|
'published_at_asc', 'oldest' => $query->orderBy('published_at')->orderBy('id'),
|
|
'most_viewed' => $query->orderByDesc('view_count')->orderByDesc('id'),
|
|
default => $query->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 store(PostStoreRequest $request): JsonResponse
|
|
{
|
|
$data = $request->validated();
|
|
$slugSource = $data['slug'] ?: $data['title'];
|
|
$data['slug'] = GeneratesUniqueSlug::forModel(Post::class, $slugSource);
|
|
$data['is_featured'] = (bool) ($data['is_featured'] ?? false);
|
|
$data['view_count'] = (int) ($data['view_count'] ?? 0);
|
|
|
|
$post = Post::create($data);
|
|
$this->revalidation->revalidateDocs();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Tạo bài viết thành công.',
|
|
'data' => new PostResource($post->load('category')),
|
|
], 201);
|
|
}
|
|
|
|
public function show(int $id): JsonResponse
|
|
{
|
|
$post = Post::with('category')->find($id);
|
|
|
|
if (!$post) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy bài viết.'], 404);
|
|
}
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'data' => new PostResource($post),
|
|
]);
|
|
}
|
|
|
|
public function update(PostUpdateRequest $request, int $id): JsonResponse
|
|
{
|
|
$post = Post::find($id);
|
|
|
|
if (!$post) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy bài viết.'], 404);
|
|
}
|
|
|
|
$data = $request->validated();
|
|
$slugSource = $data['slug'] ?: $data['title'];
|
|
$data['slug'] = GeneratesUniqueSlug::forModel(Post::class, $slugSource, $post->id);
|
|
$data['is_featured'] = (bool) ($data['is_featured'] ?? false);
|
|
$data['view_count'] = (int) ($data['view_count'] ?? $post->view_count);
|
|
|
|
$post->update($data);
|
|
$this->revalidation->revalidateDocs();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Cập nhật bài viết thành công.',
|
|
'data' => new PostResource($post->fresh()->load('category')),
|
|
]);
|
|
}
|
|
|
|
public function destroy(int $id): JsonResponse
|
|
{
|
|
$post = Post::find($id);
|
|
|
|
if (!$post) {
|
|
return response()->json(['error' => true, 'message' => 'Không tìm thấy bài viết.'], 404);
|
|
}
|
|
|
|
$post->delete();
|
|
$this->revalidation->revalidateDocs();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => 'Xoá bài viết thành công.',
|
|
]);
|
|
}
|
|
|
|
public function syncWordpress(Request $request): JsonResponse
|
|
{
|
|
$validated = $request->validate([
|
|
'source_url' => 'nullable|url',
|
|
'per_page' => 'nullable|integer|min:1|max:100',
|
|
'max_pages' => 'nullable|integer|min:1|max:200',
|
|
]);
|
|
|
|
$result = $this->wordpressSync->sync(
|
|
$validated['source_url'] ?? null,
|
|
(int) ($validated['per_page'] ?? 20),
|
|
isset($validated['max_pages']) ? (int) $validated['max_pages'] : null,
|
|
);
|
|
|
|
$this->revalidation->revalidateDocs();
|
|
|
|
return response()->json([
|
|
'error' => false,
|
|
'message' => "Đồng bộ hoàn tất. Tạo mới {$result['created']}, cập nhật {$result['updated']}, bỏ qua {$result['skipped']}.",
|
|
'data' => $result,
|
|
]);
|
|
}
|
|
}
|