Skip to content

Commit

Permalink
feat(feed): Add a feed topic list API
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 23, 2018
1 parent bfba02d commit 8406dd0
Show file tree
Hide file tree
Showing 5 changed files with 305 additions and 0 deletions.
87 changes: 87 additions & 0 deletions app/API2/Controllers/Feed/Topic.php
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 <master@zhiyicx.com> |
* | 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;
}
}
59 changes: 59 additions & 0 deletions app/API2/Requests/Feed/TopicIndex.php
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 <master@zhiyicx.com> |
* | 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' => '输入的搜索关键词不合法',
];
}
}
66 changes: 66 additions & 0 deletions app/API2/Requests/Request.php
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 <master@zhiyicx.com> |
* | 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 [];
}
}
64 changes: 64 additions & 0 deletions app/API2/Resources/Feed/TopicCollection.php
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 <master@zhiyicx.com> |
* | 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}),
];
}
}
29 changes: 29 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -616,4 +616,33 @@
* 重置未读信息
*/
$api->patch('/user/counts', \Zhiyi\Plus\API2\Controllers\UserCountsController::class.'@reset');

// Feed group
// @Route /api/v2/feed
$api->group(['prefix' => 'feed'], function (RouteContract $api) {
// Feed Topics Group
// @Route /api/v2/feed/topics
$api->group(['prefix' => 'topics'], function (RouteContract $api) {
/**
* Topic Index
*
* @Get /api/v2/feed/topics
* @Param::query {q} Search topic name keyword.
* @Param::query {limit} Featch data limit.
* @Param::query {index} Featch data start index.
* @Param::query {direction} Can be one of `asc` or `desc`.
* @Response::headers('Status', 200, 'OK')
* @Response::json
* <pre>
* [{
* "id": 1, // Topic ID
* "name": "Plus", // Topic name
* "logo": 2, // Topic logo, file with ID
* "created_at": "2018-07-23T15:04:23Z" // Topic created datetime
* }]
* </pre>
*/
$api->get('', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@index');
});
});
});

1 comment on commit 8406dd0

@medz
Copy link
Contributor Author

@medz medz commented on 8406dd0 Jul 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

issue #324

Please sign in to comment.