feat: add read-only Social Accounts API
Add GET /api/accounts (list with optional ?network= and ?type= filters) and
GET /api/accounts/{socialAccount} (show) endpoints. Include plain-text password
field for AI agent consumption.
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 5
parent
20c33f1c23
commit
5c24b6fbe0
@@ -0,0 +1,31 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Controllers\Api;
|
||||||
|
|
||||||
|
use App\Http\Controllers\Controller;
|
||||||
|
use App\Http\Resources\SocialAccountResource;
|
||||||
|
use App\Models\SocialAccount;
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
|
||||||
|
class SocialAccountController extends Controller
|
||||||
|
{
|
||||||
|
public function index(Request $request)
|
||||||
|
{
|
||||||
|
$query = SocialAccount::query();
|
||||||
|
|
||||||
|
if ($request->filled('network')) {
|
||||||
|
$query->where('network', $request->string('network'));
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($request->filled('type')) {
|
||||||
|
$query->where('type', $request->string('type'));
|
||||||
|
}
|
||||||
|
|
||||||
|
return SocialAccountResource::collection($query->orderBy('username')->get());
|
||||||
|
}
|
||||||
|
|
||||||
|
public function show(SocialAccount $socialAccount)
|
||||||
|
{
|
||||||
|
return new SocialAccountResource($socialAccount);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Http\Resources;
|
||||||
|
|
||||||
|
use Illuminate\Http\Request;
|
||||||
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
|
class SocialAccountResource extends JsonResource
|
||||||
|
{
|
||||||
|
public function toArray(Request $request): array
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
'id' => $this->id,
|
||||||
|
'network' => $this->network,
|
||||||
|
'username' => $this->username,
|
||||||
|
'email' => $this->email,
|
||||||
|
'password' => $this->password,
|
||||||
|
'type' => $this->type,
|
||||||
|
'created_at' => $this->created_at,
|
||||||
|
'updated_at' => $this->updated_at,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,7 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
use App\Http\Controllers\Api\SiteController;
|
use App\Http\Controllers\Api\SiteController;
|
||||||
|
use App\Http\Controllers\Api\SocialAccountController;
|
||||||
use Illuminate\Support\Facades\Route;
|
use Illuminate\Support\Facades\Route;
|
||||||
|
|
||||||
Route::get('/sites', [SiteController::class, 'index']);
|
Route::get('/sites', [SiteController::class, 'index']);
|
||||||
Route::get('/sites/{site}', [SiteController::class, 'show']);
|
Route::get('/sites/{site}', [SiteController::class, 'show']);
|
||||||
|
|
||||||
|
Route::get('/accounts', [SocialAccountController::class, 'index']);
|
||||||
|
Route::get('/accounts/{socialAccount}', [SocialAccountController::class, 'show']);
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\SocialAccount;
|
||||||
|
|
||||||
|
test('it lists all social accounts including password', function () {
|
||||||
|
SocialAccount::factory()->count(3)->create();
|
||||||
|
|
||||||
|
$response = $this->getJson('/api/accounts');
|
||||||
|
|
||||||
|
$response->assertOk()->assertJsonCount(3, 'data');
|
||||||
|
expect($response->json('data.0'))->toHaveKey('password');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it filters accounts by network', function () {
|
||||||
|
SocialAccount::factory()->create(['network' => 'twitter']);
|
||||||
|
SocialAccount::factory()->create(['network' => 'instagram']);
|
||||||
|
|
||||||
|
$response = $this->getJson('/api/accounts?network=twitter');
|
||||||
|
|
||||||
|
$response->assertOk()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonPath('data.0.network', 'twitter');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it filters accounts by type', function () {
|
||||||
|
SocialAccount::factory()->create(['type' => 'post']);
|
||||||
|
SocialAccount::factory()->create(['type' => 'like']);
|
||||||
|
|
||||||
|
$response = $this->getJson('/api/accounts?type=post');
|
||||||
|
|
||||||
|
$response->assertOk()
|
||||||
|
->assertJsonCount(1, 'data')
|
||||||
|
->assertJsonPath('data.0.type', 'post');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it shows a single account with password', function () {
|
||||||
|
$account = SocialAccount::factory()->create(['password' => 'super-secret']);
|
||||||
|
|
||||||
|
$response = $this->getJson("/api/accounts/{$account->id}");
|
||||||
|
|
||||||
|
$response->assertOk()->assertJsonPath('data.password', 'super-secret');
|
||||||
|
});
|
||||||
|
|
||||||
|
test('it returns 404 for a missing account', function () {
|
||||||
|
$this->getJson('/api/accounts/999')->assertNotFound();
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user