Skip to content

Commit a8ec382

Browse files
committed
✨ ajout de la creation et l'edition d'un commentaire
1 parent 85228d1 commit a8ec382

File tree

13 files changed

+336
-82
lines changed

13 files changed

+336
-82
lines changed

app/Events/CommentWasAdded.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace App\Events;
4+
5+
use App\Models\Discussion;
6+
use App\Models\Reply;
7+
use Illuminate\Queue\SerializesModels;
8+
9+
class CommentWasAdded
10+
{
11+
use SerializesModels;
12+
13+
public function __construct(public Reply $reply, public Discussion $discussion)
14+
{
15+
}
16+
}

app/Http/Controllers/Api/ReplyController.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22

33
namespace App\Http\Controllers\Api;
44

5+
use App\Events\CommentWasAdded;
56
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\Api\CreateReplyRequest;
8+
use App\Http\Requests\Api\UpdateReplyRequest;
69
use App\Http\Resources\ReplyResource;
710
use App\Models\Discussion;
811
use App\Models\Reaction;
@@ -11,26 +14,52 @@
1114
use Illuminate\Http\JsonResponse;
1215
use Illuminate\Http\Request;
1316
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
14-
use Illuminate\Support\Facades\Auth;
1517

1618
class ReplyController extends Controller
1719
{
1820
public function all(int $target): AnonymousResourceCollection
1921
{
2022
/** @var Discussion $discussion */
2123
$discussion = Discussion::findOrFail($target);
24+
$replies = collect();
2225

23-
return ReplyResource::collection($discussion->replies);
26+
foreach ($discussion->replies as $reply) {
27+
if ($reply->allChildReplies->isNotEmpty()) {
28+
foreach ($reply->allChildReplies as $childReply) {
29+
$replies->add($childReply);
30+
}
31+
}
32+
33+
$replies->add($reply);
34+
}
35+
36+
return ReplyResource::collection($replies);
2437
}
2538

26-
public function store()
39+
public function store(CreateReplyRequest $request): ReplyResource
2740
{
41+
// dd($request->all());
42+
// if ($request->parent) {}
43+
$reply = new Reply(['body' => $request->body]);
44+
$author = User::find($request->user_id);
45+
46+
$target = Discussion::find($request->target);
47+
$reply->authoredBy($author);
48+
$reply->to($target);
49+
$reply->save();
2850

51+
// On envoie un event pour une nouvelle réponse à tous les abonnés de la discussion
52+
event(new CommentWasAdded($reply, $target));
53+
54+
return new ReplyResource($reply);
2955
}
3056

31-
public function update()
57+
public function update(UpdateReplyRequest $request, int $id): ReplyResource
3258
{
59+
$reply = Reply::find($id);
60+
$reply->update(['body' => $request->body]);
3361

62+
return new ReplyResource($reply);
3463
}
3564

3665
public function like(Request $request, int $id): ReplyResource
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Api;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class CreateReplyRequest extends FormRequest
8+
{
9+
public function authorize(): bool
10+
{
11+
return true;
12+
}
13+
14+
public function rules(): array
15+
{
16+
return [
17+
'body' => 'required',
18+
'user_id' => 'required',
19+
];
20+
}
21+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Requests\Api;
4+
5+
use App\Http\Requests\FormRequest;
6+
7+
class UpdateReplyRequest extends FormRequest
8+
{
9+
public function authorize(): bool
10+
{
11+
return true;
12+
}
13+
14+
/**
15+
* Get the validation rules that apply to the request.
16+
*
17+
* @return array
18+
*/
19+
public function rules(): array
20+
{
21+
return [
22+
'body' => 'required',
23+
];
24+
}
25+
}

app/Http/Requests/FormRequest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Contracts\Validation\Validator;
6+
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
7+
use Illuminate\Http\JsonResponse;
8+
9+
abstract class FormRequest extends LaravelFormRequest
10+
{
11+
/**
12+
* Get the validation rules that apply to the request.
13+
*
14+
* @return array
15+
*/
16+
abstract public function rules(): array;
17+
18+
public function failedValidation(Validator $validator): JsonResponse
19+
{
20+
$transformed = [];
21+
22+
foreach ($validator->errors() as $field => $message) {
23+
$transformed[] = [
24+
'field' => $field,
25+
'message' => $message[0]
26+
];
27+
}
28+
29+
return response()->json([
30+
'errors' => $transformed
31+
], JsonResponse::HTTP_UNPROCESSABLE_ENTITY);
32+
}
33+
}

app/Http/Resources/ReplyResource.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ public function toArray($request): array
1616
'created_at' => $this->created_at->getTimestamp(),
1717
'author' => new UserResource($this->author),
1818
'has_replies' => $this->allChildReplies->isNotEmpty(),
19-
'replies' => $this->replyable_type === 'discussion' ? self::collection($this->replies) : null,
2019
'likes_count' => $this->getReactionsSummary()->sum('count'),
2120
];
2221
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
namespace App\Listeners;
4+
5+
use App\Events\CommentWasAdded;
6+
use App\Models\User;
7+
use App\Notifications\NewCommentNotification;
8+
9+
class SendNewCommentNotification
10+
{
11+
public function handle(CommentWasAdded $event)
12+
{
13+
$discussion = $event->discussion;
14+
15+
foreach ($discussion->subscribes as $subscription) {
16+
if ($this->replyAuthorDoesNotMatchSubscriber($event->reply->author, $subscription)) {
17+
$subscription->user->notify(new NewCommentNotification($event->reply, $subscription, $discussion));
18+
}
19+
}
20+
}
21+
22+
private function replyAuthorDoesNotMatchSubscriber(User $author, $subscription): bool
23+
{
24+
return ! $author->is($subscription->user);
25+
}
26+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Notifications;
4+
5+
use App\Models\Discussion;
6+
use App\Models\Reply;
7+
use App\Models\Subscribe;
8+
use Illuminate\Bus\Queueable;
9+
use Illuminate\Notifications\Messages\MailMessage;
10+
use Illuminate\Notifications\Notification;
11+
12+
class NewCommentNotification extends Notification
13+
{
14+
use Queueable;
15+
16+
public function __construct(public Reply $reply, public Subscribe $subscription, public Discussion $discussion)
17+
{
18+
}
19+
20+
public function via($notifiable): array
21+
{
22+
return ['mail', 'database'];
23+
}
24+
25+
public function toMail($notifiable): MailMessage
26+
{
27+
return (new MailMessage)
28+
->subject("Re: {$this->discussion->subject()}")
29+
->line('@' . $this->reply->author->username . ' a répondu à ce sujet.')
30+
->line($this->reply->excerpt(150))
31+
->action('Voir la discussion', route('discussions.show', $this->discussion))
32+
->line('Vous recevez ceci parce que vous êtes abonné à cette discussion.');
33+
}
34+
35+
public function toArray($notifiable): array
36+
{
37+
return [
38+
'type' => 'new_comment',
39+
'reply' => $this->reply->id,
40+
'replyable_id' => $this->reply->replyable_id,
41+
'replyable_type' => $this->reply->replyable_type,
42+
'replyable_subject' => $this->discussion->subject(),
43+
];
44+
}
45+
}

app/Providers/EventServiceProvider.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace App\Providers;
44

5+
use App\Events\CommentWasAdded;
56
use App\Events\ReplyWasCreated;
67
use App\Events\ThreadWasCreated;
78
use App\Listeners\NotifyMentionedUsers;
89
use App\Listeners\PostNewThreadNotification;
10+
use App\Listeners\SendNewCommentNotification;
911
use App\Listeners\SendNewReplyNotification;
1012
use App\Listeners\SendNewThreadNotification;
1113
use Illuminate\Auth\Events\Registered;
@@ -31,6 +33,9 @@ class EventServiceProvider extends ServiceProvider
3133
SendNewThreadNotification::class,
3234
PostNewThreadNotification::class,
3335
],
36+
CommentWasAdded::class => [
37+
SendNewCommentNotification::class,
38+
],
3439
];
3540

3641
/**

0 commit comments

Comments
 (0)