Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 80 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
| RoutingController | |
0.00% |
0 / 80 |
|
0.00% |
0 / 6 |
306 | |
0.00% |
0 / 1 |
| index | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| store | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
12 | |||
| show | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
6 | |||
| update | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| destroy | |
0.00% |
0 / 12 |
|
0.00% |
0 / 1 |
12 | |||
| toggle | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Http\Controllers\Api\V1; |
| 4 | |
| 5 | use App\Http\Resources\RouteResource; |
| 6 | use App\Models\Feed; |
| 7 | use App\Models\PlatformChannel; |
| 8 | use App\Models\Route; |
| 9 | use Illuminate\Http\JsonResponse; |
| 10 | use Illuminate\Http\Request; |
| 11 | use Illuminate\Validation\ValidationException; |
| 12 | |
| 13 | class RoutingController extends BaseController |
| 14 | { |
| 15 | /** |
| 16 | * Display a listing of routing configurations |
| 17 | */ |
| 18 | public function index(): JsonResponse |
| 19 | { |
| 20 | $routes = Route::with(['feed', 'platformChannel']) |
| 21 | ->orderBy('is_active', 'desc') |
| 22 | ->orderBy('priority', 'asc') |
| 23 | ->get(); |
| 24 | |
| 25 | return $this->sendResponse( |
| 26 | RouteResource::collection($routes), |
| 27 | 'Routing configurations retrieved successfully.' |
| 28 | ); |
| 29 | } |
| 30 | |
| 31 | /** |
| 32 | * Store a newly created routing configuration |
| 33 | */ |
| 34 | public function store(Request $request): JsonResponse |
| 35 | { |
| 36 | try { |
| 37 | $validated = $request->validate([ |
| 38 | 'feed_id' => 'required|exists:feeds,id', |
| 39 | 'platform_channel_id' => 'required|exists:platform_channels,id', |
| 40 | 'is_active' => 'boolean', |
| 41 | 'priority' => 'nullable|integer|min:0', |
| 42 | ]); |
| 43 | |
| 44 | $validated['is_active'] = $validated['is_active'] ?? true; |
| 45 | $validated['priority'] = $validated['priority'] ?? 0; |
| 46 | |
| 47 | $route = Route::create($validated); |
| 48 | |
| 49 | return $this->sendResponse( |
| 50 | new RouteResource($route->load(['feed', 'platformChannel'])), |
| 51 | 'Routing configuration created successfully!', |
| 52 | 201 |
| 53 | ); |
| 54 | } catch (ValidationException $e) { |
| 55 | return $this->sendValidationError($e->errors()); |
| 56 | } catch (\Exception $e) { |
| 57 | return $this->sendError('Failed to create routing configuration: ' . $e->getMessage(), [], 500); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Display the specified routing configuration |
| 63 | */ |
| 64 | public function show(Feed $feed, PlatformChannel $channel): JsonResponse |
| 65 | { |
| 66 | $route = Route::where('feed_id', $feed->id) |
| 67 | ->where('platform_channel_id', $channel->id) |
| 68 | ->with(['feed', 'platformChannel']) |
| 69 | ->first(); |
| 70 | |
| 71 | if (!$route) { |
| 72 | return $this->sendNotFound('Routing configuration not found.'); |
| 73 | } |
| 74 | |
| 75 | return $this->sendResponse( |
| 76 | new RouteResource($route), |
| 77 | 'Routing configuration retrieved successfully.' |
| 78 | ); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Update the specified routing configuration |
| 83 | */ |
| 84 | public function update(Request $request, Feed $feed, PlatformChannel $channel): JsonResponse |
| 85 | { |
| 86 | try { |
| 87 | $route = Route::where('feed_id', $feed->id) |
| 88 | ->where('platform_channel_id', $channel->id) |
| 89 | ->first(); |
| 90 | |
| 91 | if (!$route) { |
| 92 | return $this->sendNotFound('Routing configuration not found.'); |
| 93 | } |
| 94 | |
| 95 | $validated = $request->validate([ |
| 96 | 'is_active' => 'boolean', |
| 97 | 'priority' => 'nullable|integer|min:0', |
| 98 | ]); |
| 99 | |
| 100 | $route->update($validated); |
| 101 | |
| 102 | return $this->sendResponse( |
| 103 | new RouteResource($route->fresh(['feed', 'platformChannel'])), |
| 104 | 'Routing configuration updated successfully!' |
| 105 | ); |
| 106 | } catch (ValidationException $e) { |
| 107 | return $this->sendValidationError($e->errors()); |
| 108 | } catch (\Exception $e) { |
| 109 | return $this->sendError('Failed to update routing configuration: ' . $e->getMessage(), [], 500); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Remove the specified routing configuration |
| 115 | */ |
| 116 | public function destroy(Feed $feed, PlatformChannel $channel): JsonResponse |
| 117 | { |
| 118 | try { |
| 119 | $route = Route::where('feed_id', $feed->id) |
| 120 | ->where('platform_channel_id', $channel->id) |
| 121 | ->first(); |
| 122 | |
| 123 | if (!$route) { |
| 124 | return $this->sendNotFound('Routing configuration not found.'); |
| 125 | } |
| 126 | |
| 127 | $route->delete(); |
| 128 | |
| 129 | return $this->sendResponse( |
| 130 | null, |
| 131 | 'Routing configuration deleted successfully!' |
| 132 | ); |
| 133 | } catch (\Exception $e) { |
| 134 | return $this->sendError('Failed to delete routing configuration: ' . $e->getMessage(), [], 500); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * Toggle routing configuration active status |
| 140 | */ |
| 141 | public function toggle(Feed $feed, PlatformChannel $channel): JsonResponse |
| 142 | { |
| 143 | try { |
| 144 | $route = Route::where('feed_id', $feed->id) |
| 145 | ->where('platform_channel_id', $channel->id) |
| 146 | ->first(); |
| 147 | |
| 148 | if (!$route) { |
| 149 | return $this->sendNotFound('Routing configuration not found.'); |
| 150 | } |
| 151 | |
| 152 | $newStatus = !$route->is_active; |
| 153 | $route->update(['is_active' => $newStatus]); |
| 154 | |
| 155 | $status = $newStatus ? 'activated' : 'deactivated'; |
| 156 | |
| 157 | return $this->sendResponse( |
| 158 | new RouteResource($route->fresh(['feed', 'platformChannel'])), |
| 159 | "Routing configuration {$status} successfully!" |
| 160 | ); |
| 161 | } catch (\Exception $e) { |
| 162 | return $this->sendError('Failed to toggle routing configuration status: ' . $e->getMessage(), [], 500); |
| 163 | } |
| 164 | } |
| 165 | } |