修改话题

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(); $topic->save();
return $this->response->item($topic, new TopicTransformer())->setStatusCode(201); 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() public function rules()
{ {
switch ($this->method()) {
case 'POSt':
return [ return [
'title' => 'required|string', 'title' => 'required|string',
'body' => 'required|string', 'body' => 'required|string',
'category_id' => 'required|exists:categories,id', 'category_id' => 'required|exists:categories,id',
]; ];
case 'PATCH':
return [
'title' => 'string',
'body' => 'string',
'category' => 'exists:categories,id'
];
default:
return [];
}
} }
public function attributes() public function attributes()

View File

@ -9,6 +9,9 @@ use App\Observers\LinkObserver;
use App\Observers\ReplyObserver; use App\Observers\ReplyObserver;
use App\Observers\TopicObserver; use App\Observers\TopicObserver;
use Carbon\Carbon; use Carbon\Carbon;
use Dingo\Api\Facade\API;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@ -39,5 +42,12 @@ class AppServiceProvider extends ServiceProvider
if (app()->isLocal()) { if (app()->isLocal()) {
$this->app->register(\VIACreative\SudoSu\ServiceProvider::class); $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', [ $api->version('v1', [
'namespace' => 'App\Http\Controllers\Api', 'namespace' => 'App\Http\Controllers\Api',
'middleware' => 'serializer:array', 'middleware' => ['serializer:array', 'bindings'],
], function ($api) { ], function ($api) {
$api->group([ $api->group([
'middleware' => 'api.throttle', 'middleware' => 'api.throttle',
@ -54,7 +54,7 @@ $api->version('v1', [
}); });
// 需要 token 验证的接口 // 需要 token 验证的接口
$api->group(['middleware' => 'api.auth'], function ($api) { $api->group(['middleware' => 'api.auth',], function ($api) {
// 当前登录用户信息 // 当前登录用户信息
$api->get('user', 'UsersController@me') $api->get('user', 'UsersController@me')
->name('api.user.show'); ->name('api.user.show');
@ -66,6 +66,8 @@ $api->version('v1', [
->name('api.user.update'); ->name('api.user.update');
//发布话题 //发布话题
$api->post('topics', 'TopicsController@store')->name('api.topics.store'); $api->post('topics', 'TopicsController@store')->name('api.topics.store');
//发布话题
$api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update');
}); });
}); });