话题列表
This commit is contained in:
parent
66a176b592
commit
c63c71fc39
@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Requests\Api\TopicRequest;
|
||||
use App\Models\Topic;
|
||||
use App\Models\User;
|
||||
use App\Transformers\TopicTransformer;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class TopicsController extends Controller
|
||||
{
|
||||
@ -28,4 +30,35 @@ class TopicsController extends Controller
|
||||
$topic->update($request->all());
|
||||
return $this->response->item($topic, new TopicTransformer());
|
||||
}
|
||||
|
||||
public function index(Request $request)
|
||||
{
|
||||
$query = Topic::query();
|
||||
if ($category_id = $request->get('category_id', 0)) {
|
||||
$query->where(['category_id', $category_id]);
|
||||
}
|
||||
$order = $request->get('order', 'recent');
|
||||
$query->withOrder($query, $order);
|
||||
$topics = $query->paginate(20);
|
||||
return $this->response->paginator($topics, new TopicTransformer());
|
||||
}
|
||||
|
||||
public function userIndex(User $user)
|
||||
{
|
||||
$topics = $user->topics()->recent()->paginate(20);
|
||||
return $this->response->paginator($topics, new TopicTransformer());
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Topic $topic
|
||||
* @return \Dingo\Api\Http\Response
|
||||
* @throws \Illuminate\Auth\Access\AuthorizationException
|
||||
* @throws \Throwable
|
||||
*/
|
||||
public function destroy(Topic $topic)
|
||||
{
|
||||
$this->authorize('update', $topic);
|
||||
$topic->delete();
|
||||
return $this->response->noContent();
|
||||
}
|
||||
}
|
||||
|
@ -16,6 +16,8 @@ namespace App\Models;
|
||||
* @property string excerpt
|
||||
* @property string created_at
|
||||
* @property string updated_at
|
||||
* @property User user
|
||||
* @property Category category
|
||||
* @package App\Models
|
||||
*/
|
||||
class Topic extends Model
|
||||
|
@ -15,6 +15,9 @@ use League\Fractal\TransformerAbstract;
|
||||
|
||||
class TopicTransformer extends TransformerAbstract
|
||||
{
|
||||
|
||||
protected $availableIncludes = ['user', 'category'];
|
||||
|
||||
public function transform(Topic $topic)
|
||||
{
|
||||
return [
|
||||
@ -32,4 +35,14 @@ class TopicTransformer extends TransformerAbstract
|
||||
'updated_at' => $topic->updated_at instanceof Carbon ? $topic->updated_at->toDateTimeString() : $topic->updated_at,
|
||||
];
|
||||
}
|
||||
|
||||
public function includeCategory(Topic $topic)
|
||||
{
|
||||
return $this->item($topic->category, new CategoryTransformer());
|
||||
}
|
||||
|
||||
public function includeUser(Topic $topic)
|
||||
{
|
||||
return $this->item($topic->user, new UserTransformer());
|
||||
}
|
||||
}
|
@ -38,6 +38,7 @@
|
||||
"filp/whoops": "~2.0",
|
||||
"fzaninotto/faker": "~1.4",
|
||||
"mockery/mockery": "~1.0",
|
||||
"overtrue/laravel-query-logger": "^1.0",
|
||||
"phpunit/phpunit": "~6.0",
|
||||
"summerblue/generator": "~0.5"
|
||||
},
|
||||
|
45
composer.lock
generated
45
composer.lock
generated
@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "3e238e73ff229ba2ec8024b3e85640a5",
|
||||
"content-hash": "32f207e4a0d2ec61b4710f12c6b34d6d",
|
||||
"packages": [
|
||||
{
|
||||
"name": "cakephp/chronos",
|
||||
@ -5340,6 +5340,49 @@
|
||||
"time": "2018-05-29T17:25:09+00:00"
|
||||
},
|
||||
{
|
||||
"name": "overtrue/laravel-query-logger",
|
||||
"version": "1.0.1",
|
||||
"source": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/overtrue/laravel-query-logger.git",
|
||||
"reference": "af7269a410ebc568bfd88fe807c062bb0f06c737"
|
||||
},
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://api.github.com/repos/overtrue/laravel-query-logger/zipball/af7269a410ebc568bfd88fe807c062bb0f06c737",
|
||||
"reference": "af7269a410ebc568bfd88fe807c062bb0f06c737",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"laravel/framework": "5.*"
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"laravel": {
|
||||
"providers": [
|
||||
"Overtrue\\LaravelQueryLogger\\ServiceProvider"
|
||||
]
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Overtrue\\LaravelQueryLogger\\": "src"
|
||||
}
|
||||
},
|
||||
"notification-url": "https://packagist.org/downloads/",
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"authors": [
|
||||
{
|
||||
"name": "overtrue",
|
||||
"email": "anzhengchao@gmail.com"
|
||||
}
|
||||
],
|
||||
"description": "A dev tool to log all queries for laravel application.",
|
||||
"time": "2017-10-11T10:02:01+00:00"
|
||||
},
|
||||
{
|
||||
"name": "phar-io/manifest",
|
||||
"version": "1.0.1",
|
||||
"source": {
|
||||
|
@ -15,7 +15,7 @@ $api = app('Dingo\Api\Routing\Router');
|
||||
|
||||
$api->version('v1', [
|
||||
'namespace' => 'App\Http\Controllers\Api',
|
||||
'middleware' => ['serializer:array', 'bindings'],
|
||||
'middleware' => ['serializer:array', 'bindings'],//注意里的bindings中间件,使用这个才能使用隐式路由绑定
|
||||
], function ($api) {
|
||||
$api->group([
|
||||
'middleware' => 'api.throttle',
|
||||
@ -51,6 +51,10 @@ $api->version('v1', [
|
||||
// 游客可以访问的接口
|
||||
$api->get('categories', 'CategoriesController@index')
|
||||
->name('api.categories.index');
|
||||
//话题列表
|
||||
$api->get('topics', 'TopicsController@index')->name('api.topics.index');
|
||||
//某用户的话题列表
|
||||
$api->get('users/{user}/topics', 'TopicsController@userIndex')->name('api.users.topics.index');
|
||||
});
|
||||
|
||||
// 需要 token 验证的接口
|
||||
@ -68,6 +72,9 @@ $api->version('v1', [
|
||||
$api->post('topics', 'TopicsController@store')->name('api.topics.store');
|
||||
//发布话题
|
||||
$api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update');
|
||||
//删除话题
|
||||
$api->delete('topics/{topic}', 'TopicsController@destroy')->name('api.topics.destroy');
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
@ -78,6 +85,4 @@ $api->version('v1', [
|
||||
});
|
||||
});*/
|
||||
|
||||
/*Route::middleware('auth:api')->get('/user', function (Request $request) {
|
||||
return $request->user();
|
||||
});*/
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user