Skip to content

Commit 4035e22

Browse files
committed
Changes in the Form Request class and API.
1 parent 843b109 commit 4035e22

File tree

4 files changed

+81
-138
lines changed

4 files changed

+81
-138
lines changed

app/Exceptions/Handler.php

+12-24
Original file line numberDiff line numberDiff line change
@@ -3,46 +3,34 @@
33
namespace App\Exceptions;
44

55
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
6+
use Illuminate\Validation\ValidationException;
67
use Throwable;
78

89
class Handler extends ExceptionHandler
910
{
10-
/**
11-
* A list of exception types with their corresponding custom log levels.
12-
*
13-
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
14-
*/
15-
protected $levels = [
16-
//
17-
];
11+
protected $levels = [];
1812

19-
/**
20-
* A list of the exception types that are not reported.
21-
*
22-
* @var array<int, class-string<\Throwable>>
23-
*/
24-
protected $dontReport = [
25-
//
26-
];
13+
protected $dontReport = [];
2714

28-
/**
29-
* A list of the inputs that are never flashed to the session on validation exceptions.
30-
*
31-
* @var array<int, string>
32-
*/
3315
protected $dontFlash = [
3416
'current_password',
3517
'password',
3618
'password_confirmation',
3719
];
3820

39-
/**
40-
* Register the exception handling callbacks for the application.
41-
*/
4221
public function register(): void
4322
{
4423
$this->reportable(function (Throwable $e) {
4524
//
4625
});
4726
}
27+
28+
// ✅ Add this method
29+
public function invalidJson($request, ValidationException $exception)
30+
{
31+
return response()->json([
32+
'status' => false,
33+
'message' => $exception->errors(),
34+
], $exception->status);
35+
}
4836
}

app/Http/Controllers/API/Admin/ManagePostApiController.php

+40-96
Original file line numberDiff line numberDiff line change
@@ -4,156 +4,100 @@
44

55
use App\Http\Controllers\Controller;
66
use App\Http\Requests\StorePostRequest;
7-
use App\Http\Requests\UpdatePostRequest;
87
use App\Models\Post;
98
use Illuminate\Http\JsonResponse;
109
use Illuminate\Http\Request;
11-
use Illuminate\Support\Facades\Validator;
1210

1311
class ManagePostApiController extends Controller
1412
{
15-
public function index()
13+
public function index(): JsonResponse
1614
{
1715
$posts = Post::orderBy('id', 'DESC')->get();
1816

1917
return response()->json([
2018
'success' => true,
2119
'data' => $posts
22-
], 200);
20+
]);
2321
}
2422

25-
public function store(Request $request)
23+
public function store(StorePostRequest $request): JsonResponse
2624
{
27-
$validator = Validator::make($request->all(), [
28-
'title' => 'required',
29-
'description' => 'required',
30-
'images' => 'required',
31-
'status' => 'required'
32-
]);
33-
34-
if ($validator->fails()) {
35-
return response()->json([
36-
'status' => false,
37-
'message' => $validator
38-
]);
39-
}
40-
41-
$validated = $validator->validated();
42-
43-
$validated = $validator->safe()->except(['title', 'title']);
44-
$validated = $validator->safe()->except(['description', 'description']);
45-
$validated = $validator->safe()->except(['images', 'images']);
46-
$validated = $validator->safe()->except(['status', 'status']);
47-
48-
$input = $request->all();
49-
// dd($postimg = $request->file('images'));
25+
$input = $request->validated();
5026

5127
if ($postimg = $request->file('images')) {
52-
$imagepath = public_path('storage/post');
53-
$postname = date('YmdHis') . '.' . $postimg->getClientOriginalExtension();
54-
$postimg->move($imagepath, $postname);
55-
$input['images'] = 'storage/post/' . $postname;
28+
$imagePath = public_path('storage/post');
29+
$postName = date('YmdHis') . '.' . $postimg->getClientOriginalExtension();
30+
$postimg->move($imagePath, $postName);
31+
$input['images'] = 'storage/post/' . $postName;
5632
}
5733

58-
// dd($input['images']);
59-
60-
Post::create([
61-
'title' => $input['title'],
62-
'description' => $input['description'],
63-
'images' => $input['images'],
64-
'status' => $input['status']
65-
]);
66-
67-
// dd($input);
34+
$post = Post::create($input);
6835

6936
return response()->json([
7037
'status' => true,
71-
'message' => 'Post created Successfully!',
72-
'data' => $input
38+
'message' => 'Post created successfully!',
39+
'data' => $post
7340
]);
7441
}
7542

