-
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 list participants for a topic API
- Loading branch information
Showing
5 changed files
with
177 additions
and
2 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
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,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 */); | ||
} | ||
} |
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,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 条', | ||
]; | ||
} | ||
} |
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,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; | ||
} | ||
} |
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