200 lines
No EOL
6.6 KiB
PHP
200 lines
No EOL
6.6 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use Domains\Feed\Resources\RouteResource;
|
|
use Domains\Feed\Models\Feed;
|
|
use Domains\Platform\Models\PlatformChannel;
|
|
use Domains\Feed\Models\Route;
|
|
use Domains\Feed\Requests\StoreRouteRequest;
|
|
use Domains\Feed\Requests\UpdateRouteRequest;
|
|
use Domains\Settings\Services\LanguageService;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class RoutingController extends BaseController
|
|
{
|
|
public function __construct(
|
|
private LanguageService $languageService
|
|
) {}
|
|
/**
|
|
* Display a listing of routing configurations
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
$routes = Route::withAllRelationships()
|
|
->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(StoreRouteRequest $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validated();
|
|
|
|
$validated['is_active'] = $validated['is_active'] ?? true;
|
|
$validated['priority'] = $validated['priority'] ?? 0;
|
|
|
|
$route = Route::create($validated);
|
|
|
|
// Load relationships efficiently
|
|
$route->load([
|
|
'feed:id,name,url,type,provider,is_active',
|
|
'platformChannel:id,platform_instance_id,name,display_name,description,language_id,is_active',
|
|
'platformChannel.platformInstance:id,name,url',
|
|
'language:id,short_code,name,native_name,is_active',
|
|
'keywords:id,feed_id,platform_channel_id,keyword,is_active'
|
|
]);
|
|
|
|
return $this->sendResponse(
|
|
new RouteResource($route),
|
|
'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 = $this->findRoute($feed, $channel);
|
|
|
|
if (!$route) {
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
}
|
|
|
|
$route->load([
|
|
'feed:id,name,url,type,provider,is_active',
|
|
'platformChannel:id,platform_instance_id,name,display_name,description,language_id,is_active',
|
|
'platformChannel.platformInstance:id,name,url',
|
|
'language:id,short_code,name,native_name,is_active',
|
|
'keywords:id,feed_id,platform_channel_id,keyword,is_active'
|
|
]);
|
|
|
|
return $this->sendResponse(
|
|
new RouteResource($route),
|
|
'Routing configuration retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update the specified routing configuration
|
|
*/
|
|
public function update(UpdateRouteRequest $request, Feed $feed, PlatformChannel $channel): JsonResponse
|
|
{
|
|
try {
|
|
$route = $this->findRoute($feed, $channel);
|
|
|
|
if (!$route) {
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
}
|
|
|
|
$validated = $request->validated();
|
|
|
|
Route::where('feed_id', $feed->id)
|
|
->where('platform_channel_id', $channel->id)
|
|
->update($validated);
|
|
|
|
return $this->sendResponse(
|
|
new RouteResource($route->fresh(['feed', 'platformChannel', 'language', 'keywords'])),
|
|
'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 = $this->findRoute($feed, $channel);
|
|
|
|
if (!$route) {
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
}
|
|
|
|
Route::where('feed_id', $feed->id)
|
|
->where('platform_channel_id', $channel->id)
|
|
->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 = $this->findRoute($feed, $channel);
|
|
|
|
if (!$route) {
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
}
|
|
|
|
$newStatus = !$route->is_active;
|
|
Route::where('feed_id', $feed->id)
|
|
->where('platform_channel_id', $channel->id)
|
|
->update(['is_active' => $newStatus]);
|
|
|
|
$status = $newStatus ? 'activated' : 'deactivated';
|
|
|
|
return $this->sendResponse(
|
|
new RouteResource($route->fresh(['feed', 'platformChannel', 'language', 'keywords'])),
|
|
"Routing configuration {$status} successfully!"
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to toggle routing configuration status: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get common languages for feed and channel
|
|
*/
|
|
public function commonLanguages(Feed $feed, PlatformChannel $channel): JsonResponse
|
|
{
|
|
$commonLanguages = $this->languageService->getCommonLanguages($feed->id, $channel->id);
|
|
|
|
return $this->sendResponse(
|
|
$commonLanguages,
|
|
'Common languages for feed and channel retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Find a route by feed and channel
|
|
*/
|
|
private function findRoute(Feed $feed, PlatformChannel $channel): ?Route
|
|
{
|
|
return Route::where('feed_id', $feed->id)
|
|
->where('platform_channel_id', $channel->id)
|
|
->first();
|
|
}
|
|
} |