话题列表

This commit is contained in:
fthvgb1 2018-06-10 19:28:01 +08:00
parent 66a176b592
commit c63c71fc39
6 changed files with 102 additions and 5 deletions

View File

@ -4,7 +4,9 @@ namespace App\Http\Controllers\Api;
use App\Http\Requests\Api\TopicRequest; use App\Http\Requests\Api\TopicRequest;
use App\Models\Topic; use App\Models\Topic;
use App\Models\User;
use App\Transformers\TopicTransformer; use App\Transformers\TopicTransformer;
use Illuminate\Http\Request;
class TopicsController extends Controller class TopicsController extends Controller
{ {
@ -28,4 +30,35 @@ class TopicsController extends Controller
$topic->update($request->all()); $topic->update($request->all());
return $this->response->item($topic, new TopicTransformer()); 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();
}
} }

View File

@ -16,6 +16,8 @@ namespace App\Models;
* @property string excerpt * @property string excerpt
* @property string created_at * @property string created_at
* @property string updated_at * @property string updated_at
* @property User user
* @property Category category
* @package App\Models * @package App\Models
*/ */
class Topic extends Model class Topic extends Model

View File

@ -15,6 +15,9 @@ use League\Fractal\TransformerAbstract;
class TopicTransformer extends TransformerAbstract class TopicTransformer extends TransformerAbstract
{ {
protected $availableIncludes = ['user', 'category'];
public function transform(Topic $topic) public function transform(Topic $topic)
{ {
return [ return [
@ -32,4 +35,14 @@ class TopicTransformer extends TransformerAbstract
'updated_at' => $topic->updated_at instanceof Carbon ? $topic->updated_at->toDateTimeString() : $topic->updated_at, '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());
}
} }

View File

@ -38,6 +38,7 @@
"filp/whoops": "~2.0", "filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4", "fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0", "mockery/mockery": "~1.0",
"overtrue/laravel-query-logger": "^1.0",
"phpunit/phpunit": "~6.0", "phpunit/phpunit": "~6.0",
"summerblue/generator": "~0.5" "summerblue/generator": "~0.5"
}, },

45
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"content-hash": "3e238e73ff229ba2ec8024b3e85640a5", "content-hash": "32f207e4a0d2ec61b4710f12c6b34d6d",
"packages": [ "packages": [
{ {
"name": "cakephp/chronos", "name": "cakephp/chronos",
@ -5339,6 +5339,49 @@
], ],
"time": "2018-05-29T17:25:09+00:00" "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", "name": "phar-io/manifest",
"version": "1.0.1", "version": "1.0.1",

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', 'bindings'], 'middleware' => ['serializer:array', 'bindings'],//注意里的bindings中间件使用这个才能使用隐式路由绑定
], function ($api) { ], function ($api) {
$api->group([ $api->group([
'middleware' => 'api.throttle', 'middleware' => 'api.throttle',
@ -51,6 +51,10 @@ $api->version('v1', [
// 游客可以访问的接口 // 游客可以访问的接口
$api->get('categories', 'CategoriesController@index') $api->get('categories', 'CategoriesController@index')
->name('api.categories.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 验证的接口 // 需要 token 验证的接口
@ -68,6 +72,9 @@ $api->version('v1', [
$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'); $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();
});*/