fedi-feed-router/backend/app/Http/Controllers/Api/V1/RoutingController.php

165 lines
No EOL
5.2 KiB
PHP

<?php
namespace App\Http\Controllers\Api\V1;
use App\Http\Resources\RouteResource;
use App\Models\Feed;
use App\Models\PlatformChannel;
use App\Models\Route;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;
class RoutingController extends BaseController
{
/**
* Display a listing of routing configurations
*/
public function index(): JsonResponse
{
$routes = Route::with(['feed', 'platformChannel'])
->orderBy('is_active', 'desc')
->orderBy('priority', 'asc')
->get();
return $this->sendResponse(
RouteResource::collection($routes),
'Routing configurations retrieved successfully.'
);
}
/**
* Store a newly created routing configuration
*/
public function store(Request $request): JsonResponse
{
try {
$validated = $request->validate([
'feed_id' => 'required|exists:feeds,id',
'platform_channel_id' => 'required|exists:platform_channels,id',
'is_active' => 'boolean',
'priority' => 'nullable|integer|min:0',
]);
$validated['is_active'] = $validated['is_active'] ?? true;
$validated['priority'] = $validated['priority'] ?? 0;
$route = Route::create($validated);
return $this->sendResponse(
new RouteResource($route->load(['feed', 'platformChannel'])),
'Routing configuration created successfully!',
201
);
} catch (ValidationException $e) {
return $this->sendValidationError($e->errors());
} catch (\Exception $e) {
return $this->sendError('Failed to create routing configuration: ' . $e->getMessage(), [], 500);
}
}
/**
* Display the specified routing configuration
*/
public function show(Feed $feed, PlatformChannel $channel): JsonResponse
{
$route = Route::where('feed_id', $feed->id)
->where('platform_channel_id', $channel->id)
->with(['feed', 'platformChannel'])
->first();
if (!$route) {
return $this->sendNotFound('Routing configuration not found.');
}
return $this->sendResponse(
new RouteResource($route),
'Routing configuration retrieved successfully.'
);
}
/**
* Update the specified routing configuration
*/
public function update(Request $request, Feed $feed, PlatformChannel $channel): JsonResponse
{
try {
$route = Route::where('feed_id', $feed->id)
->where('platform_channel_id', $channel->id)
->first();
if (!$route) {
return $this->sendNotFound('Routing configuration not found.');
}
$validated = $request->validate([
'is_active' => 'boolean',
'priority' => 'nullable|integer|min:0',
]);
$route->update($validated);
return $this->sendResponse(
new RouteResource($route->fresh(['feed', 'platformChannel'])),
'Routing configuration updated successfully!'
);
} catch (ValidationException $e) {
return $this->sendValidationError($e->errors());
} catch (\Exception $e) {
return $this->sendError('Failed to update routing configuration: ' . $e->getMessage(), [], 500);
}
}
/**
* Remove the specified routing configuration
*/
public function destroy(Feed $feed, PlatformChannel $channel): JsonResponse
{
try {
$route = Route::where('feed_id', $feed->id)
->where('platform_channel_id', $channel->id)
->first();
if (!$route) {
return $this->sendNotFound('Routing configuration not found.');
}
$route->delete();
return $this->sendResponse(
null,
'Routing configuration deleted successfully!'
);
} catch (\Exception $e) {
return $this->sendError('Failed to delete routing configuration: ' . $e->getMessage(), [], 500);
}
}
/**
* Toggle routing configuration active status
*/
public function toggle(Feed $feed, PlatformChannel $channel): JsonResponse
{
try {
$route = Route::where('feed_id', $feed->id)
->where('platform_channel_id', $channel->id)
->first();
if (!$route) {
return $this->sendNotFound('Routing configuration not found.');
}
$newStatus = !$route->is_active;
$route->update(['is_active' => $newStatus]);
$status = $newStatus ? 'activated' : 'deactivated';
return $this->sendResponse(
new RouteResource($route->fresh(['feed', 'platformChannel'])),
"Routing configuration {$status} successfully!"
);
} catch (\Exception $e) {
return $this->sendError('Failed to toggle routing configuration status: ' . $e->getMessage(), [], 500);
}
}
}