Skip to content

Commit

Permalink
feat(feed): Add a follow a topic API
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 24, 2018
1 parent b70f212 commit 2d504e6
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 0 deletions.
83 changes: 83 additions & 0 deletions app/API2/Controllers/Feed/TopicFollow.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?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\Request;
use Illuminate\Http\Response;
use Zhiyi\Plus\API2\Controllers\Controller;
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class TopicFollow extends Controller
{
/**
* Create the controller instance.
*/
public function __construct()
{
$this->middleware('auth:api');
}

/**
* Follow a topic.
*
* @param \Illuminate\Http\Request $request
* @param \Zhiyi\Plus\Models\FeedTopic $model
* @param int $topicID
* @return \Illuminate\Http\Response
*/
public function follow(Request $request, FeedTopicModel $model, int $topicID): Response
{
// Featch the request authentication user model.
$user = $request->user();

// Database query topic.
$topic = $model
->query()
->where('id', $topicID)
->first();

// If the topic Non-existent, throw a not found exception.
if (!$topic) {
throw new NotFoundHttpException('关注的话题不存在');
}

// Create success 204 response
$response = (new Response())->setStatusCode(Response::HTTP_NO_CONTENT /* 204 */);

// Database query the authentication user followed.
$exists = $topic
->followers()
->where('user_id', $user->id)
->exists();
if ($exists) {
return $response;
}

return $user->getConnection()->transaction(function () use ($user, $topic, $response): Response {
$topic->followers()->attach($user);
$topic->followers_count += 1;
$topic->save();

return $response;
});
}
}
8 changes: 8 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -660,4 +660,12 @@
$api->post('', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@create');
});
});

/*
* Follow a topic.
*
* @Put /api/v2/user/feed-topics/:topicID
* @Response::header('Status', 204, 'No Content')
*/
$api->put('user/feed-topics/{topicID}', \Zhiyi\Plus\API2\Controllers\Feed\TopicFollow::class.'@follow');
});

0 comments on commit 2d504e6

Please sign in to comment.