76-
public function update($id, UpdatePostRequest $request): JsonResponse
43+
public function update($id, StorePostRequest $request)
7744
{
78-
dd($request->all());
79-
$validator = Validator::make($request->all(), [
80-
'title' => 'required',
81-
'description' => 'required',
82-
'images' => 'required',
83-
'status' => 'required'
84-
]);
85-
86-
if ($validator->fails()) {
87-
return response()->json([
88-
'status' => false,
89-
'message' => $validator
90-
]);
91-
}
92-
//false
93-
// $validated = $request->validated();
94-
95-
// $validated = $request->safe()->except(['title', 'description', 'images', 'status']);
96-
97-
$updateInput = $request->all();
98-
// dd($request->file('images'));
99-
100-
if ($updatePostImg = $request->file('images')) {
101-
$updatedPath = public_path('storage/post/updatedimg');
102-
$postName = 'updateimg' . date('YmdHis') . '.' . $updatePostImg->getClientOriginalExtension();
103-
$updatePostImg->move($updatedPath, $postName);
104-
$updateInput['images'] = 'storage/post/updatedimg/' . $postName;
45+
$updatePost = Post::findOrFail($id);
46+
$input = $request->except('_method');
47+
48+
if ($request->hasFile('images')) {
49+
$imagePath = public_path('storage/post/updatedimg');
50+
$imageName = 'updateimg' . date('YmdHis') . '.' . $request->file('images')->getClientOriginalExtension();
51+
$request->file('images')->move($imagePath, $imageName);
52+
$input['images'] = 'storage/post/updatedimg/' . $imageName;
53+
} else {
54+
$input['images'] = $updatePost->images;
10555
}
106-
// dd($updatePostImg = $request->file('images'));
10756

108-
$updatePost = Post::findOrFail($id);
109-
// dd($updatePost);
110-
$updatePost->update($updateInput);
111-
// dd($updatePost->update($updateInput));
57+
$updatePost->update($input);
11258

11359
return response()->json([
114-
'success' => true,
115-
'message' => 'Post Updated Successfully!',
116-
'data' => $updatePost,
60+
'status' => true,
61+
'message' => 'Post updated successfully!',
62+
'data' => $updatePost,
11763
], 200);
11864
}
11965

120-
public function show($id)
66+
67+
public function show($id): JsonResponse
12168
{
122-
$findpost = Post::find($id);
123-
if(!$findpost)
124-
{
69+
$post = Post::find($id);
70+
71+
if (!$post) {
12572
return response()->json([
12673
'status' => false,
127-
'message' => 'Post Not Found'
74+
'message' => 'Post not found!'
12875
]);
12976
}
13077

131-
$findpost->first();
132-
13378
return response()->json([
13479
'status' => true,
135-
'message' => 'Post founded successfully!',
136-
'data' => $findpost
80+
'message' => 'Post found successfully!',
81+
'data' => $post
13782
]);
13883
}
13984

140-
public function delete($id)
85+
public function delete($id): JsonResponse
14186
{
142-
$finddelete = Post::find($id);
87+
$post = Post::find($id);
14388

144-
if(!$finddelete)
145-
{
89+
if (!$post) {
14690
return response()->json([
14791
'status' => false,
148-
'message' => 'Post Not found!'
92+
'message' => 'Post not found!'
14993
]);
15094
}
15195

152-
$finddelete->delete();
96+
$post->delete();
15397

15498
return response()->json([
15599
'status' => true,
156-
'message' => 'Post Deleted Successfully!'
100+
'message' => 'Post deleted successfully!'
157101
]);
158102
}
159103
}

app/Http/Requests/StorePostRequest.php

+22-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace App\Http\Requests;
44

55
use Illuminate\Foundation\Http\FormRequest;
6-
use Illuminate\Support\Arr;
76

87
class StorePostRequest extends FormRequest
98
{
@@ -17,29 +16,39 @@ public function authorize(): bool
1716

1817
/**
1918
* Get the validation rules that apply to the request.
20-
*
21-
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
2219
*/
2320
public function rules(): array
2421
{
25-
26-
return [
27-
// Custome rule here:
28-
29-
'title' => 'required',
30-
'description' => 'required',
31-
'images' => 'required|nullable',
32-
'status' => 'required'
22+
$rules = [
23+
'title' => 'required|string|max:255',
24+
'description' => 'required|string',
25+
'status' => 'required|in:Active,Inactive,Draft',
3326
];
27+
28+
if ($this->isMethod('post')) {
29+
$rules['images'] = 'required|image|mimes:jpg,jpeg,png|max:2048';
30+
} else {
31+
$rules['images'] = 'nullable|image|mimes:jpg,jpeg,png|max:2048';
32+
}
33+
34+
return $rules;
3435
}
3536

37+
38+
/**
39+
* Custom error messages for validation rules.
40+
*/
3641
public function messages(): array
3742
{
3843
return [
3944
'title.required' => 'Title is required!',
40-
'description.required' => 'Description is required',
45+
'description.required' => 'Description is required.',
4146
'images.required' => 'Please select an image.',
42-
'status.required' => 'Status is required'
47+
'images.image' => 'The uploaded file must be an image.',
48+
'images.mimes' => 'Image must be a file of type: jpeg, png, jpg, gif.',
49+
'images.max' => 'Image size should not exceed 2MB.',
50+
'status.required' => 'Status is required.',
51+
'status.in' => 'Invalid status. Must be one of: Active, Inactive, Draft.'
4352
];
4453
}
4554
}

routes/api.php

+7-5
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,13 @@
2727
Route::post('register',[AuthController::class,'register']);
2828
Route::post('login', [AuthController::class, 'login']);
2929

30-
Route::get('blogs',[ManagePostApiController::class, 'index']);
31-
Route::post('blogs/create', [ManagePostApiController::class, 'store']);
32-
Route::put('blogs/update/{id}', [ManagePostApiController::class, 'update']);
33-
Route::get('blogs/show/{id}', [ManagePostApiController::class, 'show']);
34-
Route::delete('blogs/delete/{id}', [ManagePostApiController::class, 'delete']);
30+
Route::prefix('blogs')->controller(ManagePostApiController::class)->group(function(){
31+
Route::get('/', 'index');
32+
Route::post('/create', 'store');
33+
Route::put('/update/{id}', 'update');
34+
Route::get('/show/{id}','show');
35+
Route::delete('/delete/{id}', 'delete');
36+
});
3537

3638
Route::middleware('auth:sanctum')->group(function(){
3739

0 commit comments

Comments
 (0)