Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ArticlesController | |
0.00% |
0 / 33 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
2 | |||
| approve | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| reject | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api\V1; |
| 4 | |
| 5 | use App\Http\Resources\ArticleResource; |
| 6 | use App\Models\Article; |
| 7 | use App\Models\Setting; |
| 8 | use Illuminate\Http\JsonResponse; |
| 9 | use Illuminate\Http\Request; |
| 10 | |
| 11 | class ArticlesController extends BaseController |
| 12 | { |
| 13 | /** |
| 14 | * Display a listing of articles |
| 15 | */ |
| 16 | public function index(Request $request): JsonResponse |
| 17 | { |
| 18 | $perPage = min($request->get('per_page', 15), 100); // Max 100 items per page |
| 19 | $articles = Article::with(['feed', 'articlePublication']) |
| 20 | ->orderBy('created_at', 'desc') |
| 21 | ->paginate($perPage); |
| 22 | |
| 23 | $publishingApprovalsEnabled = Setting::isPublishingApprovalsEnabled(); |
| 24 | |
| 25 | return $this->sendResponse([ |
| 26 | 'articles' => ArticleResource::collection($articles->items()), |
| 27 | 'pagination' => [ |
| 28 | 'current_page' => $articles->currentPage(), |
| 29 | 'last_page' => $articles->lastPage(), |
| 30 | 'per_page' => $articles->perPage(), |
| 31 | 'total' => $articles->total(), |
| 32 | 'from' => $articles->firstItem(), |
| 33 | 'to' => $articles->lastItem(), |
| 34 | ], |
| 35 | 'settings' => [ |
| 36 | 'publishing_approvals_enabled' => $publishingApprovalsEnabled, |
| 37 | ], |
| 38 | ]); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Approve an article |
| 43 | */ |
| 44 | public function approve(Article $article): JsonResponse |
| 45 | { |
| 46 | try { |
| 47 | $article->approve('manual'); |
| 48 | |
| 49 | return $this->sendResponse( |
| 50 | new ArticleResource($article->fresh(['feed', 'articlePublication'])), |
| 51 | 'Article approved and queued for publishing.' |
| 52 | ); |
| 53 | } catch (\Exception $e) { |
| 54 | return $this->sendError('Failed to approve article: ' . $e->getMessage(), [], 500); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Reject an article |
| 60 | */ |
| 61 | public function reject(Article $article): JsonResponse |
| 62 | { |
| 63 | try { |
| 64 | $article->reject('manual'); |
| 65 | |
| 66 | return $this->sendResponse( |
| 67 | new ArticleResource($article->fresh(['feed', 'articlePublication'])), |
| 68 | 'Article rejected.' |
| 69 | ); |
| 70 | } catch (\Exception $e) { |
| 71 | return $this->sendError('Failed to reject article: ' . $e->getMessage(), [], 500); |
| 72 | } |
| 73 | } |
| 74 | } |