-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(feed): Add a feed topic list API
- Loading branch information
Showing
5 changed files
with
305 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <[email protected]> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\API2\Controllers\Feed; | ||
|
||
use Illuminate\Database\Eloquent\Builder as EloquentBuilder; | ||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Zhiyi\Plus\API2\Controllers\Controller; | ||
use Zhiyi\Plus\API2\Requests\Feed\TopicIndex as IndexRequest; | ||
use Zhiyi\Plus\API2\Resources\Feed\TopicCollection; | ||
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel; | ||
|
||
class Topic extends Controller | ||
{ | ||
|
||
/** | ||
* List topics. | ||
* | ||
* @param \Zhiyi\Plus\Requests\Feed\TopicIndex $request | ||
* @param \Zhiyi\Plus\Models\FeedTopic $model | ||
* @return // | ||
*/ | ||
public function index(IndexRequest $request, FeedTopicModel $model): JsonResponse | ||
{ | ||
// Get query data `id` order direction. | ||
// Value: `asc` or `desc` | ||
$direction = $request->query('direction', 'desc'); | ||
|
||
// Query database data. | ||
$result = $model | ||
->query() | ||
|
||
// If `$request->query('q')` param exists, | ||
// create "`name` like %?%" SQL where. | ||
->when((bool) ($searchKeyword = $request->query('q', false)), function (EloquentBuilder $query) use ($searchKeyword) { | ||
return $query->where('name', 'like', sprintf('%%%s%%', $searchKeyword)); | ||
}) | ||
|
||
// If `$request->query('index)` param exists, | ||
// using `$direction` create "id ? ?" where | ||
// ?[0] `$direction === asc` is `>` | ||
// ?[0] `$direction === desc` is `<` | ||
->when((bool) ($indexID = $request->query('index', false)), function (EloquentBuilder $query) use ($indexID, $direction) { | ||
return $query->where('id', $direction === 'asc' ? '>' : '<', $indexID); | ||
}) | ||
|
||
// Set the number of data | ||
->limit($request->query('limit', 15)) | ||
|
||
// Using `$direction` set `id` direction, | ||
// the `$direction` enum `asc` or `desc`. | ||
->orderBy('id', $direction) | ||
|
||
// Set only query table column name. | ||
->select('id', 'name', 'logo', Model::CREATED_AT) | ||
|
||
// Run the SQL query, return a collection. | ||
// instanceof \Illuminate\Support\Collection | ||
->get(); | ||
|
||
// Create the action response. | ||
$response = (new TopicCollection($result)) | ||
->response() | ||
->setStatusCode(Response::HTTP_OK /* 200 */); | ||
|
||
return $response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <[email protected]> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\API2\Requests\Feed; | ||
|
||
use Zhiyi\Plus\API2\Requests\Request; | ||
|
||
class TopicIndex extends Request | ||
{ | ||
/** | ||
* Get the validator rules. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'limit' => ['nullable', 'integer', 'min:1', 'max:100'], | ||
'index' => ['nullable', 'integer', 'min:0'], | ||
'direction' => ['nullable', 'in:asc,desc'], | ||
'q' => ['nullable', 'string'], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the validator error messages. | ||
* | ||
* @return array | ||
*/ | ||
public function messages(): array | ||
{ | ||
return [ | ||
'limit.integer' => '请求数据量必须必须是整数', | ||
'limit.min' => '请求数据量最少 1 条', | ||
'limit.max' => '请求数据量最多 100 条', | ||
'index.integer' => '请求参数类型非法,index 必须是整数', | ||
'index.min' => 'index 必须是大于 0 的正整数', | ||
'direction.in' => '排序方向值非法,必须是 `asc` 或者 `desc`', | ||
'q.string' => '输入的搜索关键词不合法', | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <[email protected]> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\API2\Requests; | ||
|
||
use Illuminate\Foundation\Http\FormRequest; | ||
|
||
class Request extends FormRequest | ||
{ | ||
/** | ||
* Get request authorize. | ||
* | ||
* @return bool | ||
*/ | ||
public function authorize(): bool | ||
{ | ||
return true; | ||
} | ||
|
||
/** | ||
* Get custom message from validateor rules. | ||
* | ||
* @return array | ||
*/ | ||
public function rules(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Get custom messages for validator errors. | ||
* | ||
* @return array | ||
*/ | ||
public function messages(): array | ||
{ | ||
return []; | ||
} | ||
|
||
/** | ||
* Get custom attributes for validator errors. | ||
* | ||
* @return array | ||
*/ | ||
public function attributes(): array | ||
{ | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* +----------------------------------------------------------------------+ | ||
* | ThinkSNS Plus | | ||
* +----------------------------------------------------------------------+ | ||
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. | | ||
* +----------------------------------------------------------------------+ | ||
* | This source file is subject to version 2.0 of the Apache license, | | ||
* | that is bundled with this package in the file LICENSE, and is | | ||
* | available through the world-wide-web at the following url: | | ||
* | http://www.apache.org/licenses/LICENSE-2.0.html | | ||
* +----------------------------------------------------------------------+ | ||
* | Author: Slim Kit Group <[email protected]> | | ||
* | Homepage: www.thinksns.com | | ||
* +----------------------------------------------------------------------+ | ||
*/ | ||
|
||
namespace Zhiyi\Plus\API2\Resources\Feed; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Illuminate\Http\Resources\Json\ResourceCollection; | ||
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel; | ||
use Zhiyi\Plus\Utils\DateTimeToIso8601ZuluString; | ||
|
||
class TopicCollection extends ResourceCollection | ||
{ | ||
use DateTimeToIso8601ZuluString; | ||
|
||
/** | ||
* The collection to array. | ||
* | ||
* @param \Illuminate\Http\Request $request | ||
* @return array | ||
*/ | ||
public function toArray($request): array | ||
{ | ||
return $this | ||
->collection | ||
->map(function (FeedTopicModel $item) use ($request): array { | ||
return $this->collectionItemToArray($item, $request); | ||
}) | ||
->values() | ||
->toArray(); | ||
} | ||
|
||
/** | ||
* The collection tem to array. | ||
* | ||
* @param \Zhiyi\Plus\Models\FeedTopic $item | ||
* @return array | ||
*/ | ||
public function collectionItemToArray(FeedTopicModel $item): array | ||
{ | ||
return [ | ||
'id' => $item->id, | ||
'name' => $item->name, | ||
'logo' => $this->when((bool) $item->logo, $item->logo), | ||
'created_at' => $this->dateTimeToIso8601ZuluString($this->${Model::CREATED_AT}), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8406dd0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue #324