Skip to content

Commit f4ecd94

Browse files
committed
feat(core): Add list all comments API
#337
1 parent 44972d3 commit f4ecd94

File tree

4 files changed

+225
-0
lines changed

4 files changed

+225
-0
lines changed

app/API2/Controllers/Comment.php

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* +----------------------------------------------------------------------+
7+
* | ThinkSNS Plus |
8+
* +----------------------------------------------------------------------+
9+
* | Copyright (c) 2018 Chengdu ZhiYiChuangXiang Technology Co., Ltd. |
10+
* +----------------------------------------------------------------------+
11+
* | This source file is subject to version 2.0 of the Apache license, |
12+
* | that is bundled with this package in the file LICENSE, and is |
13+
* | available through the world-wide-web at the following url: |
14+
* | http://www.apache.org/licenses/LICENSE-2.0.html |
15+
* +----------------------------------------------------------------------+
16+
* | Author: Slim Kit Group <master@zhiyicx.com> |
17+
* | Homepage: www.thinksns.com |
18+
* +----------------------------------------------------------------------+
19+
*/
20+
21+
namespace Zhiyi\Plus\API2\Controllers;
22+
23+
use Illuminate\Http\JsonResponse;
24+
use Zhiyi\Plus\API2\Requests\ListAllComments;
25+
use Zhiyi\Plus\Models\Comment as CommentModel;
26+
use Zhiyi\Plus\API2\Resources\Comment as CommentResource;
27+
28+
class Comment extends Controller
29+
{
30+
/**
31+
* List all comments
32+
* @param \Zhiyi\Plus\API2\Requests\ListAllComments $request
33+
* @param \Zhiyi\Plus\Models\Comment $model
34+
* @return \Illuminate\Http\JsonResponse
35+
*/
36+
public function index(ListAllComments $request, CommentModel $model): JsonResponse
37+
{
38+
// Skip non id where?
39+
$skipNonId = false;
40+
41+
// Get database query `id` direction.
42+
$direction = $request->query('direction', 'desc');
43+
44+
// Query comments.
45+
$comments = $model
46+
47+
// New a database query.
48+
->query()
49+
50+
// 如果存在 `id` 查询字段,进入查新条件构建
51+
// 如果是空字段,则继续,反之设置 `$skipNonId = true` 跳过后面的条件
52+
->when($id = $request->query('id'), function ($query) use ($id, &$skipNonId) {
53+
$id = array_values(array_filter(explode(',', $id)));
54+
if (! $id) {
55+
return $query;
56+
} elseif (count($id) === 1) {
57+
$skipNonId = true;
58+
59+
return $query->where('id', array_pop($id));
60+
}
61+
62+
$skipNonId = true;
63+
64+
return $query->whereIn('id', $id);
65+
})
66+
67+
// 如果传递了数据开始标记,进入查询构建,
68+
// 如果 `$skipNonId` 为 true 则跳过
69+
->when(($index = $request->query('index') && ! $skipNonId), function ($query) use ($index, $direction) {
70+
return $query->where('id', $direction === 'desc' ? '<' : '>', $index);
71+
})
72+
->when(($author = $request->query('author')) && ! $skipNonId, function ($query) use ($author) {
73+
return $query->where('user_id', $author);
74+
})
75+
->when(($forUser = $request->query('for_user')) && ! $skipNonId, function ($query) use ($forUser, $request) {
76+
$forType = $request->query('for_type', 'all');
77+
78+
if ($forType === 'target') {
79+
return $query->where('target_user', $forUser);
80+
} elseif ($forType === 'reply') {
81+
return $query->where('reply_user', $forUser);
82+
}
83+
84+
return $query->where(function ($query) use ($forUser) {
85+
return $query
86+
->where('target_user', $forUser)
87+
->orWhere('reply_user', $forUser);
88+
});
89+
})
90+
->when(($resourceableId = $request->query('resourceable_id')) && ! $skipNonId, function ($query) use ($resourceableId, $request) {
91+
$resourceableId = array_values(array_filter(explode(',', $resourceableId)));
92+
if (! $resourceableId) {
93+
return $query;
94+
}
95+
96+
return $query
97+
->where('commentable_type', $request->query('resourceable_type'))
98+
->whereIn('commentable_id', $resourceableId);
99+
})
100+
->orderBy('id', $direction)
101+
->get();
102+
103+
return CommentResource::collection($comments)
104+
->toResponse($request)
105+
->setStatusCode(JsonResponse::HTTP_OK);
106+
}
107+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zhiyi\Plus\API2\Requests;
6+
7+
class ListAllComments extends Request
8+
{
9+
/**
10+
* The validate rules.
11+
* @return array
12+
*/
13+
public function rules(): array
14+
{
15+
return [
16+
'limit' => 'nullable|integer|min:1|max:100',
17+
'index' => 'nullable|integer|min:0',
18+
'direction' => 'nullable|string|in:asc,desc',
19+
'author' => 'nullable|integer|min:0',
20+
'for_user' => 'nullable|integer|min:0',
21+
'for_type' => 'nullable|string|in:all,target,reply',
22+
'id' => 'nullable|string',
23+
'resourceable_id' => 'nullable|string',
24+
'resourceable_type' => 'required_with:resourceable_id|string',
25+
];
26+
}
27+
28+
/**
29+
* Get the validate error messages.
30+
* @return array
31+
*/
32+
public function messages(): array
33+
{
34+
return [
35+
'limit.integer' => 'limit 只能是正整数',
36+
'limit.min' => '本次请求最少要求获取一条数据',
37+
'limit.max' => '本次请求最多只能获取 100 条数据',
38+
'index.integer' => '位置标记只能是正整数',
39+
'index.min' => '位置标记不能为负数',
40+
'direction.in' => '数据排序方向只允许 `asc` 或者 `desc`',
41+
'author.integer' => '作者只允许用户 ID',
42+
'author.min' => '作者用户 ID 非法',
43+
'for_user.integer' => '接收用户 ID 非法',
44+
'for_user.min' => '接收用户 ID 非法',
45+
'for_type.in' => '接收用户数据类型错误',
46+
];
47+
}
48+
}

app/API2/Resources/Comment.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Zhiyi\Plus\API2\Resources;
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Http\Resources\Json\JsonResource;
9+
use Zhiyi\Plus\Utils\DateTimeToIso8601ZuluString;
10+
11+
class Comment extends JsonResource
12+
{
13+
use DateTimeToIso8601ZuluString;
14+
15+
/**
16+
* The resource to array.
17+
* @param \Illuminate\Http\Request $request
18+
* @return array
19+
*/
20+
public function toArray($request): array
21+
{
22+
return [
23+
'id' => $this->id,
24+
'user_id' => $this->user_id,
25+
'target_user' => $this->target_user,
26+
'reply_user' => $this->when($this->reply_user, $this->reply_user),
27+
'body' => $this->body,
28+
'resourceable' => [
29+
'type' => $this->commentable_type,
30+
'id' => $this->commentable_id,
31+
],
32+
'created_at' => $this->dateTimeToIso8601ZuluString(
33+
$this->{Model::CREATED_AT}
34+
),
35+
];
36+
}
37+
}

routes/api.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,4 +758,37 @@
758758
* </pre>')
759759
*/
760760
$api->get('user/message/atme', \Zhiyi\Plus\API2\Controllers\User\Message\At::class);
761+
762+
/*
763+
* List all comments
764+
*
765+
* @Get /api/v2/comments
766+
* @Param::query('limit', 'integer', 'The query data limit.')
767+
* @Param::query('index', 'integer', 'The query data start index.')
768+
* @Param::query('direction', 'enum:asc,desc', 'The data order by id direction.')
769+
* @Param::query('author', 'integer', 'The comment author user id')
770+
* @Param::query('for_user', 'integer', 'The comment target user id')
771+
* @Param::query('for_type', 'enum:all,target,reply', 'for user type')
772+
* @Param::query('id', 'string', 'Comment IDs, using `,` slicing')
773+
* @Param::query('resourceable_id', 'string', 'Resourceable IDs, using `,` slicing')
774+
* @Param::query('resourceable_type', 'string', 'Resourceabe type name')
775+
* @Response::header('Sttaus', 200, 'OK')
776+
* @Response::json('<pre>
777+
* [
778+
* {
779+
* "id": 1,
780+
* "user_id": 1,
781+
* "target_user": 2,
782+
* "reply_user": 3,
783+
* "body": "Hi, I love you.",
784+
* "resourceable": {
785+
* "type": "feeds",
786+
* "id": 1
787+
* },
788+
* "created_at": "2018-08-15T05:57:01Z"
789+
* }
790+
* ]
791+
* </pre>')
792+
*/
793+
$api->get('comments', \Zhiyi\Plus\API2\Controllers\Comment::class.'@index');
761794
});

0 commit comments

Comments
 (0)