2025-08-02 15:20:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
|
|
|
|
|
|
use App\Http\Resources\ArticleResource;
|
|
|
|
|
use App\Models\Article;
|
|
|
|
|
use App\Models\Setting;
|
2025-08-09 18:34:19 +02:00
|
|
|
use App\Jobs\ArticleDiscoveryJob;
|
2025-08-02 15:20:09 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
|
|
|
|
use Illuminate\Http\Request;
|
2025-08-09 18:34:19 +02:00
|
|
|
use Illuminate\Support\Facades\Artisan;
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
class ArticlesController extends BaseController
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* Display a listing of articles
|
|
|
|
|
*/
|
|
|
|
|
public function index(Request $request): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
$perPage = min($request->get('per_page', 15), 100); // Max 100 items per page
|
|
|
|
|
$articles = Article::with(['feed', 'articlePublication'])
|
|
|
|
|
->orderBy('created_at', 'desc')
|
|
|
|
|
->paginate($perPage);
|
|
|
|
|
|
|
|
|
|
$publishingApprovalsEnabled = Setting::isPublishingApprovalsEnabled();
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse([
|
|
|
|
|
'articles' => ArticleResource::collection($articles->items()),
|
|
|
|
|
'pagination' => [
|
|
|
|
|
'current_page' => $articles->currentPage(),
|
|
|
|
|
'last_page' => $articles->lastPage(),
|
|
|
|
|
'per_page' => $articles->perPage(),
|
|
|
|
|
'total' => $articles->total(),
|
|
|
|
|
'from' => $articles->firstItem(),
|
|
|
|
|
'to' => $articles->lastItem(),
|
|
|
|
|
],
|
|
|
|
|
'settings' => [
|
|
|
|
|
'publishing_approvals_enabled' => $publishingApprovalsEnabled,
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Approve an article
|
|
|
|
|
*/
|
|
|
|
|
public function approve(Article $article): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$article->approve('manual');
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
|
|
|
|
new ArticleResource($article->fresh(['feed', 'articlePublication'])),
|
|
|
|
|
'Article approved and queued for publishing.'
|
|
|
|
|
);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->sendError('Failed to approve article: ' . $e->getMessage(), [], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Reject an article
|
|
|
|
|
*/
|
|
|
|
|
public function reject(Article $article): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$article->reject('manual');
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
|
|
|
|
new ArticleResource($article->fresh(['feed', 'articlePublication'])),
|
|
|
|
|
'Article rejected.'
|
|
|
|
|
);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->sendError('Failed to reject article: ' . $e->getMessage(), [], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-09 18:34:19 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Manually refresh articles from all active feeds
|
|
|
|
|
*/
|
|
|
|
|
public function refresh(): JsonResponse
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
// Dispatch the article discovery job
|
|
|
|
|
ArticleDiscoveryJob::dispatch();
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
|
|
|
|
null,
|
|
|
|
|
'Article refresh started. New articles will appear shortly.'
|
|
|
|
|
);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->sendError('Failed to start article refresh: ' . $e->getMessage(), [], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-02 15:20:09 +02:00
|
|
|
}
|