|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\Api; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use Illuminate\Http\Request; |
| 7 | +use App\Models\Post; |
| 8 | +use App\Models\User; |
| 9 | +use Illuminate\Support\Facades\Auth; |
| 10 | +use Illuminate\Support\Facades\Validator; |
| 11 | +use App\Http\Requests\PostRequest; |
| 12 | +use App\Traits\ApiTrait; |
| 13 | +use App\Traits\Media; |
| 14 | + |
| 15 | +class PostController extends Controller |
| 16 | +{ |
| 17 | + use ApiTrait, Media; |
| 18 | + |
| 19 | + public function getPosts() |
| 20 | + { |
| 21 | + // Retrieve all posts with the user relationship and order them by 'created_at' in descending order |
| 22 | + $posts = Post::with('user')->orderBy('created_at', 'desc')->get(); |
| 23 | + |
| 24 | + // Transform the posts data to include user_name and user_imageUrl |
| 25 | + $postData = $posts->map(function ($post) { |
| 26 | + $data = $post->toArray(); |
| 27 | + $data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table |
| 28 | + $data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table |
| 29 | + unset($data['user']); // Remove the user relationship to avoid redundancy |
| 30 | + return $data; |
| 31 | + }); |
| 32 | + |
| 33 | + return $this->data(compact('postData')); |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + public function showPost($id) |
| 38 | + { |
| 39 | + $post = Post::with('user')->find($id); |
| 40 | + |
| 41 | + if (!$post) { |
| 42 | + return $this->errorMessage([], 'Post not found', 404); |
| 43 | + } |
| 44 | + |
| 45 | + // Convert the Post model and user model to an array |
| 46 | + $postData = $post->toArray(); |
| 47 | + |
| 48 | + // Replace the user_id with user's name and imageUrl |
| 49 | + $postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table |
| 50 | + $postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table |
| 51 | + |
| 52 | + // Unset the user relationship to avoid redundancy |
| 53 | + unset($postData['user']); |
| 54 | + |
| 55 | + return $this->data($postData, 'Post retrieved successfully', 200); |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | + public function createPost(PostRequest $request) |
| 60 | + { |
| 61 | + // Check if either 'content' or 'file_path' is provided |
| 62 | + if (!$request->filled('content') && !$request->hasFile('file_path')) { |
| 63 | + return $this->errorMessage([], 'Either content or an image must be provided', 422); |
| 64 | + } |
| 65 | + |
| 66 | + // Create a new post |
| 67 | + $post = new Post; |
| 68 | + $post->user_id = Auth::id(); |
| 69 | + |
| 70 | + // Set content if provided |
| 71 | + if ($request->filled('content')) { |
| 72 | + $post->content = $request->input('content'); |
| 73 | + } |
| 74 | + |
| 75 | + // Handle image upload, if provided |
| 76 | + if ($request->hasFile('file_path')) { |
| 77 | + $image = $request->file('file_path'); |
| 78 | + $imagePath = $this->upload($image, 'posts'); |
| 79 | + $post->image_path = "images/posts/$imagePath"; |
| 80 | + } |
| 81 | + |
| 82 | + |
| 83 | + $post->save(); |
| 84 | + return $this->successMessage('Post created successfully', 201); |
| 85 | + } |
| 86 | + |
| 87 | + public function editPost(PostRequest $request, $id) |
| 88 | + { |
| 89 | + $post = Post::find($id); |
| 90 | + if (!$post) { |
| 91 | + return $this->errorMessage([], 'Post not found', 404); |
| 92 | + } |
| 93 | + |
| 94 | + if ($post->user_id !== Auth::id()) { |
| 95 | + return $this->errorMessage([], 'You are not authorized to edit this post', 403); |
| 96 | + } |
| 97 | + |
| 98 | + // Check if there are any changes to update |
| 99 | + if (!$request->filled('content') && !$request->hasFile('file_path')) { |
| 100 | + return $this->errorMessage([], 'No changes to update', 422); |
| 101 | + } |
| 102 | + |
| 103 | + // Update the post content if provided |
| 104 | + if ($request->filled('content')) { |
| 105 | + $post->content = $request->input('content'); |
| 106 | + } |
| 107 | + |
| 108 | + // Handle image upload, if provided |
| 109 | + if ($request->hasFile('file_path')) { |
| 110 | + $image = $request->file('file_path'); |
| 111 | + $imagePath = $this->upload($image, 'posts'); |
| 112 | + $post->image_path = "images/posts/$imagePath"; |
| 113 | + } |
| 114 | + |
| 115 | + $post->save(); |
| 116 | + |
| 117 | + if ($request->filled('content') || $request->hasFile('file_path')) { |
| 118 | + return $this->successMessage('Post updated successfully', 200); |
| 119 | + } else { |
| 120 | + return $this->errorMessage([], 'No changes to update', 422); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + |
| 125 | + public function deletePost($id) |
| 126 | + { |
| 127 | + $post = Post::find($id); |
| 128 | + if (!$post) { |
| 129 | + return $this->errorMessage([], 'Post not found', 404); |
| 130 | + } |
| 131 | + |
| 132 | + if ($post->user_id !== Auth::id()) { |
| 133 | + return $this->errorMessage([], 'You are not authorized to edit this post', 403); |
| 134 | + } |
| 135 | + |
| 136 | + // Delete the post and associated image, if it exists |
| 137 | + if ($post->image_path) { |
| 138 | + $this->delete($post->image_path); |
| 139 | + } |
| 140 | + |
| 141 | + $post->delete(); |
| 142 | + return $this->successMessage('Post deleted successfully', 200); |
| 143 | + } |
| 144 | + |
| 145 | + public function getUserPosts($userId) |
| 146 | + { |
| 147 | + // Find the user by their ID |
| 148 | + $user = User::find($userId); |
| 149 | + |
| 150 | + if (!$user) { |
| 151 | + return $this->errorMessage([], 'User not found', 404); |
| 152 | + } |
| 153 | + |
| 154 | + // Retrieve all posts for the user with the user relationship |
| 155 | + $posts = $user->posts()->orderBy('created_at', 'desc')->get(); |
| 156 | + |
| 157 | + // Transform the posts data to include user_name and user_imageUrl |
| 158 | + $postData = $posts->map(function ($post) { |
| 159 | + $data = $post->toArray(); |
| 160 | + $data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table |
| 161 | + $data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table |
| 162 | + unset($data['user']); // Remove the user relationship to avoid redundancy |
| 163 | + return $data; |
| 164 | + }); |
| 165 | + |
| 166 | + return $this->data(compact('postData')); |
| 167 | + } |
| 168 | +} |
0 commit comments