Skip to content

Commit 59c1951

Browse files
committed
feat: added API Resources: MessageResource[Collection], ThreadResource[Collection]
1 parent afa168e commit 59c1951

File tree

5 files changed

+118
-2
lines changed

5 files changed

+118
-2
lines changed

src/Models/Thread.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,22 @@ class Thread extends Eloquent
2525
*
2626
* @var array
2727
*/
28-
protected $fillable = ['subject', 'slug', 'start_date', 'end_date', 'max_participants', 'avatar'];
28+
protected $fillable = [
29+
'subject', 'slug',
30+
'start_date', 'end_date',
31+
'max_participants', 'avatar',
32+
];
2933

3034
/**
3135
* The attributes that should be mutated to dates.
3236
*
3337
* @var array
3438
*/
35-
protected $dates = ['deleted_at'];
39+
protected $dates = [
40+
'deleted_at',
41+
'start_date',
42+
'end_date',
43+
];
3644

3745
/**
3846
* Internal cache for creator.

src/Resources/MessageResource.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Lexx\ChatMessenger\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class MessageResource extends JsonResource
8+
{
9+
/**
10+
* Transform the resource into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
return [
18+
'id' => (int) $this->id,
19+
'body' => $this->body,
20+
'user_id' => $this->user_id,
21+
'thread_id' => $this->thread_id,
22+
23+
'name' => optional($this->user)->name ?? null,
24+
'user_avatar' => optional($this->user)->avatar_url,
25+
26+
'created_at' => $this->created_at,
27+
'updated_at' => $this->updated_at,
28+
'deleted_at' => $this->deleted_at,
29+
];
30+
}
31+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Lexx\ChatMessenger\Resources;
4+
5+
use Illuminate\Http\Resources\Json\ResourceCollection;
6+
7+
class MessageResourceCollection extends ResourceCollection
8+
{
9+
/**
10+
* Transform the resource collection into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
return $this->collection;
18+
}
19+
}

src/Resources/ThreadResource.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Lexx\ChatMessenger\Resources;
4+
5+
use Illuminate\Http\Resources\Json\JsonResource;
6+
7+
class ThreadResource extends JsonResource
8+
{
9+
/**
10+
* Transform the resource into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
return [
18+
'id' => (int) $this->id,
19+
'subject' => $this->subject,
20+
'slug' => $this->slug,
21+
'start_date' => $this->start_date,
22+
'end_date' => $this->end_date,
23+
'max_participants' => $this->max_participants,
24+
'avatar' => $this->avatar,
25+
26+
'created_at' => $this->created_at,
27+
'updated_at' => $this->updated_at,
28+
'deleted_at' => $this->deleted_at,
29+
30+
'unread_count' => $this->userUnreadMessagesCount(auth()->id()),
31+
'latest_message' => optional($this->latestMessage)->body ?? null,
32+
'creator' => optional($this->creator())->name,
33+
'creator_avatar' => optional($this->creator())->avatar_url,
34+
'participants' => $this->participantsString(auth()->id()),
35+
36+
'messages' => MessageResource::collection($this->whenLoaded('messages')),
37+
];
38+
}
39+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Lexx\ChatMessenger\Resources;
4+
5+
use Illuminate\Http\Resources\Json\ResourceCollection;
6+
7+
class ThreadResourceCollection extends ResourceCollection
8+
{
9+
/**
10+
* Transform the resource collection into an array.
11+
*
12+
* @param \Illuminate\Http\Request $request
13+
* @return array
14+
*/
15+
public function toArray($request)
16+
{
17+
return $this->collection;
18+
}
19+
}

0 commit comments

Comments
 (0)