|
| 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 | +} |
0 commit comments