From 200fbdfc0a77bdec7cedf9f77ad6087907f2c33c Mon Sep 17 00:00:00 2001 From: fthvgb1 Date: Sun, 10 Jun 2018 22:37:37 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9B=9E=E5=A4=8Dcrd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Controllers/Api/RepliesController.php | 50 +++++++++++++++++++ .../Requests/Api/AuthorizationRequest.php | 1 - app/Http/Requests/Api/CaptchaRequest.php | 10 ---- app/Http/Requests/Api/FormRequest.php | 30 +++++++++++ app/Http/Requests/Api/ImageRequest.php | 10 ---- app/Http/Requests/Api/ReplyRequest.php | 33 ++++++++++++ .../Api/SocialAuthorizationRequest.php | 11 ---- app/Http/Requests/Api/TopicRequest.php | 10 ---- .../Requests/Api/VerificationCodeRequest.php | 10 ---- app/Models/Reply.php | 11 ++++ app/Transformers/ReplyTransformer.php | 36 +++++++++++++ routes/api.php | 12 ++++- 12 files changed, 171 insertions(+), 53 deletions(-) create mode 100644 app/Http/Controllers/Api/RepliesController.php create mode 100644 app/Http/Requests/Api/FormRequest.php create mode 100644 app/Http/Requests/Api/ReplyRequest.php create mode 100644 app/Transformers/ReplyTransformer.php diff --git a/app/Http/Controllers/Api/RepliesController.php b/app/Http/Controllers/Api/RepliesController.php new file mode 100644 index 0000000..e2d2bd4 --- /dev/null +++ b/app/Http/Controllers/Api/RepliesController.php @@ -0,0 +1,50 @@ +content = $request->get('content'); + $reply->topic_id = $topic->id; + $reply->user_id = $this->user()->id; + $reply->save(); + return $this->response->item($reply, new ReplyTransformer())->setStatusCode(201); + } + + /** + * @param Reply $reply + * @return \Dingo\Api\Http\Response + * @throws \Illuminate\Auth\Access\AuthorizationException + * @throws \Throwable + */ + public function destroy(Reply $reply) + { + $this->authorize('destroy', $reply); + $reply->delete(); + return $this->response->noContent(); + } + + /** + * @param Topic $topic + * @return \Dingo\Api\Http\Response + */ + public function index(Topic $topic) + { + $replies = $topic->replies()->recent()->paginate(20); + return $this->response->paginator($replies, new ReplyTransformer()); + } + + public function userIndex(User $user) + { + $replies = $user->replies()->recent()->paginate(20); + return $this->response->paginator($replies, new ReplyTransformer()); + } +} diff --git a/app/Http/Requests/Api/AuthorizationRequest.php b/app/Http/Requests/Api/AuthorizationRequest.php index d739564..d4b39ee 100644 --- a/app/Http/Requests/Api/AuthorizationRequest.php +++ b/app/Http/Requests/Api/AuthorizationRequest.php @@ -2,7 +2,6 @@ namespace App\Http\Requests\Api; -use Illuminate\Foundation\Http\FormRequest; class AuthorizationRequest extends FormRequest { diff --git a/app/Http/Requests/Api/CaptchaRequest.php b/app/Http/Requests/Api/CaptchaRequest.php index 4e4f83e..2314c1e 100644 --- a/app/Http/Requests/Api/CaptchaRequest.php +++ b/app/Http/Requests/Api/CaptchaRequest.php @@ -2,19 +2,9 @@ namespace App\Http\Requests\Api; -use Illuminate\Foundation\Http\FormRequest; class CaptchaRequest extends FormRequest { - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } /** * Get the validation rules that apply to the request. diff --git a/app/Http/Requests/Api/FormRequest.php b/app/Http/Requests/Api/FormRequest.php new file mode 100644 index 0000000..0a3e9ab --- /dev/null +++ b/app/Http/Requests/Api/FormRequest.php @@ -0,0 +1,30 @@ + 'required|min:2', + ]; + } +} diff --git a/app/Http/Requests/Api/SocialAuthorizationRequest.php b/app/Http/Requests/Api/SocialAuthorizationRequest.php index 13648fb..63b7eee 100644 --- a/app/Http/Requests/Api/SocialAuthorizationRequest.php +++ b/app/Http/Requests/Api/SocialAuthorizationRequest.php @@ -2,19 +2,8 @@ namespace App\Http\Requests\Api; -use Illuminate\Foundation\Http\FormRequest; - class SocialAuthorizationRequest extends FormRequest { - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } /** * Get the validation rules that apply to the request. diff --git a/app/Http/Requests/Api/TopicRequest.php b/app/Http/Requests/Api/TopicRequest.php index 5c1bcd1..e01b45d 100644 --- a/app/Http/Requests/Api/TopicRequest.php +++ b/app/Http/Requests/Api/TopicRequest.php @@ -2,7 +2,6 @@ namespace App\Http\Requests\Api; -use Illuminate\Foundation\Http\FormRequest; /** * Class TopicRequest @@ -13,15 +12,6 @@ use Illuminate\Foundation\Http\FormRequest; */ class TopicRequest extends FormRequest { - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } public function rules() { diff --git a/app/Http/Requests/Api/VerificationCodeRequest.php b/app/Http/Requests/Api/VerificationCodeRequest.php index 4cbfbcb..852a66f 100644 --- a/app/Http/Requests/Api/VerificationCodeRequest.php +++ b/app/Http/Requests/Api/VerificationCodeRequest.php @@ -2,19 +2,9 @@ namespace App\Http\Requests\Api; -use Illuminate\Foundation\Http\FormRequest; class VerificationCodeRequest extends FormRequest { - /** - * Determine if the user is authorized to make this request. - * - * @return bool - */ - public function authorize() - { - return true; - } /** * Get the validation rules that apply to the request. diff --git a/app/Models/Reply.php b/app/Models/Reply.php index 7040d3a..5f5fbb0 100644 --- a/app/Models/Reply.php +++ b/app/Models/Reply.php @@ -2,6 +2,17 @@ namespace App\Models; +/** + * Class Reply + * @property int id + * @property string content + * @property string created_at + * @property string updated_at + * @property int user_id + * @property int topic_id + * @property User user + * @package App\Models + */ class Reply extends Model { protected $fillable = ['content']; diff --git a/app/Transformers/ReplyTransformer.php b/app/Transformers/ReplyTransformer.php new file mode 100644 index 0000000..51a0cf3 --- /dev/null +++ b/app/Transformers/ReplyTransformer.php @@ -0,0 +1,36 @@ + (int)$reply->id, + 'user_id' => (int)$reply->user_id, + 'topic_id' => (int)$reply->topic_id, + 'content' => $reply->content, + 'created_at' => $reply->created_at instanceof Carbon ? $reply->created_at->toDateTimeString() : $reply->created_at, + 'updated_at' => $reply->updated_at instanceof Carbon ? $reply->updated_at->toDateTimeString() : $reply->updated_at, + ]; + } + + public function includeUser(Reply $reply) + { + return $this->item($reply->user, new UserTransformer()); + } +} \ No newline at end of file diff --git a/routes/api.php b/routes/api.php index 6c5e680..860b1bd 100644 --- a/routes/api.php +++ b/routes/api.php @@ -76,7 +76,17 @@ $api->version('v1', [ $api->patch('topics/{topic}', 'TopicsController@update')->name('api.topics.update'); //删除话题 $api->delete('topics/{topic}', 'TopicsController@destroy')->name('api.topics.destroy'); - + // 发布回复 + $api->post('topics/{topic}/replies', 'RepliesController@store') + ->name('api.topics.replies.store'); + //删除回复 + $api->delete('replies/{reply}', 'RepliesController@destroy')->name('api.replies.destroy'); + // 话题回复列表 + $api->get('topics/{topic}/replies', 'RepliesController@index') + ->name('api.topics.replies.index'); + //某个用户的回复列表 + $api->get('users/{user}/replies', 'RepliesController@userIndex') + ->name('api.users.replies.index'); }); });