Skip to content

Commit da7740f

Browse files
get user by id and make home structure
1 parent b5ccdaa commit da7740f

File tree

7 files changed

+298
-206
lines changed

7 files changed

+298
-206
lines changed

Codelink.postman_collection.json

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@
2727
"formdata": [
2828
{
2929
"key": "name",
30-
"value": "salah",
30+
"value": "eman",
3131
"type": "text"
3232
},
3333
{
3434
"key": "email",
35-
"value": "salah@gmail.com",
35+
"value": "emanelhelaly11@gmail.com",
3636
"type": "text"
3737
},
3838
{
@@ -228,7 +228,7 @@
228228
},
229229
{
230230
"key": "code",
231-
"value": "2305",
231+
"value": "3125",
232232
"type": "text"
233233
}
234234
]
@@ -309,6 +309,67 @@
309309
},
310310
"response": []
311311
},
312+
{
313+
"name": "users/getUser",
314+
"protocolProfileBehavior": {
315+
"disableBodyPruning": true
316+
},
317+
"request": {
318+
"auth": {
319+
"type": "bearer",
320+
"bearer": [
321+
{
322+
"key": "token",
323+
"value": "3|aSaW60z2gLueW6If4mx6kcxleKBtlotgeAgCmkvi",
324+
"type": "string"
325+
}
326+
]
327+
},
328+
"method": "GET",
329+
"header": [
330+
{
331+
"key": "Accept",
332+
"value": "application/json",
333+
"type": "text"
334+
},
335+
{
336+
"key": "Authorization",
337+
"value": "application/json",
338+
"type": "text"
339+
}
340+
],
341+
"body": {
342+
"mode": "formdata",
343+
"formdata": [
344+
{
345+
"key": "email",
346+
"value": "[email protected]",
347+
"type": "text"
348+
},
349+
{
350+
"key": "password",
351+
"value": "123123",
352+
"type": "text"
353+
}
354+
]
355+
},
356+
"url": {
357+
"raw": "http://localhost:8000/api/users/show/1",
358+
"protocol": "http",
359+
"host": [
360+
"localhost"
361+
],
362+
"port": "8000",
363+
"path": [
364+
"api",
365+
"users",
366+
"show",
367+
"1"
368+
]
369+
}
370+
},
371+
"response": []
372+
},
312373
{
313374
"name": "posts",
314375
"request": {
@@ -317,7 +378,7 @@
317378
"bearer": [
318379
{
319380
"key": "token",
320-
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
381+
"value": "3|aSaW60z2gLueW6If4mx6kcxleKBtlotgeAgCmkvi",
321382
"type": "string"
322383
}
323384
]
@@ -379,7 +440,7 @@
379440
"bearer": [
380441
{
381442
"key": "token",
382-
"value": "1|2YArSOq0wRwfYxdSvlemFcX5PlewiRNkXlS8SQGz",
443+
"value": "3|aSaW60z2gLueW6If4mx6kcxleKBtlotgeAgCmkvi",
383444
"type": "string"
384445
}
385446
]
@@ -409,8 +470,7 @@
409470
{
410471
"key": "file_path",
411472
"type": "file",
412-
"src": [],
413-
"disabled": true
473+
"src": "/E:/Photos/IMG_20230102_040115_692.jpg"
414474
}
415475
]
416476
},
@@ -441,7 +501,7 @@
441501
"bearer": [
442502
{
443503
"key": "token",
444-
"value": "1|DZu0Q0HEtqM18Sn1Xt7GYJPoNsKg50WPNQkUF0vb",
504+
"value": "3|aSaW60z2gLueW6If4mx6kcxleKBtlotgeAgCmkvi",
445505
"type": "string"
446506
}
447507
]
@@ -454,7 +514,6 @@
454514
"type": "text"
455515
},
456516
{
457-
"warning": "This is a duplicate header and will be overridden by the Authorization header generated by Postman.",
458517
"key": "Authorization",
459518
"value": "application/json",
460519
"type": "text"
@@ -489,7 +548,7 @@
489548
"bearer": [
490549
{
491550
"key": "token",
492-
"value": "1|TWIwa7od8VtgAanjieMZoVEOiM0hV57tTZsUqXXe",
551+
"value": "3|aSaW60z2gLueW6If4mx6kcxleKBtlotgeAgCmkvi",
493552
"type": "string"
494553
}
495554
]
@@ -513,19 +572,19 @@
513572
"formdata": [
514573
{
515574
"key": "content",
516-
"value": "edit",
575+
"value": "",
517576
"type": "text"
518577
},
519578
{
520579
"key": "file_path",
521580
"type": "file",
522-
"src": "/E:/Photos/IMG_20230102_040115_692.jpg",
581+
"src": [],
523582
"disabled": true
524583
}
525584
]
526585
},
527586
"url": {
528-
"raw": "http://localhost:8000/api/posts/edit/11",
587+
"raw": "http://localhost:8000/api/posts/edit/1",
529588
"protocol": "http",
530589
"host": [
531590
"localhost"
@@ -535,7 +594,7 @@
535594
"api",
536595
"posts",
537596
"edit",
538-
"11"
597+
"1"
539598
]
540599
}
541600
},

