|
4 | 4 |
|
5 | 5 | use App\Http\Controllers\Controller;
|
6 | 6 | use App\Http\Requests\StorePostRequest;
|
7 |
| -use App\Http\Requests\UpdatePostRequest; |
8 | 7 | use App\Models\Post;
|
9 | 8 | use Illuminate\Http\JsonResponse;
|
10 | 9 | use Illuminate\Http\Request;
|
11 |
| -use Illuminate\Support\Facades\Validator; |
12 | 10 |
|
13 | 11 | class ManagePostApiController extends Controller
|
14 | 12 | {
|
15 |
| - public function index() |
| 13 | + public function index(): JsonResponse |
16 | 14 | {
|
17 | 15 | $posts = Post::orderBy('id', 'DESC')->get();
|
18 | 16 |
|
19 | 17 | return response()->json([
|
20 | 18 | 'success' => true,
|
21 | 19 | 'data' => $posts
|
22 |
| - ], 200); |
| 20 | + ]); |
23 | 21 | }
|
24 | 22 |
|
25 |
| - public function store(Request $request) |
| 23 | + public function store(StorePostRequest $request): JsonResponse |
26 | 24 | {
|
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(); |
50 | 26 |
|
51 | 27 | 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; |
56 | 32 | }
|
57 | 33 |
|
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); |
68 | 35 |
|
69 | 36 | return response()->json([
|
70 | 37 | 'status' => true,
|
71 |
| - 'message' => 'Post created Successfully!', |
72 |
| - 'data' => $input |
| 38 | + 'message' => 'Post created successfully!', |
| 39 | + 'data' => $post |
73 | 40 | ]);
|
74 | 41 | }
|
75 | 42 |
|
76 |
| - public function update($id, UpdatePostRequest $request): JsonResponse |
| 43 | + public function update($id, StorePostRequest $request) |
77 | 44 | {
|
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; |
105 | 55 | }
|
106 |
| - // dd($updatePostImg = $request->file('images')); |
107 | 56 |
|
108 |
| - $updatePost = Post::findOrFail($id); |
109 |
| - // dd($updatePost); |
110 |
| - $updatePost->update($updateInput); |
111 |
| - // dd($updatePost->update($updateInput)); |
| 57 | + $updatePost->update($input); |
112 | 58 |
|
113 | 59 | 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, |
117 | 63 | ], 200);
|
118 | 64 | }
|
119 | 65 |
|
120 |
| - public function show($id) |
| 66 | + |
| 67 | + public function show($id): JsonResponse |
121 | 68 | {
|
122 |
| - $findpost = Post::find($id); |
123 |
| - if(!$findpost) |
124 |
| - { |
| 69 | + $post = Post::find($id); |
| 70 | + |
| 71 | + if (!$post) { |
125 | 72 | return response()->json([
|
126 | 73 | 'status' => false,
|
127 |
| - 'message' => 'Post Not Found' |
| 74 | + 'message' => 'Post not found!' |
128 | 75 | ]);
|
129 | 76 | }
|
130 | 77 |
|
131 |
| - $findpost->first(); |
132 |
| - |
133 | 78 | return response()->json([
|
134 | 79 | 'status' => true,
|
135 |
| - 'message' => 'Post founded successfully!', |
136 |
| - 'data' => $findpost |
| 80 | + 'message' => 'Post found successfully!', |
| 81 | + 'data' => $post |
137 | 82 | ]);
|
138 | 83 | }
|
139 | 84 |
|
140 |
| - public function delete($id) |
| 85 | + public function delete($id): JsonResponse |
141 | 86 | {
|
142 |
| - $finddelete = Post::find($id); |
| 87 | + $post = Post::find($id); |
143 | 88 |
|
144 |
| - if(!$finddelete) |
145 |
| - { |
| 89 | + if (!$post) { |
146 | 90 | return response()->json([
|
147 | 91 | 'status' => false,
|
148 |
| - 'message' => 'Post Not found!' |
| 92 | + 'message' => 'Post not found!' |
149 | 93 | ]);
|
150 | 94 | }
|
151 | 95 |
|
152 |
| - $finddelete->delete(); |
| 96 | + $post->delete(); |
153 | 97 |
|
154 | 98 | return response()->json([
|
155 | 99 | 'status' => true,
|
156 |
| - 'message' => 'Post Deleted Successfully!' |
| 100 | + 'message' => 'Post deleted successfully!' |
157 | 101 | ]);
|
158 | 102 | }
|
159 | 103 | }
|
0 commit comments