Skip to content

Commit

Permalink
feat(feed): ( issue #324 ) Add a edit an topic API
Browse files Browse the repository at this point in the history
  • Loading branch information
medz committed Jul 25, 2018
1 parent 7ae7ebd commit 2f9ccda
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 4 deletions.
60 changes: 57 additions & 3 deletions app/API2/Controllers/Feed/Topic.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,18 @@

namespace Zhiyi\Plus\API2\Controllers\Feed;

use Illuminate\Http\Response;
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\EditTopic as EditTopicRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Zhiyi\Plus\API2\Requests\Feed\CreateTopic as CreateTopicRequest;
use Symfony\Component\HttpKernel\Exception\UnprocessableEntityHttpException;

Expand All @@ -43,12 +45,12 @@ public function __construct()
// Add Auth(api) middleware.
$this
->middleware('auth:api')
->only(['create']);
->only(['create', 'update']);

// Add DisposeSensitive middleware.
$this
->middleware('sensitive:name,desc')
->only(['create']);
->only(['create', 'update']);
}

/**
Expand Down Expand Up @@ -178,4 +180,56 @@ public function create(CreateTopicRequest $request, ModelsTypes $types): JsonRes
Response::HTTP_CREATED /* 201 */
);
}

/**
* Edit an topic.
*
* @param \Zhiyi\Plus\API2\Requests\Feed\EditTopic $request
* @param \Zhiyi\Plus\Models\FeedTopic $topic
* @return \Illuminate\Http\Response
*/
public function update(EditTopicRequest $request, FeedTopicModel $topic): Response
{
$this->authorize('update', $topic);

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

// If `logo` and `desc` field all is NULL
$with = null;
if (! ($logo = (int) $request->input('logo')) && ! ($desc = $request->input('desc'))) {
return $response;
} else if ($logo && $logo !== $topic->logo) {
$with = (new FileWithModel)
->query()
->where('id', $logo)
->first();
if ($with->channel || $with->raw) {
throw new UnprocessableEntityHttpException('Logo 文件不合法');
}

$with->user_id = $request->user()->id;
}

$topic->desc = $desc ?: $topic->desc;

return $topic->getConnection()->transaction(function () use ($response, $topic, $with): Response {
if ($with instanceof FileWithModel) {
if ($topic->logo) {
$with->query()->where('id', $topic->logo)->delete();
}

$with->channel = $types->get(FeedTopicModel::class, ModelsTypes::KEY_BY_CLASSNAME);
$with->raw = $topic->id;
$with->save();

// Set file with ID to topic logo.
$topic->logo = $with->id;
}

$topic->save();

return $response;
});
}
}
53 changes: 53 additions & 0 deletions app/API2/Requests/Feed/EditTopic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?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 EditTopic extends Request
{
/**
* Get the validator rules.
*
* @return array
*/
public function rules(): array
{
return [
'logo' => ['nullable', 'integer', 'min:1'],
'desc' => ['nullable', 'string', 'max:500'],
];
}

/**
* Get the validator error messages.
*
* @return array
*/
public function messages(): array
{
return [
'desc.max' => '话题描述请控制在 500 字以内',
'logo.integer' => '话题 Logo 数据非法',
'logo.min' => '话题 Logo 文件 ID 非法',
];
}
}
39 changes: 39 additions & 0 deletions app/Policies/Feed/Topic.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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\Policies\Feed;

use Zhiyi\Plus\Models\User as UserModel;
use Zhiyi\Plus\Models\FeedTopic as FeedTopicModel;

class Topic
{
/**
* Check the topic can be operated by the user.
*
* @param \Zhiyi\Plus\Models\User $user
* @param \Zhiyi\Plus\Models\FeedTopic $topic
* @return bool
*/
public function update(UserModel $user, FeedTopicModel $topic): bool
{
return $user->id === $topic->creator_user_id;
}
}
2 changes: 1 addition & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class AuthServiceProvider extends ServiceProvider
* @var array
*/
protected $policies = [
'Zhiyi\Plus\Model' => 'Zhiyi\Plus\Policies\ModelPolicy',
\Zhiyi\Plus\Models\FeedTopic::class => \Zhiyi\Plus\Policies\Feed\Topic::class
];

/**
Expand Down
10 changes: 10 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,16 @@
* </pre>')
*/
$api->post('', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@create');

/*
* Edit an topic.
*
* @Patch /api/v2/feed/topics/:topicID
* @Param::input('desc', 'string', 'The desc of the topic')
* @Param::input('logo', 'integer', 'The topic logo file with ID')
* @Response::header('Status', 204, 'No Content')
*/
$api->patch('{topic}', \Zhiyi\Plus\API2\Controllers\Feed\Topic::class.'@update');
});
});

Expand Down

0 comments on commit 2f9ccda

Please sign in to comment.