Skip to content

Commit

Permalink
feat(feed): ( issue #324 ) Add list all topics API support 0nly hot
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 26, 2018
1 parent 8fe0020 commit b399ab6
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
28 changes: 28 additions & 0 deletions app/API2/Controllers/Feed/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,30 @@ public function __construct()
->only(['create', 'update']);
}

public function listTopicsOnlyHot(FeedTopicModel $model): JsonResponse
{
$topics = $model
->query()
->whereNotNull('hot_at')
->limit(8)
->orderBy('id', 'desc')
->get();
if (($count = $topics->count()) < 8) {
$topics = $topics->merge(
$model->query()
->whereNull('hot_at')
->limit(8 - $count)
->orderBy('feeds_count', 'desc')
->get()
->all()
)->values();
}

return (new TopicCollectionResource($topics))
->response()
->setStatusCode(Response::HTTP_OK /* 200 */);
}

/**
* List topics.
*
Expand All @@ -62,6 +86,10 @@ public function __construct()
*/
public function index(IndexRequest $request, FeedTopicModel $model): JsonResponse
{
if ($request->query('only') === 'hot') {
return $this->listTopicsOnlyHot($model);
}

// Get query data `id` order direction.
// Value: `asc` or `desc`
$direction = $request->query('direction', 'desc');
Expand Down
2 changes: 2 additions & 0 deletions app/API2/Requests/Feed/TopicIndex.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function rules(): array
'index' => ['nullable', 'integer', 'min:0'],
'direction' => ['nullable', 'in:asc,desc'],
'q' => ['nullable', 'string'],
'only' => ['nullable', 'string', 'in:hot']
];
}

Expand All @@ -54,6 +55,7 @@ public function messages(): array
'index.min' => 'index 必须是大于 0 的正整数',
'direction.in' => '排序方向值非法,必须是 `asc` 或者 `desc`',
'q.string' => '输入的搜索关键词不合法',
'only.in' => 'only 只接受 `hot` 值',
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public function up()
$table->integer('creator_user_id')->unsigned()->comment('The topic creator user ID');
$table->integer('feeds_count')->unsigned()->nullable()->default(0)->comment('The topic link feeds count');
$table->integer('followers_count')->unsigned()->nullable()->default(0)->comment('The topic followers count');
$table->dateTime('hot_at')->nullable()->default(null)->comment('设置为热门的时间');
$table->timestamps();

$table->unique('name');
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,7 @@
* @Param::query {limit} Featch data limit.
* @Param::query {index} Featch data start index.
* @Param::query {direction} Can be one of `asc` or `desc`.
* @Param::query('only', 'string', 'The value is `hot`')
* @Response::header('Status', 200, 'OK')
* @Response::json('<pre>
* [{
Expand Down

0 comments on commit b399ab6

Please sign in to comment.