Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
76.00% |
38 / 50 |
|
33.33% |
2 / 6 |
CRAP | |
0.00% |
0 / 1 |
| FeedsController | |
76.00% |
38 / 50 |
|
33.33% |
2 / 6 |
15.34 | |
0.00% |
0 / 1 |
| index | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
1 | |||
| store | |
66.67% |
8 / 12 |
|
0.00% |
0 / 1 |
3.33 | |||
| show | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| update | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
3.43 | |||
| destroy | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
2.09 | |||
| toggle | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
3.10 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api\V1; |
| 4 | |
| 5 | use App\Http\Requests\StoreFeedRequest; |
| 6 | use App\Http\Requests\UpdateFeedRequest; |
| 7 | use App\Http\Resources\FeedResource; |
| 8 | use App\Models\Feed; |
| 9 | use Illuminate\Http\JsonResponse; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Validation\ValidationException; |
| 12 | |
| 13 | class FeedsController extends BaseController |
| 14 | { |
| 15 | /** |
| 16 | * Display a listing of feeds |
| 17 | */ |
| 18 | public function index(Request $request): JsonResponse |
| 19 | { |
| 20 | $feeds = Feed::orderBy('is_active', 'desc') |
| 21 | ->orderBy('name') |
| 22 | ->get(); |
| 23 | |
| 24 | return $this->sendResponse( |
| 25 | FeedResource::collection($feeds), |
| 26 | 'Feeds retrieved successfully.' |
| 27 | ); |
| 28 | } |
| 29 | |
| 30 | /** |
| 31 | * Store a newly created feed |
| 32 | */ |
| 33 | public function store(StoreFeedRequest $request): JsonResponse |
| 34 | { |
| 35 | try { |
| 36 | $validated = $request->validated(); |
| 37 | $validated['is_active'] = $validated['is_active'] ?? true; |
| 38 | |
| 39 | $feed = Feed::create($validated); |
| 40 | |
| 41 | return $this->sendResponse( |
| 42 | new FeedResource($feed), |
| 43 | 'Feed created successfully!', |
| 44 | 201 |
| 45 | ); |
| 46 | } catch (ValidationException $e) { |
| 47 | return $this->sendValidationError($e->errors()); |
| 48 | } catch (\Exception $e) { |
| 49 | return $this->sendError('Failed to create feed: ' . $e->getMessage(), [], 500); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Display the specified feed |
| 55 | */ |
| 56 | public function show(Feed $feed): JsonResponse |
| 57 | { |
| 58 | return $this->sendResponse( |
| 59 | new FeedResource($feed), |
| 60 | 'Feed retrieved successfully.' |
| 61 | ); |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Update the specified feed |
| 66 | */ |
| 67 | public function update(UpdateFeedRequest $request, Feed $feed): JsonResponse |
| 68 | { |
| 69 | try { |
| 70 | $validated = $request->validated(); |
| 71 | $validated['is_active'] = $validated['is_active'] ?? $feed->is_active; |
| 72 | |
| 73 | $feed->update($validated); |
| 74 | |
| 75 | return $this->sendResponse( |
| 76 | new FeedResource($feed->fresh()), |
| 77 | 'Feed updated successfully!' |
| 78 | ); |
| 79 | } catch (ValidationException $e) { |
| 80 | return $this->sendValidationError($e->errors()); |
| 81 | } catch (\Exception $e) { |
| 82 | return $this->sendError('Failed to update feed: ' . $e->getMessage(), [], 500); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Remove the specified feed |
| 88 | */ |
| 89 | public function destroy(Feed $feed): JsonResponse |
| 90 | { |
| 91 | try { |
| 92 | $feed->delete(); |
| 93 | |
| 94 | return $this->sendResponse( |
| 95 | null, |
| 96 | 'Feed deleted successfully!' |
| 97 | ); |
| 98 | } catch (\Exception $e) { |
| 99 | return $this->sendError('Failed to delete feed: ' . $e->getMessage(), [], 500); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | /** |
| 104 | * Toggle feed active status |
| 105 | */ |
| 106 | public function toggle(Feed $feed): JsonResponse |
| 107 | { |
| 108 | try { |
| 109 | $newStatus = !$feed->is_active; |
| 110 | $feed->update(['is_active' => $newStatus]); |
| 111 | |
| 112 | $status = $newStatus ? 'activated' : 'deactivated'; |
| 113 | |
| 114 | return $this->sendResponse( |
| 115 | new FeedResource($feed->fresh()), |
| 116 | "Feed {$status} successfully!" |
| 117 | ); |
| 118 | } catch (\Exception $e) { |
| 119 | return $this->sendError('Failed to toggle feed status: ' . $e->getMessage(), [], 500); |
| 120 | } |
| 121 | } |
| 122 | } |