Skip to content

Commit

Permalink
feat: added API Resources: MessageResource[Collection], ThreadResourc…
Browse files Browse the repository at this point in the history
…e[Collection]
  • Loading branch information
syntaxlexx committed Jan 17, 2022
1 parent afa168e commit 59c1951
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Models/Thread.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,22 @@ class Thread extends Eloquent
*
* @var array
*/
protected $fillable = ['subject', 'slug', 'start_date', 'end_date', 'max_participants', 'avatar'];
protected $fillable = [
'subject', 'slug',
'start_date', 'end_date',
'max_participants', 'avatar',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = ['deleted_at'];
protected $dates = [
'deleted_at',
'start_date',
'end_date',
];

/**
* Internal cache for creator.
Expand Down
31 changes: 31 additions & 0 deletions src/Resources/MessageResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Lexx\ChatMessenger\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class MessageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => (int) $this->id,
'body' => $this->body,
'user_id' => $this->user_id,
'thread_id' => $this->thread_id,

'name' => optional($this->user)->name ?? null,
'user_avatar' => optional($this->user)->avatar_url,

'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'deleted_at' => $this->deleted_at,
];
}
}
19 changes: 19 additions & 0 deletions src/Resources/MessageResourceCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Lexx\ChatMessenger\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class MessageResourceCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection;
}
}
39 changes: 39 additions & 0 deletions src/Resources/ThreadResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Lexx\ChatMessenger\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class ThreadResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => (int) $this->id,
'subject' => $this->subject,
'slug' => $this->slug,
'start_date' => $this->start_date,
'end_date' => $this->end_date,
'max_participants' => $this->max_participants,
'avatar' => $this->avatar,

'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'deleted_at' => $this->deleted_at,

'unread_count' => $this->userUnreadMessagesCount(auth()->id()),
'latest_message' => optional($this->latestMessage)->body ?? null,
'creator' => optional($this->creator())->name,
'creator_avatar' => optional($this->creator())->avatar_url,
'participants' => $this->participantsString(auth()->id()),

'messages' => MessageResource::collection($this->whenLoaded('messages')),
];
}
}
19 changes: 19 additions & 0 deletions src/Resources/ThreadResourceCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Lexx\ChatMessenger\Resources;

use Illuminate\Http\Resources\Json\ResourceCollection;

class ThreadResourceCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return $this->collection;
}
}

0 comments on commit 59c1951

Please sign in to comment.