Skip to content

Commit cbafdac

Browse files
committed
profile controllers and routes
1 parent b5ccdaa commit cbafdac

File tree

6 files changed

+238
-87
lines changed

6 files changed

+238
-87
lines changed

app/Http/Controllers/Api/Auth/LoginController.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,42 +15,42 @@ class LoginController extends Controller
1515
public function login(LoginRequest $request)
1616
{
1717
$user = User::where('email', $request->email)->first();
18-
18+
1919
if (!$user || !Hash::check($request->password, $user->password)) {
2020
return ApiTrait::errorMessage(
2121
['login' => __('Invalid email or password.')],
2222
__('Login failed. Please check your credentials.'),
2323
403
2424
);
2525
}
26-
26+
2727
$responseData = [
2828
'user' => $user->only($user->responseFields()),
2929
];
30-
30+
3131
if (!$user->email_verified_at) {
3232
return ApiTrait::data(
3333
$responseData,
3434
__('Your account is not verified. Please verify your email.'),
3535
401
3636
);
3737
}
38-
38+
3939
$token = $user->createToken('auth_token')->plainTextToken;
4040
$responseData['user']['token'] = $token;
41-
41+
4242
return ApiTrait::data(
4343
$responseData,
4444
__('Login successful.'),
4545
200
4646
);
4747
}
48-
48+
4949

5050
public function logout(Request $request)
5151
{
5252
$user = Auth::guard('sanctum')->user();
53-
$user->currentAccessToken()->delete();
53+
// $user->currentAccessToken()->delete();
5454
return ApiTrait::successMessage(__('Logout successful.'));
5555
}
5656
}

app/Http/Controllers/Api/PostController.php

Lines changed: 57 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414

1515
class PostController extends Controller
1616
{
17-
use ApiTrait, Media;
17+
use ApiTrait, Media;
1818

1919
public function getPosts()
2020
{
2121
// Retrieve all posts with the user relationship and order them by 'created_at' in descending order
2222
$posts = Post::with('user')->orderBy('created_at', 'desc')->get();
23-
23+
2424
// Transform the posts data to include user_name and user_imageUrl
2525
$postData = $posts->map(function ($post) {
2626
$data = $post->toArray();
@@ -29,99 +29,99 @@ public function getPosts()
2929
unset($data['user']); // Remove the user relationship to avoid redundancy
3030
return $data;
3131
});
32-
32+
3333
return $this->data(compact('postData'));
3434
}
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-
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+
5858

5959
public function createPost(PostRequest $request)
60-
{
60+
{
6161
// Check if either 'content' or 'file_path' is provided
6262
if (!$request->filled('content') && !$request->hasFile('file_path')) {
6363
return $this->errorMessage([], 'Either content or an image must be provided', 422);
6464
}
65-
65+
6666
// Create a new post
6767
$post = new Post;
68-
$post->user_id = Auth::id();
69-
68+
$post->user_id = Auth::id();
69+
7070
// Set content if provided
7171
if ($request->filled('content')) {
7272
$post->content = $request->input('content');
7373
}
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-
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+
8383
$post->save();
8484
return $this->successMessage('Post created successfully', 201);
8585
}
86-
86+
8787
public function editPost(PostRequest $request, $id)
88-
{
88+
{
8989
$post = Post::find($id);
9090
if (!$post) {
9191
return $this->errorMessage([], 'Post not found', 404);
9292
}
93-
93+
9494
if ($post->user_id !== Auth::id()) {
9595
return $this->errorMessage([], 'You are not authorized to edit this post', 403);
9696
}
97-
97+
9898
// Check if there are any changes to update
9999
if (!$request->filled('content') && !$request->hasFile('file_path')) {
100100
return $this->errorMessage([], 'No changes to update', 422);
101101
}
102-
102+
103103
// Update the post content if provided
104104
if ($request->filled('content')) {
105105
$post->content = $request->input('content');
106106
}
107-
107+
108108
// 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-
}
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+
}
114114

115115
$post->save();
116-
116+
117117
if ($request->filled('content') || $request->hasFile('file_path')) {
118118
return $this->successMessage('Post updated successfully', 200);
119119
} else {
120120
return $this->errorMessage([], 'No changes to update', 422);
121121
}
122122
}
123123

124-
124+
125125
public function deletePost($id)
126126
{
127127
$post = Post::find($id);
@@ -137,7 +137,7 @@ public function deletePost($id)
137137
if ($post->image_path) {
138138
$this->delete($post->image_path);
139139
}
140-
140+
141141
$post->delete();
142142
return $this->successMessage('Post deleted successfully', 200);
143143
}
@@ -146,14 +146,14 @@ public function getUserPosts($userId)
146146
{
147147
// Find the user by their ID
148148
$user = User::find($userId);
149-
149+
150150
if (!$user) {
151151
return $this->errorMessage([], 'User not found', 404);
152152
}
153-
153+
154154
// Retrieve all posts for the user with the user relationship
155155
$posts = $user->posts()->orderBy('created_at', 'desc')->get();
156-
156+
157157
// Transform the posts data to include user_name and user_imageUrl
158158
$postData = $posts->map(function ($post) {
159159
$data = $post->toArray();
@@ -162,10 +162,7 @@ public function getUserPosts($userId)
162162
unset($data['user']); // Remove the user relationship to avoid redundancy
163163
return $data;
164164
});
165-
165+
166166
return $this->data(compact('postData'));
167167
}
168-
169-
170-
171168
}

0 commit comments

Comments
 (0)