app/Http/Controllers/Api/CommentController.php renamed to app/Http/Controllers/Api/Home/CommentController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Controllers\Api;
3+
namespace App\Http\Controllers\Api\Home;
44

55
use App\Http\Controllers\Controller;
66
use Illuminate\Http\Request;

app/Http/Controllers/Api/LikesController.php renamed to app/Http/Controllers/Api/Home/LikesController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace App\Http\Controllers\Api;
3+
namespace App\Http\Controllers\Api\Home;
44

55
use App\Http\Controllers\Controller;
66
use App\Models\Like;
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
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\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(['post' => $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+
$post->save();
83+
84+
// Fetch the post again to include user_name, user_imageUrl, and other data
85+
$post = Post::with('user')->find($post->id);
86+
87+
$postData = $post->toArray();
88+
// Replace the user_id with user's name and imageUrl
89+
$postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
90+
$postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
91+
unset($postData['user']); // Remove the user relationship to avoid redundancy
92+
93+
return $this->data(['post' => $postData], 'Post created successfully', 201);
94+
}
95+
96+
public function editPost(PostRequest $request, $id)
97+
{
98+
$post = Post::find($id);
99+
if (!$post) {
100+
return $this->errorMessage([], 'Post not found', 404);
101+
}
102+
103+
if ($post->user_id !== Auth::id()) {
104+
return $this->errorMessage([], 'You are not authorized to edit this post', 403);
105+
}
106+
107+
// Check if there are any changes to update
108+
if (!$request->filled('content') && !$request->hasFile('file_path')) {
109+
return $this->errorMessage([], 'No changes to update', 422);
110+
}
111+
112+
// Update the post content if provided
113+
if ($request->filled('content')) {
114+
$post->content = $request->input('content');
115+
}
116+
117+
// Handle image upload, if provided
118+
if ($request->hasFile('file_path')) {
119+
$image = $request->file('file_path');
120+
$imagePath = $this->upload($image, 'posts');
121+
$post->image_path = "images/posts/$imagePath";
122+
} elseif (empty($request->input('file_path'))) {
123+
// No new image provided, check if 'file_path' input is empty
124+
// If it's empty, delete the existing image
125+
$this->delete($post->image_path);
126+
$post->image_path = null; // Set image_path to null in the database
127+
}
128+
129+
$post->save();
130+
131+
// Fetch the post again to include user_name, user_imageUrl, and other data
132+
$post = Post::with('user')->find($post->id);
133+
134+
$postData = $post->toArray();
135+
// Replace the user_id with user's name and imageUrl
136+
$postData['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
137+
$postData['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
138+
unset($postData['user']); // Remove the user relationship to avoid redundancy
139+
140+
if ($request->filled('content') || $request->hasFile('file_path')) {
141+
return $this->data(['post' => $postData], 'Post updated successfully', 200);
142+
} else {
143+
return $this->errorMessage([], 'No changes to update', 422);
144+
}
145+
}
146+
147+
148+
149+
150+
public function deletePost($id)
151+
{
152+
$post = Post::find($id);
153+
if (!$post) {
154+
return $this->errorMessage([], 'Post not found', 404);
155+
}
156+
157+
if ($post->user_id !== Auth::id()) {
158+
return $this->errorMessage([], 'You are not authorized to edit this post', 403);
159+
}
160+
161+
// Delete the post and associated image, if it exists
162+
if ($post->image_path) {
163+
$this->delete($post->image_path);
164+
}
165+
166+
$post->delete();
167+
return $this->successMessage('Post deleted successfully', 200);
168+
}
169+
170+
public function getUserPosts($userId)
171+
{
172+
// Find the user by their ID
173+
$user = User::find($userId);
174+
175+
if (!$user) {
176+
return $this->errorMessage([], 'User not found', 404);
177+
}
178+
179+
// Retrieve all posts for the user with the user relationship
180+
$posts = $user->posts()->orderBy('created_at', 'desc')->get();
181+
182+
// Transform the posts data to include user_name and user_imageUrl
183+
$postData = $posts->map(function ($post) {
184+
$data = $post->toArray();
185+
$data['user_name'] = $post->user->name; // Change 'name' to the actual column name in your users table
186+
$data['user_imageUrl'] = $post->user->imageUrl; // Change 'imageUrl' to the actual column name in your users table
187+
unset($data['user']); // Remove the user relationship to avoid redundancy
188+
return $data;
189+
});
190+
191+
return $this->data(compact('postData'));
192+
}
193+
194+
195+
196+
}

0 commit comments

Comments
 (0)