2025-08-02 15:20:09 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
|
|
2025-08-15 16:39:18 +02:00
|
|
|
use Domains\Feed\Resources\RouteResource;
|
|
|
|
|
use Domains\Feed\Models\Feed;
|
|
|
|
|
use Domains\Platform\Models\PlatformChannel;
|
|
|
|
|
use Domains\Feed\Models\Route;
|
2025-08-16 10:09:38 +02:00
|
|
|
use Domains\Feed\Requests\StoreRouteRequest;
|
|
|
|
|
use Domains\Feed\Requests\UpdateRouteRequest;
|
2025-08-02 15:20:09 +02:00
|
|
|
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
|
|
|
|
|
{
|
2025-08-16 10:09:38 +02:00
|
|
|
$routes = Route::with(['feed', 'platformChannel', 'language', 'keywords'])
|
2025-08-02 15:20:09 +02:00
|
|
|
->orderBy('is_active', 'desc')
|
|
|
|
|
->orderBy('priority', 'asc')
|
|
|
|
|
->get();
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
|
|
|
|
RouteResource::collection($routes),
|
|
|
|
|
'Routing configurations retrieved successfully.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Store a newly created routing configuration
|
|
|
|
|
*/
|
2025-08-16 10:09:38 +02:00
|
|
|
public function store(StoreRouteRequest $request): JsonResponse
|
2025-08-02 15:20:09 +02:00
|
|
|
{
|
|
|
|
|
try {
|
2025-08-16 10:09:38 +02:00
|
|
|
$validated = $request->validated();
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
$validated['is_active'] = $validated['is_active'] ?? true;
|
|
|
|
|
$validated['priority'] = $validated['priority'] ?? 0;
|
|
|
|
|
|
|
|
|
|
$route = Route::create($validated);
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
2025-08-16 10:09:38 +02:00
|
|
|
new RouteResource($route->load(['feed', 'platformChannel', 'language', 'keywords'])),
|
2025-08-02 15:20:09 +02:00
|
|
|
'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
|
|
|
|
|
{
|
2025-08-06 20:01:28 +02:00
|
|
|
$route = $this->findRoute($feed, $channel);
|
|
|
|
|
|
2025-08-02 15:20:09 +02:00
|
|
|
if (!$route) {
|
|
|
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
|
|
|
}
|
2025-08-06 20:01:28 +02:00
|
|
|
|
2025-08-16 10:09:38 +02:00
|
|
|
$route->load(['feed', 'platformChannel', 'language', 'keywords']);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
|
|
|
|
new RouteResource($route),
|
|
|
|
|
'Routing configuration retrieved successfully.'
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Update the specified routing configuration
|
|
|
|
|
*/
|
2025-08-16 10:09:38 +02:00
|
|
|
public function update(UpdateRouteRequest $request, Feed $feed, PlatformChannel $channel): JsonResponse
|
2025-08-02 15:20:09 +02:00
|
|
|
{
|
|
|
|
|
try {
|
2025-08-06 20:01:28 +02:00
|
|
|
$route = $this->findRoute($feed, $channel);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
if (!$route) {
|
|
|
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-16 10:09:38 +02:00
|
|
|
$validated = $request->validated();
|
2025-08-02 15:20:09 +02:00
|
|
|
|
2025-08-05 21:53:49 +02:00
|
|
|
Route::where('feed_id', $feed->id)
|
|
|
|
|
->where('platform_channel_id', $channel->id)
|
|
|
|
|
->update($validated);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
2025-08-16 10:09:38 +02:00
|
|
|
new RouteResource($route->fresh(['feed', 'platformChannel', 'language', 'keywords'])),
|
2025-08-02 15:20:09 +02:00
|
|
|
'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 {
|
2025-08-06 20:01:28 +02:00
|
|
|
$route = $this->findRoute($feed, $channel);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
if (!$route) {
|
|
|
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-05 21:53:49 +02:00
|
|
|
Route::where('feed_id', $feed->id)
|
|
|
|
|
->where('platform_channel_id', $channel->id)
|
|
|
|
|
->delete();
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
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 {
|
2025-08-06 20:01:28 +02:00
|
|
|
$route = $this->findRoute($feed, $channel);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
if (!$route) {
|
|
|
|
|
return $this->sendNotFound('Routing configuration not found.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$newStatus = !$route->is_active;
|
2025-08-05 21:53:49 +02:00
|
|
|
Route::where('feed_id', $feed->id)
|
|
|
|
|
->where('platform_channel_id', $channel->id)
|
|
|
|
|
->update(['is_active' => $newStatus]);
|
2025-08-02 15:20:09 +02:00
|
|
|
|
|
|
|
|
$status = $newStatus ? 'activated' : 'deactivated';
|
|
|
|
|
|
|
|
|
|
return $this->sendResponse(
|
2025-08-16 10:09:38 +02:00
|
|
|
new RouteResource($route->fresh(['feed', 'platformChannel', 'language', 'keywords'])),
|
2025-08-02 15:20:09 +02:00
|
|
|
"Routing configuration {$status} successfully!"
|
|
|
|
|
);
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
|
return $this->sendError('Failed to toggle routing configuration status: ' . $e->getMessage(), [], 500);
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-08-06 20:01:28 +02:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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();
|
|
|
|
|
}
|
2025-08-02 15:20:09 +02:00
|
|
|
}
|