151 lines
No EOL
4.9 KiB
PHP
151 lines
No EOL
4.9 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\Api\V1;
|
|
|
|
use App\Enums\PlatformEnum;
|
|
use App\Http\Resources\PlatformAccountResource;
|
|
use App\Models\PlatformAccount;
|
|
use App\Models\PlatformInstance;
|
|
use Illuminate\Http\JsonResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
class PlatformAccountsController extends BaseController
|
|
{
|
|
/**
|
|
* Display a listing of platform accounts
|
|
*/
|
|
public function index(): JsonResponse
|
|
{
|
|
$accounts = PlatformAccount::orderBy('platform')
|
|
->orderBy('created_at', 'desc')
|
|
->get();
|
|
|
|
return $this->sendResponse(
|
|
PlatformAccountResource::collection($accounts),
|
|
'Platform accounts retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Store a newly created platform account
|
|
*/
|
|
public function store(Request $request): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'platform' => 'required|in:lemmy,mastodon,reddit',
|
|
'instance_url' => 'required|url',
|
|
'username' => 'required|string|max:255',
|
|
'password' => 'required|string',
|
|
'settings' => 'nullable|array',
|
|
]);
|
|
|
|
// Create or find platform instance
|
|
$platformEnum = PlatformEnum::from($validated['platform']);
|
|
$instance = PlatformInstance::firstOrCreate([
|
|
'platform' => $platformEnum,
|
|
'url' => $validated['instance_url'],
|
|
], [
|
|
'name' => parse_url($validated['instance_url'], PHP_URL_HOST),
|
|
'description' => ucfirst($validated['platform']) . ' instance',
|
|
'is_active' => true,
|
|
]);
|
|
|
|
$account = PlatformAccount::create($validated);
|
|
|
|
// If this is the first account for this platform, make it active
|
|
if (!PlatformAccount::where('platform', $validated['platform'])
|
|
->where('is_active', true)
|
|
->exists()) {
|
|
$account->setAsActive();
|
|
}
|
|
|
|
return $this->sendResponse(
|
|
new PlatformAccountResource($account),
|
|
'Platform account created successfully!',
|
|
201
|
|
);
|
|
} catch (ValidationException $e) {
|
|
return $this->sendValidationError($e->errors());
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to create platform account: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified platform account
|
|
*/
|
|
public function show(PlatformAccount $platformAccount): JsonResponse
|
|
{
|
|
return $this->sendResponse(
|
|
new PlatformAccountResource($platformAccount),
|
|
'Platform account retrieved successfully.'
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Update the specified platform account
|
|
*/
|
|
public function update(Request $request, PlatformAccount $platformAccount): JsonResponse
|
|
{
|
|
try {
|
|
$validated = $request->validate([
|
|
'instance_url' => 'required|url',
|
|
'username' => 'required|string|max:255',
|
|
'password' => 'nullable|string',
|
|
'settings' => 'nullable|array',
|
|
]);
|
|
|
|
// Don't update password if not provided
|
|
if (empty($validated['password'])) {
|
|
unset($validated['password']);
|
|
}
|
|
|
|
$platformAccount->update($validated);
|
|
|
|
return $this->sendResponse(
|
|
new PlatformAccountResource($platformAccount->fresh()),
|
|
'Platform account updated successfully!'
|
|
);
|
|
} catch (ValidationException $e) {
|
|
return $this->sendValidationError($e->errors());
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to update platform account: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified platform account
|
|
*/
|
|
public function destroy(PlatformAccount $platformAccount): JsonResponse
|
|
{
|
|
try {
|
|
$platformAccount->delete();
|
|
|
|
return $this->sendResponse(
|
|
null,
|
|
'Platform account deleted successfully!'
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to delete platform account: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Set platform account as active
|
|
*/
|
|
public function setActive(PlatformAccount $platformAccount): JsonResponse
|
|
{
|
|
try {
|
|
$platformAccount->setAsActive();
|
|
|
|
return $this->sendResponse(
|
|
new PlatformAccountResource($platformAccount->fresh()),
|
|
"Set {$platformAccount->username}@{$platformAccount->instance_url} as active for {$platformAccount->platform->value}!"
|
|
);
|
|
} catch (\Exception $e) {
|
|
return $this->sendError('Failed to set platform account as active: ' . $e->getMessage(), [], 500);
|
|
}
|
|
}
|
|
} |