Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Discord Account API endpoint #98

Merged
merged 1 commit into from
Jan 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions app/Http/Controllers/API/v1/DiscordAccountController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Http\Controllers\API\v1;

use Illuminate\Http\Request;
use App\Models\DiscordAccount;
use App\Http\Controllers\Controller;
use App\Http\Resources\API\v1\DiscordAccountResource;


/**
* @group Discord Account
*
* APIs for getting Discord accounts.
*/
class DiscordAccountController extends Controller
{
/**
* Display the specified resource.
*
* @urlParam id int required The ID of the Discord Account.
*
* @response {
* "data": [
* {
* "id": 1,
* "uuid": "1830ef92-b58b-4671-9096-2b7741c0b0d8",
* "id": 1234567890,
* "username": "DanielRTRD",
* "discriminator": 9659,
* "verified_at": "2021-01-01T17:57:10.000000Z",
* "created_at": "2021-01-01T17:57:10.000000Z",
* "updated_at": "2021-01-01T17:57:10.000000Z",
* "deleted_at": null
* },
* ]
* }
*/
public function show(Request $request, $id)
{
if (!$request->user()->tokenCan('read')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
$gja = DiscordAccount::with(['user.roles.permissions'])->where('id', $id)->firstOrFail();
return new DiscordAccountResource($gja);
}
}
42 changes: 42 additions & 0 deletions app/Http/Resources/API/v1/DiscordAccountResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Http\Resources\API\v1;

use Illuminate\Http\Resources\Json\JsonResource;

class DiscordAccountResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
if ($request->user()->can('api.full')) {
return parent::toArray($request);
}
if ($request->user()->can('api.moderate')) {
return [
'id' => $this->id,
'username' => $this->username,
'discriminator' => $this->discriminator,
'verified_at' => $this->verified_at,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'user' => new UserResource($this->user),
];
}
if ($request->user()->can('api.minimal')) {
return [
'id' => $this->id,
'username' => $this->username,
'verified_at' => $this->verified_at,
];
}
return [
'id' => $this->id,
];
}
}
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\API\v1\UserController;
use App\Http\Controllers\API\v1\BanReasonController;
use App\Http\Controllers\API\v1\DiscordAccountController;
use App\Http\Controllers\API\v1\GamejoltAccountController;
use App\Http\Controllers\API\v1\GamejoltAccountBanController;

Expand All @@ -21,4 +22,5 @@
Route::apiResource('gamejoltaccount', GamejoltAccountController::class);
Route::apiResource('ban/gamejoltaccount', GamejoltAccountBanController::class);
Route::apiResource('banreason', BanReasonController::class);
Route::apiResource('discordaccount', DiscordAccountController::class);
});