Skip to content

Commit

Permalink
feat(feed): 添加一个创建动态话题接口
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 24, 2018
1 parent 16c0fe0 commit f2eca06
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 4 deletions.
89 changes: 89 additions & 0 deletions app/API2/Controllers/Feed/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,28 @@
use Illuminate\Http\JsonResponse;
use Illuminate\Database\Eloquent\Model;
use Zhiyi\Plus\API2\Controllers\Controller;
use Zhiyi\Plus\Types\Models as ModelsTypes;
use Symfony\Component\HttpFoundation\Response;
use Zhiyi\Plus\Models\FileWith as FileWithModel;
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel;
use Zhiyi\Plus\API2\Resources\Feed\TopicCollection;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Zhiyi\Plus\API2\Requests\Feed\TopicIndex as IndexRequest;
use Zhiyi\Plus\API2\Requests\Feed\CreateTopic as CreateTopicRequest;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

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

/**
* List topics.
*
Expand Down Expand Up @@ -83,4 +97,79 @@ public function index(IndexRequest $request, FeedTopicModel $model): JsonRespons

return $response;
}

/**
* Create an topic.
*
* @param \Zhiyi\Plus\API2\Requests\Feed\CreateTopic $request
* @param \Zhiyi\Plus\Types\Models $types
* @return \Illuminate\Http\JsonResponse
*/
public function create(CreateTopicRequest $request, ModelsTypes $types): JsonResponse
{
// Create feed topic module
$topic = new FeedTopicModel;
foreach($request->only(['name', 'logo', 'desc']) as $key => $value) {
$topic->{$key} = $value;
}

// If logo exists, inspect file with ID to be used.
$with = null;
if ($topic->logo) {
$with = (new FileWithModel)
->query()
->where('id', $topic->logo)
->first();
if ($with->channel || $with->raw) {
throw new UnprocessableEntityHttpException('Logo 文件不合法');
}
}

// Database query `name` used
$exists = $topic
->query()
->where('name', $topic->name)
->exists();
if ($exists) {
throw new UnprocessableEntityHttpException(sprintf('“%s”话题已存在', $topic->name));
}

// Fetch the authentication user model.
$user = $request->user();

// Open a database transaction,
// database commit success return the topic model.
$topic = $user->getConnection()->transaction(function () use ($user, $topic, $with, $types) {
// Set topic creator user ID and
// init default followers count.
$topic->creator_user_id = $user->id;
$topic->followers_count = 1;
$topic->save();

// Attach the creator user follow the topic.
$topic->followers()->attach($user);

// If the FileWith instance of `FileWithModel`,
// set topic class alias to `channel`, set the
// topic `id` to `raw` column.
// Reset FileWith owner for the authenticated auth.
if ($with instanceof FileWithModel) {
$with->channel = $types->get(ModelsTypes::KEY_BY_CLASSNAME, FeedTopicModel::class);
$with->raw = $topic->id;
$with->user_id = $user->id;
$with->save();
}

return $topic;
});

// Headers:
// Status: 201 Created
// Body:
// { "id": $topid->id }
return new JsonResponse(
['id' => $topic->id],
Response::HTTP_CREATED /* 201 */
);
}
}
56 changes: 56 additions & 0 deletions app/API2/Requests/Feed/CreateTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?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 CreateTopic extends Request
{
/**
* Get the validator rules.
*
* @return array
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:100'],
'desc' => ['nullable', 'string', 'max:500'],
'logo' => ['nullable', 'integer', 'min:1'],
];
}

/**
* Get the validator error messages.
*
* @return array
*/
public function messages(): array
{
return [
'name.required' => '请输入话题名称',
'name.max' => '话题名称请控制在 100 字以内',
'desc.max' => '话题描述请控制在 500 字以内',
'logo.integer' => '话题 Logo 数据非法',
'logo.min' => '话题 Logo 文件 ID 非法',
];
}
}
15 changes: 15 additions & 0 deletions app/Models/FeedTopic.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,26 @@
namespace Zhiyi\Plus\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;

class FeedTopic extends Model
{
/**
* The model table name.
*/
protected $table = 'feed_topics';

/**
* Topic belongs to many relation.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function followers(): BelongsToMany
{
$table = (new FeedTopicFollower)->getTable();
return $this
->belongsToMany(User::class, $table, 'topic_id', 'user_id')
->withPivot('index', Model::CREATED_AT)
->using(FeedTopicFollower::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function up()
$table->integer('logo')->unsigned()->nullable()->default(0)->comment('The topic logo, file with id');
$table->text('desc')->nullable()->comment('The topic desc');
$table->integer('creator_user_id')->unsigned()->comment('The topic creator user ID');
$table->integer('feeds_count')->unsigned()->nullable()->default(0)->comment('The topic link feeds count');
$table->integer('followers_count')->unsigned()->nullable()->default(0)->comment('The topic followers count');
$table->timestamps();

$table->unique('name');
Expand Down
21 changes: 21 additions & 0 deletions resources/lang/zh-CN/messages.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/*
* +----------------------------------------------------------------------+
* | 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 |
* +----------------------------------------------------------------------+
*/

return [
'Created' => '创建成功',
];
23 changes: 19 additions & 4 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,18 +631,33 @@
* @Param::query {limit} Featch data limit.
* @Param::query {index} Featch data start index.
* @Param::query {direction} Can be one of `asc` or `desc`.
* @Response::headers('Status', 200, 'OK')
* @Response::json
* <pre>
* @Response::header('Status', 200, 'OK')
* @Response::json('<pre>
* [{
* "id": 1, // Topic ID
* "name": "Plus", // Topic name
* "logo": 2, // Topic logo, file with ID
* "created_at": "2018-07-23T15:04:23Z" // Topic created datetime
* }]
* </pre>
* </pre>')
*/
$api->get('', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@index');

/*
* Create an topic
*
* @Post /api/v2/feed/topics
* @Param::input('name', 'string', 'The name of the topic.')
* @Param::input('desc', 'string', 'The desc of the topic.')
* @Param::input('logo', 'integer', 'The topic logo file with ID.')
* @Response::header('Status', 201, 'Created')
* @Response::json('<pre>
* {
* "id": 2 // Created topic id
* }
* </pre>')
*/
$api->post('', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@create');
});
});
});

0 comments on commit f2eca06

Please sign in to comment.