Skip to content

Commit

Permalink
feat(feed): ( issue #324 ) Add a get a single topic API
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 25, 2018
1 parent f36c6e2 commit 565b606
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 2 deletions.
18 changes: 16 additions & 2 deletions app/API2/Controllers/Feed/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@
use Zhiyi\Plus\Types\Models as ModelsTypes;
use Zhiyi\Plus\Models\FileWith as FileWithModel;
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel;
use Zhiyi\Plus\API2\Resources\Feed\TopicCollection;
use Zhiyi\Plus\API2\Resources\Feed\Topic as TopicResource;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Zhiyi\Plus\API2\Requests\Feed\TopicIndex as IndexRequest;
use Zhiyi\Plus\API2\Requests\Feed\EditTopic as EditTopicRequest;
use Zhiyi\Plus\API2\Requests\Feed\CreateTopic as CreateTopicRequest;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;
use Zhiyi\Plus\API2\Resources\Feed\TopicCollection as TopicCollectionResource;

class Topic extends Controller
{
Expand Down Expand Up @@ -98,7 +99,7 @@ public function index(IndexRequest $request, FeedTopicModel $model): JsonRespons
->get();

// Create the action response.
$response = (new TopicCollection($result))
$response = (new TopicCollectionResource($result))
->response()
->setStatusCode(Response::HTTP_OK /* 200 */);

Expand Down Expand Up @@ -228,4 +229,17 @@ public function update(EditTopicRequest $request, ModelsTypes $types, FeedTopicM
return $response;
});
}

/**
* Get a single topic.
*
* @param \Zhiyi\Plus\Models\FeedTopic $topic
* @return \Illuminate\Http\JsonResponse
*/
public function show(FeedTopicModel $topic): JsonResponse
{
return (new TopicResource($topic))
->response()
->setStatusCode(Response::HTTP_OK /* 200 */);
}
}
50 changes: 50 additions & 0 deletions app/API2/Resources/Feed/Topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?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\JsonResource;

class Topic extends JsonResource
{
/**
* The topic resource to array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request): array
{
$user = $request->user('api');

return [
'id' => $this->id,
'name' => $this->name,
'logo' => $this->when($this->logo, $this->logo),
'desc' => $this->when($this->desc, $this->desc),
'creator_user_id' => $this->creator_user_id,
'feeds_count' => $this->feeds_count,
'followers_count' => $this->followers_count,
'has_followed' => $this->when($user, function () use ($user) {
return $this->followers()->where('user_id', $user->id)->exists();
}),
];
}
}
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,14 @@
* @Response::header('Status', 204, 'No Content')
*/
$api->patch('{topic}', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@update');

/*
* Get a single topic.
*
* @Get /api/v2/feed/topics/:topicID
* @Response::header('Status', 200, 'OK')
*/
$api->get('{topic}', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@show');
});
});

Expand Down

0 comments on commit 565b606

Please sign in to comment.