-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added API Resources: MessageResource[Collection], ThreadResourc…
…e[Collection]
- Loading branch information
1 parent
afa168e
commit 59c1951
Showing
5 changed files
with
118 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |