Skip to content

Commit

Permalink
Merge pull request #283 from P3D-Legacy/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dsbilling authored Jul 17, 2023
2 parents 0cf62f8 + 09f5a28 commit 78630b0
Show file tree
Hide file tree
Showing 34 changed files with 637 additions and 604 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function handle(): int
$this->info('Running storage:link command...');
Artisan::call('storage:link');
$this->info('Generating API Docs...');
Artisan::call('scribe:generate');
Artisan::call('api:docs');
$this->info('Getting Github release...');
Artisan::call('github:syncrelease');
$this->info('Getting Discord roles...');
Expand Down
56 changes: 56 additions & 0 deletions app/Console/Commands/UpdateAPIDocs.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace App\Console\Commands;

use cebe\openapi\exceptions\IOException;
use cebe\openapi\exceptions\TypeErrorException;
use cebe\openapi\exceptions\UnresolvableReferenceException;
use cebe\openapi\Reader;
use cebe\openapi\Writer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;

class UpdateAPIDocs extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'api:docs';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Update the API documentation in JSON format from the OpenAPI documentation in YAML format.';

/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
// Generate Scribe API Docs
Artisan::call('scribe:generate');

// Get YAML from storage
$file_path = storage_path('app/scribe/openapi.yaml');
try {
$openapi = Reader::readFromYamlFile($file_path);
} catch (IOException|TypeErrorException|UnresolvableReferenceException $e) {
$this->error($e->getMessage());

return Command::FAILURE;
}
$json = Writer::writeToJson($openapi);
// Save JSON to storage
$file_path = storage_path('app/scribe/openapi.json');
file_put_contents($file_path, $json);
$this->info('API documentation updated.');

return Command::SUCCESS;
}
}
17 changes: 17 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace App\Exceptions;

use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Illuminate\Http\JsonResponse;
use Spatie\Permission\Exceptions\UnauthorizedException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Throwable;

Expand Down Expand Up @@ -50,4 +54,17 @@ public function register()
}
});
}

