|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api\Home; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use App\Models\Comment; |
| 8 | +use App\Models\Post; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use App\Http\Requests\CommentRequest; |
| 11 | +use App\Traits\ApiTrait; |
| 12 | + |
| 13 | +class CommentController extends Controller |
| 14 | +{ |
| 15 | + use ApiTrait; |
| 16 | + |
| 17 | + public function showAllComments(Post $post) |
| 18 | + { |
| 19 | + try { |
| 20 | + $comments = $post->comments->map(function ($comment) { |
| 21 | + $user = $comment->user; |
| 22 | + return [ |
| 23 | + 'id' => $comment->id, |
| 24 | + 'content' => $comment->content, |
| 25 | + 'post_id' => $comment->post_id, |
| 26 | + 'user_id' => $comment->user_id, |
| 27 | + 'user_name' => $user->name, |
| 28 | + 'user_imageUrl' => $user->imageUrl, |
| 29 | + 'deleted_at' => $comment->deleted_at, |
| 30 | + 'created_at' => $comment->created_at, |
| 31 | + 'updated_at' => $comment->updated_at, |
| 32 | + ]; |
| 33 | + }); |
| 34 | + |
| 35 | + return $this->data(compact('comments')); |
| 36 | + } catch (\Exception $e) { |
| 37 | + return $this->errorMessage([], 'An error occurred while fetching comments', 500); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public function createComment(CommentRequest $request, Post $post) |
| 42 | + { |
| 43 | + try { |
| 44 | + // Create a new comment |
| 45 | + $comment = new Comment(); |
| 46 | + $comment->content = $request->input('content'); |
| 47 | + $comment->user_id = Auth::id(); // Set the user ID from the authenticated user |
| 48 | + $comment->post_id = $post->id; // Set the post ID |
| 49 | + $comment->save(); |
| 50 | + |
| 51 | + // Update the comments_count for the post |
| 52 | + $post->increment('comments_count'); |
| 53 | + |
| 54 | + // Fetch all comments for the post |
| 55 | + $comments = $post->comments->map(function ($comment) { |
| 56 | + $user = $comment->user; |
| 57 | + return [ |
| 58 | + 'id' => $comment->id, |
| 59 | + 'content' => $comment->content, |
| 60 | + 'post_id' => $comment->post_id, |
| 61 | + 'user_id' => $comment->user_id, |
| 62 | + 'user_name' => $user->name, |
| 63 | + 'user_imageUrl' => $user->imageUrl, |
| 64 | + 'deleted_at' => $comment->deleted_at, |
| 65 | + 'created_at' => $comment->created_at, |
| 66 | + 'updated_at' => $comment->updated_at, |
| 67 | + ]; |
| 68 | + }); |
| 69 | + |
| 70 | + return $this->data(compact('comments'), 'Comment created successfully', 201); |
| 71 | + } catch (\Exception $e) { |
| 72 | + return $this->errorMessage([], 'An error occurred while creating the comment', 500); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public function editComment(CommentRequest $request, Post $post, Comment $comment) |
| 77 | + { |
| 78 | + try { |
| 79 | + // Check if the comment belongs to the post |
| 80 | + if ($comment->post_id !== $post->id) { |
| 81 | + return $this->errorMessage([], 'Comment not found for the specified post', 404); |
| 82 | + } |
| 83 | + |
| 84 | + // Update the comment content |
| 85 | + $comment->content = $request->input('content'); |
| 86 | + $comment->save(); |
| 87 | + |
| 88 | + // Fetch all comments for the post |
| 89 | + $comments = $post->comments->map(function ($comment) { |
| 90 | + $user = $comment->user; |
| 91 | + return [ |
| 92 | + 'id' => $comment->id, |
| 93 | + 'content' => $comment->content, |
| 94 | + 'post_id' => $comment->post_id, |
| 95 | + 'user_id' => $comment->user_id, |
| 96 | + 'user_name' => $user->name, |
| 97 | + 'user_imageUrl' => $user->imageUrl, |
| 98 | + 'deleted_at' => $comment->deleted_at, |
| 99 | + 'created_at' => $comment->created_at, |
| 100 | + 'updated_at' => $comment->updated_at, |
| 101 | + ]; |
| 102 | + }); |
| 103 | + |
| 104 | + return $this->data(compact('comments'), 'Comment updated successfully', 200); |
| 105 | + } catch (\Exception $e) { |
| 106 | + return $this->errorMessage([], 'An error occurred while updating the comment', 500); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + public function deleteComment(Post $post, Comment $comment) |
| 111 | + { |
| 112 | + try { |
| 113 | + // Check if the comment belongs to the post |
| 114 | + if ($comment->post_id !== $post->id) { |
| 115 | + return $this->errorMessage([], 'Comment not found for the specified post', 404); |
| 116 | + } |
| 117 | + |
| 118 | + // Decrement the comments_count for the post |
| 119 | + $post->decrement('comments_count'); |
| 120 | + |
| 121 | + // Delete the comment |
| 122 | + $comment->delete(); |
| 123 | + |
| 124 | + return $this->data(['post_id' => $post->id],'Comment deleted successfully', 200); |
| 125 | + } catch (\Exception $e) { |
| 126 | + return $this->errorMessage([], 'An error occurred while deleting the comment', 500); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments