133 lines
No EOL
4.3 KiB
PHP
133 lines
No EOL
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Http\Resources\PlatformChannelResource;
|
|
use App\Models\PlatformChannel;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PlatformChannelsController extends BaseController
|
|
{
|
|
/**
|
|
* Display a listing of platform channels
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
$channels = PlatformChannel::with(['platformInstance'])
|
|
->orderBy('is_active', 'desc')
|
|
->orderBy('name')
|
|
->get();
|
|
|
|
return $this->sendResponse(
|
|
PlatformChannelResource::collection($channels),
|
|
'Platform channels retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created platform channel
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'platform_instance_id' => 'required|exists:platform_instances,id',
|
|
'channel_id' => 'required|string|max:255',
|
|
'name' => 'required|string|max:255',
|
|
'display_name' => 'nullable|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'is_active' => 'boolean',
|
|
]);
|
|
|
|
$validated['is_active'] = $validated['is_active'] ?? true;
|
|
|
|
$channel = PlatformChannel::create($validated);
|
|
|
|
return $this->sendResponse(
|
|
new PlatformChannelResource($channel->load('platformInstance')),
|
|
'Platform channel created successfully!',
|
|
201
|
|
);
|
|
} catch (ValidationException $e) {
|
|
return $this->sendValidationError($e->errors());
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to create platform channel: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified platform channel
|
|
*/
|
|
public function show(PlatformChannel $platformChannel): JsonResponse
|
|
{
|
|
return $this->sendResponse(
|
|
new PlatformChannelResource($platformChannel->load('platformInstance')),
|
|
'Platform channel retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update the specified platform channel
|
|
*/
|
|
public function update(Request $request, PlatformChannel $platformChannel): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'name' => 'required|string|max:255',
|
|
'display_name' => 'nullable|string|max:255',
|
|
'description' => 'nullable|string',
|
|
'is_active' => 'boolean',
|
|
]);
|
|
|
|
$platformChannel->update($validated);
|
|
|
|
return $this->sendResponse(
|
|
new PlatformChannelResource($platformChannel->fresh(['platformInstance'])),
|
|
'Platform channel updated successfully!'
|
|
);
|
|
} catch (ValidationException $e) {
|
|
return $this->sendValidationError($e->errors());
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to update platform channel: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified platform channel
|
|
*/
|
|
public function destroy(PlatformChannel $platformChannel): JsonResponse
|
|
{
|
|
try {
|
|
$platformChannel->delete();
|
|
|
|
return $this->sendResponse(
|
|
null,
|
|
'Platform channel deleted successfully!'
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to delete platform channel: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Toggle platform channel active status
|
|
*/
|
|
public function toggle(PlatformChannel $channel): JsonResponse
|
|
{
|
|
try {
|
|
$newStatus = !$channel->is_active;
|
|
$channel->update(['is_active' => $newStatus]);
|
|
|
|
$status = $newStatus ? 'activated' : 'deactivated';
|
|
|
|
return $this->sendResponse(
|
|
new PlatformChannelResource($channel->fresh(['platformInstance'])),
|
|
"Platform channel {$status} successfully!"
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to toggle platform channel status: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
} |