修改话题

This commit is contained in:
fthvgb1 2018-06-10 18:35:53 +08:00
parent ef1a29cd40
commit 66a176b592
4 changed files with 45 additions and 7 deletions

View File

@ -15,4 +15,17 @@ class TopicsController extends Controller
$topic->save();
return $this->response->item($topic, new TopicTransformer())->setStatusCode(201);
}
/**
* @param TopicRequest $request
* @param Topic $topic
* @return \Dingo\Api\Http\Response
* @throws \Illuminate\Auth\Access\AuthorizationException
*/
public function update(TopicRequest $request, Topic $topic)
{
$this->authorize('update', $topic);
$topic->update($request->all());
return $this->response->item($topic, new TopicTransformer());
}
}

View File

@ -25,11 +25,24 @@ class TopicRequest extends FormRequest
public function rules()
{
return [
'title' => 'required|string',
'body' => 'required|string',
'category_id' => 'required|exists:categories,id',
];
switch ($this->method()) {
case 'POSt':
return [
'title' => 'required|string',
'body' => 'required|string',
'category_id' => 'required|exists:categories,id',
];
case 'PATCH':
return [
'title' => 'string',
'body' => 'string',
'category' => 'exists:categories,id'
];
default:
return [];
}
}
public function attributes()

View File

@ -9,6 +9,9 @@ use App\Observers\LinkObserver;
use App\Observers\ReplyObserver;
use App\Observers\TopicObserver;
use Carbon\Carbon;
use Dingo\Api\Facade\API;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -39,5 +42,12 @@ class AppServiceProvider extends ServiceProvider
if (app()->isLocal()) {
$this->app->register(\VIACreative\SudoSu\ServiceProvider::class);
}
API::error(function (ModelNotFoundException $exception) {
abort(404);
});
API::error(function (AuthorizationException $exception) {
abort(403, $exception->getMessage());
});
}
}

View File

@ -15,7 +15,7 @@ $api = app('Dingo\Api\Routing\Router');
$api->version('v1', [
'namespace' => 'App\Http\Controllers\Api',
'middleware' => 'serializer:array',
'middleware' => ['serializer:array', 'bindings'],
], function ($api) {
$api->group([
'middleware' => 'api.throttle',
@ -54,7 +54,7 @@ $api->version('v1', [
});
// 需要 token 验证的接口
$api->group(['middleware' => 'api.auth'], function ($api) {
$api->group(['middleware' => 'api.auth',], function ($api) {
// 当前登录用户信息
$api->get('user', 'UsersController@me')
->name('api.user.show');
@ -66,6 +66,8 @@ $api->version('v1', [
->name('api.user.update');
//发布话题
$api->post('topics', 'TopicsController@store')->name('api.topics.store');
//发布话题
$api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update');
});
});