Skip to content

Commit

Permalink
feat(feed): Add list participants for a topic API
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 30, 2018
1 parent 861f453 commit 179267a
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 2 deletions.
2 changes: 1 addition & 1 deletion app/API2/Controllers/Feed/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ public function show(FeedTopicModel $topic): JsonResponse
->users()
->newPivotQuery()
->where('user_id', '!=', $topic->creator_user_id)
->orderBy(Model::UPDATE_AT, 'desc')
->orderBy(Model::UPDATED_AT, 'desc')
->limit(3)
->select('user_id')
->get()
Expand Down
54 changes: 54 additions & 0 deletions app/API2/Controllers/Feed/TopicParticipant.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Http\JsonResponse;
use Illuminate\Database\Eloquent\Model;
use Zhiyi\Plus\API2\Controllers\Controller;
use Zhiyi\Plus\Models\FeedTopicUserLink as FeedTopicUserLinkModel;
use Zhiyi\Plus\API2\Requests\Feed\ListParticipantsForATopic as ListParticipantsForATopicRequest;
use Zhiyi\Plus\API2\Resources\Feed\TopicParticipantCollection as TopicParticipantCollectionResponse;

class TopicParticipant extends Controller
{
/**
* List participants for a topic.
*
* @param \Zhiyi\Plus\API2\Requests\Feed\ListParticipantsForATopic $request
* @param \Zhiyi\Plus\Models\FeedTopicUserLink $model
* @param int $topic
* @return \Illuminate\Http\JsonResponse
*/
public function index(ListParticipantsForATopicRequest $request, FeedTopicUserLinkModel $model, int $topic): JsonResponse
{
$result = $model
->query()
->where('topic_id', $topic)
->limit($request->query('limit', 15))
->offset($request->query('offset', 0))
->orderBy(Model::UPDATED_AT, 'desc')
->get();

return (new TopicParticipantCollectionResponse($result))
->response()
->setStatusCode(JsonResponse::HTTP_OK /* 200 */);
}
}
54 changes: 54 additions & 0 deletions app/API2/Requests/Feed/ListParticipantsForATopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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 ListParticipantsForATopic extends Request
{
/**
* Get the validator rules.
*
* @return array
*/
public function rules(): array
{
return [
'limit' => ['nullable', 'integer', 'min:1'],
'offset' => ['nullable', 'integer', 'min:0'],
];
}

/**
* Get the validator error messages.
*
* @return array
*/
public function messages(): array
{
return [
'limit.integer' => '请求数据量必须是整数',
'limit.min' => '请求数据量最少 1 条',
'offset.integer' => '请求的数据偏移必须是整数',
'offset.min' => '请求的数据偏移最少 0 条',
];
}
}
53 changes: 53 additions & 0 deletions app/API2/Resources/Feed/TopicParticipantCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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\Http\Resources\Json\ResourceCollection;

class TopicParticipantCollection extends ResourceCollection
{
/**
* The collection to array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
return $this
->collection
->map(function ($item) use ($request) {
return $this->renderItem($item, $request);
})
->all();
}

/**
* Render the collection item.
*
* @param mixed $item
* @return int
*/
public function renderItem($item): int
{
return $item->user_id;
}
}
16 changes: 15 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@
$api->get('{topic}', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@show');

/*
* List feeds on a topic.
* List feeds for a topic.
*
* @Get /api/v2/feed/topics/:topicID/feeds
* @Param::query('limit', 'integer', 'The data limit, default `15`.')
Expand All @@ -693,6 +693,20 @@
* </pre>')
*/
$api->get('{topic}/feeds', Zhiyi\Plus\API2\Controllers\Feed\TopicFeed::class);

/*
*
* List participants for a topic.
*
* @Get /api/v2/feed/topic/:topicID/participants
* @Param::query('limit', 'integer', 'The data limit, default `15`.')
* @Param::query('offset', 'integer', 'The data offset, default `0`.')
* @Response::header('Status', 200, 'OK')
* @Response::json('<pre>
* [2, 3, 4, 5]
* </pre>')
*/
$api->get('{topic}/participants', \Zhiyi\Plus\API2\Controllers\Feed\TopicParticipant::class.'@index');
});
});

Expand Down

0 comments on commit 179267a

Please sign in to comment.