diff --git a/app/Http/Controllers/MessageController.php b/app/Http/Controllers/MessageController.php new file mode 100644 index 0000000..670529d --- /dev/null +++ b/app/Http/Controllers/MessageController.php @@ -0,0 +1,73 @@ +paginate(6); + return MessageResource::collection($messages); + } + + /** + * Store a newly created resource in storage. + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Http\Response + */ + public function store(StoreMessageRequest $request) + { + $user = Auth::user(); + $message = new Message(['body' => $request->body]); + $user->messages()->save($message); + + $messages = Message::orderByDesc('created_at')->paginate(6); + return MessageResource::collection($messages); + } + + /** + * Display the specified resource. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function show($id) + { + // + } + + /** + * Update the specified resource in storage. + * + * @param \Illuminate\Http\Request $request + * @param int $id + * @return \Illuminate\Http\Response + */ + public function update(Request $request, $id) + { + // + } + + /** + * Remove the specified resource from storage. + * + * @param int $id + * @return \Illuminate\Http\Response + */ + public function destroy($id) + { + // + } +} diff --git a/app/Http/Requests/StoreMessageRequest.php b/app/Http/Requests/StoreMessageRequest.php new file mode 100644 index 0000000..5e7c0c4 --- /dev/null +++ b/app/Http/Requests/StoreMessageRequest.php @@ -0,0 +1,30 @@ + 'required|max:250', + ]; + } +} diff --git a/app/Http/Resources/MessageResource.php b/app/Http/Resources/MessageResource.php new file mode 100644 index 0000000..f41d8a6 --- /dev/null +++ b/app/Http/Resources/MessageResource.php @@ -0,0 +1,25 @@ + $this->id, + 'body' => $this->body, + 'user' => new UserBasicResource($this->user), + 'createdAt' => $this->created_at->diffForHumans(), + ]; + } +} diff --git a/app/Http/Resources/UserBasicResource.php b/app/Http/Resources/UserBasicResource.php new file mode 100644 index 0000000..681e181 --- /dev/null +++ b/app/Http/Resources/UserBasicResource.php @@ -0,0 +1,23 @@ + $this->id, + 'name' => $this->name, + 'email' => $this->email, + ]; + } +} diff --git a/app/Models/Message.php b/app/Models/Message.php new file mode 100644 index 0000000..2c10f93 --- /dev/null +++ b/app/Models/Message.php @@ -0,0 +1,25 @@ +belongsTo(User::class); + } +} diff --git a/app/Models/User.php b/app/Models/User.php index 8baee7f..4139a28 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -48,4 +48,9 @@ public function isAdmin(): bool { return $this->is_admin; } + + public function messages() + { + return $this->hasMany(Message::class); + } } diff --git a/database/migrations/2021_01_22_220306_create_messages_table.php b/database/migrations/2021_01_22_220306_create_messages_table.php new file mode 100644 index 0000000..40193ae --- /dev/null +++ b/database/migrations/2021_01_22_220306_create_messages_table.php @@ -0,0 +1,33 @@ +id(); + $table->foreignId('user_id')->constrained()->onDelete('cascade'); + $table->text('body'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('messages'); + } +} diff --git a/routes/api.php b/routes/api.php index a46b22d..b4529f2 100644 --- a/routes/api.php +++ b/routes/api.php @@ -5,6 +5,7 @@ use App\Http\Controllers\UserController; use App\Http\Controllers\TokenController; use App\Http\Controllers\AvatarController; +use App\Http\Controllers\MessageController; /* |-------------------------------------------------------------------------- @@ -25,4 +26,7 @@ Route::get('/users', [UserController::class, 'index']); Route::post('/users/auth/avatar', [AvatarController::class, 'store']); + + Route::post('/messages', [MessageController::class, 'store']); + Route::get('/messages', [MessageController::class, 'index']); });