From ef1a29cd40591c790acc480f4f3df42ee5b6834b Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sun, 10 Jun 2018 18:04:03 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=83=E8=AF=9D=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/CategoriesController.php | 14 ++++++ app/Http/Controllers/Api/TopicsController.php | 18 ++++++++ app/Http/Requests/Api/TopicRequest.php | 43 +++++++++++++++++++ app/Models/Category.php | 7 +++ app/Models/Topic.php | 16 +++++++ app/Transformers/CategoryTransformer.php | 26 +++++++++++ app/Transformers/TopicTransformer.php | 35 +++++++++++++++ routes/api.php | 6 +++ 8 files changed, 165 insertions(+) create mode 100644 app/Http/Controllers/Api/CategoriesController.php create mode 100644 app/Http/Controllers/Api/TopicsController.php create mode 100644 app/Http/Requests/Api/TopicRequest.php create mode 100644 app/Transformers/CategoryTransformer.php create mode 100644 app/Transformers/TopicTransformer.php diff --git a/app/Http/Controllers/Api/CategoriesController.php b/app/Http/Controllers/Api/CategoriesController.php new file mode 100644 index 0000000..6b3b62e --- /dev/null +++ b/app/Http/Controllers/Api/CategoriesController.php @@ -0,0 +1,14 @@ +response->collection(Category::all(), new CategoryTransformer()); + } +} diff --git a/app/Http/Controllers/Api/TopicsController.php b/app/Http/Controllers/Api/TopicsController.php new file mode 100644 index 0000000..a8d49a5 --- /dev/null +++ b/app/Http/Controllers/Api/TopicsController.php @@ -0,0 +1,18 @@ +fill($request->all()); + $topic->user_id = $this->user()->id; + $topic->save(); + return $this->response->item($topic, new TopicTransformer())->setStatusCode(201); + } +} diff --git a/app/Http/Requests/Api/TopicRequest.php b/app/Http/Requests/Api/TopicRequest.php new file mode 100644 index 0000000..8345782 --- /dev/null +++ b/app/Http/Requests/Api/TopicRequest.php @@ -0,0 +1,43 @@ + 'required|string', + 'body' => 'required|string', + 'category_id' => 'required|exists:categories,id', + ]; + } + + public function attributes() + { + return [ + 'title' => '标题', + 'body' => '话题内容', + 'category_id' => '分类', + ]; + } +} diff --git a/app/Models/Category.php b/app/Models/Category.php index d3a4c95..1caaf90 100644 --- a/app/Models/Category.php +++ b/app/Models/Category.php @@ -2,6 +2,13 @@ namespace App\Models; +/** + * Class Category + * @property int $id + * @property string $name + * @property string $description + * @package App\Models + */ class Category extends Model { protected $guarded = ['id']; diff --git a/app/Models/Topic.php b/app/Models/Topic.php index 8be0669..61d3778 100644 --- a/app/Models/Topic.php +++ b/app/Models/Topic.php @@ -2,6 +2,22 @@ namespace App\Models; +/** + * Class Topic + * @property int id + * @property string title + * @property int category_id + * @property int user_id + * @property int reply_count + * @property int view_count + * @property int last_reply_user_id + * @property string body + * @property string slug + * @property string excerpt + * @property string created_at + * @property string updated_at + * @package App\Models + */ class Topic extends Model { protected $fillable = ['title', 'category_id', 'body', 'excerpt', 'slug']; diff --git a/app/Transformers/CategoryTransformer.php b/app/Transformers/CategoryTransformer.php new file mode 100644 index 0000000..f5f4330 --- /dev/null +++ b/app/Transformers/CategoryTransformer.php @@ -0,0 +1,26 @@ + $category->id, + 'name' => $category->name, + 'description' => $category->description, + ]; + } + +} \ No newline at end of file diff --git a/app/Transformers/TopicTransformer.php b/app/Transformers/TopicTransformer.php new file mode 100644 index 0000000..7c69480 --- /dev/null +++ b/app/Transformers/TopicTransformer.php @@ -0,0 +1,35 @@ + $topic->id, + 'title' => $topic->title, + 'body' => $topic->body, + 'user_id' => (int)$topic->user_id, + 'category_id' => (int)$topic->category_id, + 'reply_count' => (int)$topic->reply_count, + 'view_count' => (int)$topic->view_count, + 'last_reply_user_id' => (int)$topic->last_reply_user_id, + 'excerpt' => $topic->excerpt, + 'slug' => $topic->slug, + 'created_at' => $topic->created_at instanceof Carbon ? $topic->created_at->toDateTimeString() : $topic->created_at, + 'updated_at' => $topic->updated_at instanceof Carbon ? $topic->updated_at->toDateTimeString() : $topic->updated_at, + ]; + } +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 06d9582..27006ac 100644 --- a/routes/api.php +++ b/routes/api.php @@ -47,6 +47,10 @@ $api->version('v1', [ // 删除token $api->delete('authorizations/current', 'AuthorizationsController@destroy') ->name('api.authorizations.destroy'); + + // 游客可以访问的接口 + $api->get('categories', 'CategoriesController@index') + ->name('api.categories.index'); }); // 需要 token 验证的接口 @@ -60,6 +64,8 @@ $api->version('v1', [ // 编辑登录用户信息 $api->patch('user', 'UsersController@update') ->name('api.user.update'); + //发布话题 + $api->post('topics', 'TopicsController@store')->name('api.topics.store'); }); });