public function render($request, Throwable $e): \Illuminate\Http\Response|JsonResponse|Response
{
if ($e instanceof AuthorizationException && $request->expectsJson()) {
return response()->json(['error' => 'Unauthorized'], 403);
}

if ($e instanceof UnauthorizedException && $request->expectsJson()) {
return response()->json(['error' => 'Unauthorized'], 403);
}

return parent::render($request, $e);
}
}
55 changes: 9 additions & 46 deletions app/Http/Controllers/API/v1/BanReasonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,18 @@ class BanReasonController extends Controller
{
public function __construct()
{
//$this->middleware(['permission:api']);
$this->middleware('permission:ban_reason.show')->only(['index', 'show']);
}

/**
* Display a listing of the resource.
*
* @response {
* "data": [
* {
* "uuid": "1830ef92-b58b-4671-9096-2b7741c0b0d8",
* "name": "Abusing in-game glitches",
* "user_id": 1,
* "created_at": "2021-01-01T17:57:10.000000Z",
* "updated_at": "2021-01-01T17:57:10.000000Z",
* "deleted_at": null
* },
* {
* "id": 2,
* "uuid": "ba3900a8-bc82-43c6-9d4f-1760205f95f6",
* "name": "General hacking or cheating",
* "user_id": 1,
* "created_at": "2022-01-04T18:49:46.000000Z",
* "updated_at": "2022-01-04T18:49:46.000000Z",
* "deleted_at": null
* },
* ]
* }
* @apiResourceCollection App\Http\Resources\API\v1\BanReasonResource
*
* @apiResourceModel App\Models\BanReason
*/
public function index(Request $request)
public function index(Request $request): \Illuminate\Http\JsonResponse|\Illuminate\Http\Resources\Json\AnonymousResourceCollection
{
if (! $request->user()->tokenCan('read')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
$resources = BanReason::all();

return BanReasonResource::collection($resources);
Expand All @@ -61,26 +38,12 @@ public function index(Request $request)
*
* @urlParam id string required The UUID of the ban reason.
*
* @response {
* "data": [
* {
* "uuid": "1830ef92-b58b-4671-9096-2b7741c0b0d8",
* "name": "Abusing in-game glitches",
* "user_id": 1,
* "created_at": "2021-01-01T17:57:10.000000Z",
* "updated_at": "2021-01-01T17:57:10.000000Z",
* "deleted_at": null
* },
* ]
* }
* @apiResource App\Http\Resources\API\v1\BanReasonResource
*
* @apiResourceModel App\Models\BanReason
*/
public function show(Request $request, $id)
public function show(Request $request, $id): BanReasonResource|\Illuminate\Http\JsonResponse
{
if (! $request->user()->tokenCan('read')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
$resource = BanReason::findOrFail($id);

return new BanReasonResource($resource);
Expand Down
30 changes: 9 additions & 21 deletions app/Http/Controllers/API/v1/DiscordAccountController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,34 +14,22 @@
*/
class DiscordAccountController extends Controller
{
public function __construct()
{
$this->middleware('permission:discord_account.show')->only(['show']);
}

/**
* 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
* },
* ]
* }
* @apiResource App\Http\Resources\API\v1\DiscordAccountResource
*
* @apiResourceModel App\Models\DiscordAccount
*/
public function show(Request $request, $id)
public function show(Request $request, $id): \Illuminate\Http\JsonResponse|DiscordAccountResource
{
if (! $request->user()->tokenCan('read')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
$account = DiscordAccount::with(['roles', 'user.roles.permissions'])->findOrFail($id);

return new DiscordAccountResource($account);
Expand Down
25 changes: 11 additions & 14 deletions app/Http/Controllers/API/v1/DiscordBotSettingController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Http\Controllers\Controller;
use App\Models\DiscordBotSetting;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

/**
Expand All @@ -13,10 +14,16 @@
*/
class DiscordBotSettingController extends Controller
{
public function __construct()
{
$this->middleware('permission:discord_bot_setting.show')->only(['index']);
$this->middleware('permission:discord_bot_setting.update')->only(['update']);
}

/**
* Display the first resource.
*
* @response {
* @jsonresponse {
* "data": [
* {
* "category_id": 1,
Expand All @@ -29,13 +36,8 @@ class DiscordBotSettingController extends Controller
* ]
* }
*/
public function index(Request $request): \Illuminate\Http\JsonResponse
public function index(Request $request): JsonResponse
{
if (! $request->user()->tokenCan('read')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
$discordBotSetting = DiscordBotSetting::first(); // Only show first in table

return response()->json($discordBotSetting);
Expand All @@ -51,7 +53,7 @@ public function index(Request $request): \Illuminate\Http\JsonResponse
* @bodyParam events_id int required The ID of your desired event channel.
* @bodyParam hide_events json A JSON object.
*
* @response 201 {
* @jsonresponse 201 {
* "category_id": 1,
* "chat_id": 1,
* "events_id": 1,
Expand All @@ -60,13 +62,8 @@ public function index(Request $request): \Illuminate\Http\JsonResponse
* "updated_at": "2021-01-01T17:57:10.000000Z",
* }
*/
public function update(Request $request, int $id): \Illuminate\Http\JsonResponse
public function update(Request $request, int $id): JsonResponse
{
if (! $request->user()->tokenCan('update')) {
return response()->json([
'error' => 'Token does not have access!',
]);
}
if ($id !== 1) {
return response()->json([
'error' => 'Invalid ID!',
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/API/v1/Game/BadgeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BadgeController extends Controller
/**
* Display a listing of the resource.
*
* @response {
* @jsonresponse {
* "boulder": {
* "name": "Boulder",
* "image": "https://pokemon3d.net/img/badge/Boulder.png"
Expand Down
Loading

0 comments on commit 78630b0

Please sign in to comment.