Skip to content

Commit

Permalink
Merge pull request #2 from garethredfern/messaging
Browse files Browse the repository at this point in the history
Messaging
  • Loading branch information
Gareth Redfern authored Jan 25, 2021
2 parents 4591284 + ad36d3d commit a591ef5
Show file tree
Hide file tree
Showing 8 changed files with 218 additions and 0 deletions.
73 changes: 73 additions & 0 deletions app/Http/Controllers/MessageController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace App\Http\Controllers;

use App\Models\Message;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use App\Http\Resources\MessageResource;
use App\Http\Requests\StoreMessageRequest;

class MessageController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$messages = Message::orderByDesc('created_at')->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)
{
//
}
}
30 changes: 30 additions & 0 deletions app/Http/Requests/StoreMessageRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class StoreMessageRequest 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.
*
* @return array
*/
public function rules()
{
return [
'body' => 'required|max:250',
];
}
}
25 changes: 25 additions & 0 deletions app/Http/Resources/MessageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Carbon;

class MessageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'body' => $this->body,
'user' => new UserBasicResource($this->user),
'createdAt' => $this->created_at->diffForHumans(),
];
}
}
23 changes: 23 additions & 0 deletions app/Http/Resources/UserBasicResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class UserBasicResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
];
}
}
25 changes: 25 additions & 0 deletions app/Models/Message.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Message extends Model
{
use HasFactory;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'body',
];

public function user()
{
return $this->belongsTo(User::class);
}
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public function isAdmin(): bool
{
return $this->is_admin;
}

public function messages()
{
return $this->hasMany(Message::class);
}
}
33 changes: 33 additions & 0 deletions database/migrations/2021_01_22_220306_create_messages_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateMessagesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('messages', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->onDelete('cascade');
$table->text('body');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('messages');
}
}
4 changes: 4 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\Http\Controllers\UserController;
use App\Http\Controllers\TokenController;
use App\Http\Controllers\AvatarController;
use App\Http\Controllers\MessageController;

/*
|--------------------------------------------------------------------------
Expand All @@ -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']);
});

0 comments on commit a591ef5

Please sign in to comment.