Skip to content

Commit

Permalink
+ route /forums/{fid}/threads/tid forwarding to `App\Http\Controlle…
Browse files Browse the repository at this point in the history
…rs\ThreadsIDQuery->query()` @ routes/api.php

+ `App\Http\Controllers\ThreadsIDQuery` to print tid by every 1k threads with cursor pagination

* replace the invoking to `Symfony\Component\HttpFoundation\Response->send()` with `Illuminate\Http\ResponseTrait::throwResponse()` to pass through registered middlewares
* rename error code `40006` to `40406`
@ `App\Helper->abortAPI()`

* replace usages of facade `\Response::` with global function `response()` @ `App\Exceptions\Handler->convertValidationExceptionToResponse()` & `App\Helper->abortAPI()`
@ be
  • Loading branch information
n0099 committed Jul 3, 2024
1 parent b3813db commit 8089d16
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 12 deletions.
2 changes: 1 addition & 1 deletion be/app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function convertValidationExceptionToResponse(\Illuminate\Validation\V
return $e->response;
}

return \Response::json([
return response()->json([
'errorCode' => 40000,
'errorInfo' => $e->validator->getMessageBag()->getMessages()
], 400);
Expand Down
7 changes: 2 additions & 5 deletions be/app/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public static function abortAPI(int $errorCode): never
40003 => '部分查询参数与查询帖子类型要求不匹配',
40004 => '排序方式与查询帖子类型要求不匹配',
40005 => '提供了多个唯一查询参数',
40006 => '指定查询的贴吧不存在',
],
401 => [
40101 => 'Google reCAPTCHA 验证未通过 请刷新页面/更换设备/网络环境后重试',
Expand All @@ -77,6 +76,7 @@ public static function abortAPI(int $errorCode): never
40401 => '帖子查询结果为空',
40402 => '用户查询结果为空',
40403 => '吧帖量统计查询结果为空',
40406 => '指定查询的贴吧不存在',
],
500 => [
50001 => '数据库中存在多个贴吧表存储了该 ID 的帖子',
Expand All @@ -94,10 +94,7 @@ public static function abortAPI(int $errorCode): never
if ($errorInfo === null) {
throw new \InvalidArgumentException('Given error code doesn\'t existed');
}
\Response::json([
'errorCode' => $errorCode,
'errorInfo' => $errorInfo
], $statusCode)->send();
response()->json(compact('errorCode', 'errorInfo'), $statusCode)->throwResponse();
exit;
}

Expand Down
30 changes: 30 additions & 0 deletions be/app/Http/Controllers/ThreadsIDQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Controllers;

use App\Eloquent\Model\Forum;
use App\Eloquent\Model\Post\PostFactory;
use App\Helper;
use Illuminate\Pagination\Cursor;

class ThreadsIDQuery extends Controller
{
public function query(\Illuminate\Http\Request $request, int $fid): array
{
['cursor' => $cursor] = $request->validate([
'cursor' => 'integer'
]) + ['cursor' => 0];
Helper::abortAPIIfNot(40406, (new Forum())->fid($fid)->exists());
$thread = PostFactory::newThread($fid);
$primaryKey = $thread->getTable() . '.tid';
$paginators = $thread->cursorPaginate(1000, columns: 'tid', cursor: new Cursor([$primaryKey => $cursor]));

return [
'pages' => [
'currentCursor' => $paginators->cursor()->parameter($primaryKey),
'nextCursor' => $paginators->nextCursor()->parameter($primaryKey)
],
'tid' => array_column($paginators->items(), 'tid')
];
}
}
2 changes: 1 addition & 1 deletion be/app/Http/PostsQuery/IndexQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public function query(QueryParams $params, ?string $cursor): self
$fid = $getFidByPostIDParam($postIDParamName, $postIDParamValue);
$queries = $getQueryBuilders($fid);
} else {
Helper::abortAPI(40006);
Helper::abortAPI(40406);
}
} elseif ($hasPostIDParam) { // query by post ID only
$fid = $getFidByPostIDParam($postIDParamName, $postIDParamValue);
Expand Down
9 changes: 4 additions & 5 deletions be/routes/api.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
<?php

use App\Http\Controllers\PostsQuery;
use App\Http\Controllers\UsersQuery;
use App\Http\Controllers;
use App\Http\Middleware\ReCAPTCHACheck;
use Illuminate\Support\Facades\Route;

Route::get('/forums', static fn () => \App\Eloquent\Model\Forum::all()->toArray());

Route::get('/forums/{fid}/threads/tid', [Controllers\ThreadsIDQuery::class, 'query']);
Route::middleware(ReCAPTCHACheck::class)->group(static function () {
Route::get('/posts', [PostsQuery::class, 'query']);
Route::get('/users', [UsersQuery::class, 'query']);
Route::get('/posts', [Controllers\PostsQuery::class, 'query']);
Route::get('/users', [Controllers\UsersQuery::class, 'query']);
});

0 comments on commit 8089d16

Please sign in to comment.