get('per_page', 15), 100); $feeds = Feed::with(['language']) ->withCount('articles') ->orderBy('is_active', 'desc') ->orderBy('name') ->paginate($perPage); return $this->sendResponse([ 'feeds' => FeedResource::collection($feeds->items()), 'pagination' => [ 'current_page' => $feeds->currentPage(), 'last_page' => $feeds->lastPage(), 'per_page' => $feeds->perPage(), 'total' => $feeds->total(), 'from' => $feeds->firstItem(), 'to' => $feeds->lastItem(), ] ], 'Feeds retrieved successfully.'); } /** * Store a newly created feed */ public function store(StoreFeedRequest $request): JsonResponse { try { $validated = $request->validated(); $validated['is_active'] = $validated['is_active'] ?? true; // Map provider to URL and set type $providers = [ 'vrt' => new \App\Services\Parsers\VrtHomepageParserAdapter(), 'belga' => new \App\Services\Parsers\BelgaHomepageParserAdapter(), ]; $adapter = $providers[$validated['provider']]; $validated['url'] = $adapter->getHomepageUrl(); $validated['type'] = 'website'; // Remove provider from validated data as it's not a database column unset($validated['provider']); $feed = Feed::create($validated); return $this->sendResponse( new FeedResource($feed), 'Feed created successfully!', 201 ); } catch (ValidationException $e) { return $this->sendValidationError($e->errors()); } catch (\Exception $e) { return $this->sendError('Failed to create feed: ' . $e->getMessage(), [], 500); } } /** * Display the specified feed */ public function show(Feed $feed): JsonResponse { return $this->sendResponse( new FeedResource($feed), 'Feed retrieved successfully.' ); } /** * Update the specified feed */ public function update(UpdateFeedRequest $request, Feed $feed): JsonResponse { try { $validated = $request->validated(); $validated['is_active'] = $validated['is_active'] ?? $feed->is_active; $feed->update($validated); return $this->sendResponse( new FeedResource($feed->fresh()), 'Feed updated successfully!' ); } catch (ValidationException $e) { return $this->sendValidationError($e->errors()); } catch (\Exception $e) { return $this->sendError('Failed to update feed: ' . $e->getMessage(), [], 500); } } /** * Remove the specified feed */ public function destroy(Feed $feed): JsonResponse { try { $feed->delete(); return $this->sendResponse( null, 'Feed deleted successfully!' ); } catch (\Exception $e) { return $this->sendError('Failed to delete feed: ' . $e->getMessage(), [], 500); } } /** * Toggle feed active status */ public function toggle(Feed $feed): JsonResponse { try { $newStatus = !$feed->is_active; $feed->update(['is_active' => $newStatus]); $status = $newStatus ? 'activated' : 'deactivated'; return $this->sendResponse( new FeedResource($feed->fresh()), "Feed {$status} successfully!" ); } catch (\Exception $e) { return $this->sendError('Failed to toggle feed status: ' . $e->getMessage(), [], 500); } } }