. */ namespace App\Http\Controllers\Api\Admin; use App\Http\Controllers\Controller; use App\Rules\Vcard; use App\VcardStorage; use Illuminate\Http\Request; use Sabre\VObject; use stdClass; class VcardsStorageController extends Controller { public function index(Request $request, int $accountId) { $list = $request->space->accounts()->findOrFail($accountId)->vcardsStorage()->get()->keyBy('uuid'); return $list->isEmpty() ? new stdClass : $list; } public function show(Request $request, int $accountId, string $uuid) { return $request->space->accounts()->findOrFail($accountId)->vcardsStorage()->where('uuid', $uuid)->firstOrFail(); } public function store(Request $request, int $accountId) { $request->validate([ 'vcard' => ['required', new Vcard()] ]); $vcardo = VObject\Reader::read($request->get('vcard')); if ($request->space->accounts()->findOrFail($accountId)->vcardsStorage()->where('uuid', $vcardo->UID)->first()) { abort(409, 'Vcard already exists'); } $vcard = new VcardStorage(); $vcard->account_id = $accountId; $vcard->uuid = $vcardo->UID; $vcard->vcard = preg_replace('/\r\n?/', "\n", $vcardo->serialize()); $vcard->save(); return $vcard; } public function update(Request $request, int $accountId, string $uuid) { $request->validate([ 'vcard' => ['required', new Vcard()] ]); $vcardo = VObject\Reader::read($request->get('vcard')); if ($vcardo->UID != $uuid) { abort(422, 'UUID should be the same'); } $vcard = $request->space->accounts()->findOrFail($accountId)->vcardsStorage()->where('uuid', $uuid)->firstOrFail(); $vcard->vcard = preg_replace('/\r\n?/', "\n", $vcardo->serialize()); $vcard->save(); return $vcard; } public function destroy(Request $request, int $accountId, string $uuid) { $vcard = $request->space->accounts()->findOrFail($accountId)->vcardsStorage()->where('uuid', $uuid)->firstOrFail(); return $vcard->delete(); } }