|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers\API\Admin; |
| 4 | + |
| 5 | +use App\Http\Controllers\Controller; |
| 6 | +use App\Http\Requests\StorePostRequest; |
| 7 | +use App\Models\Post; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Http\Request; |
| 10 | +use Illuminate\Support\Facades\Validator; |
| 11 | + |
| 12 | +class ManagePostApiController extends Controller |
| 13 | +{ |
| 14 | + public function index() |
| 15 | + { |
| 16 | + $posts = Post::orderBy('id', 'DESC')->get(); |
| 17 | + |
| 18 | + return response()->json([ |
| 19 | + 'success' => true, |
| 20 | + 'data' => $posts |
| 21 | + ], 200); |
| 22 | + } |
| 23 | + |
| 24 | + public function store(Request $request) |
| 25 | + { |
| 26 | + $validator = Validator::make($request->all(), [ |
| 27 | + 'title' => 'required', |
| 28 | + 'description' => 'required', |
| 29 | + 'images' => 'required', |
| 30 | + 'status' => 'required' |
| 31 | + ]); |
| 32 | + |
| 33 | + if ($validator->fails()) { |
| 34 | + return response()->json([ |
| 35 | + 'status' => false, |
| 36 | + 'message' => $validator |
| 37 | + ]); |
| 38 | + } |
| 39 | + |
| 40 | + $validated = $validator->validated(); |
| 41 | + |
| 42 | + $validated = $validator->safe()->except(['title', 'title']); |
| 43 | + $validated = $validator->safe()->except(['description', 'description']); |
| 44 | + $validated = $validator->safe()->except(['images', 'images']); |
| 45 | + $validated = $validator->safe()->except(['status', 'status']); |
| 46 | + |
| 47 | + $input = $request->all(); |
| 48 | + // dd($postimg = $request->file('images')); |
| 49 | + |
| 50 | + if ($postimg = $request->file('images')) { |
| 51 | + $imagepath = public_path('storage/post'); |
| 52 | + $postname = date('YmdHis') . '.' . $postimg->getClientOriginalExtension(); |
| 53 | + $postimg->move($imagepath, $postname); |
| 54 | + $input['images'] = 'storage/post/' . $postname; |
| 55 | + } |
| 56 | + |
| 57 | + // dd($input['images']); |
| 58 | + |
| 59 | + Post::create([ |
| 60 | + 'title' => $input['title'], |
| 61 | + 'description' => $input['description'], |
| 62 | + 'images' => $input['images'], |
| 63 | + 'status' => $input['status'] |
| 64 | + ]); |
| 65 | + |
| 66 | + // dd($input); |
| 67 | + |
| 68 | + return response()->json([ |
| 69 | + 'status' => true, |
| 70 | + 'message' => 'Post created Successfully!', |
| 71 | + 'data' => $input |
| 72 | + ]); |
| 73 | + } |
| 74 | + |
| 75 | + public function update($id, StorePostRequest $request): JsonResponse |
| 76 | + { |
| 77 | + |
| 78 | + // dd($request->hasFile('images')); //false |
| 79 | + $validated = $request->validated(); |
| 80 | + |
| 81 | + $validated = $request->safe()->except(['title', 'description', 'images', 'status']); |
| 82 | + |
| 83 | + $updateInput = $request->all(); |
| 84 | + // dd($request->file('images')); |
| 85 | + |
| 86 | + if ($updatePostImg = $request->file('images')) { |
| 87 | + $updatedPath = public_path('storage/post/updatedimg'); |
| 88 | + $postName = 'updateimg' . date('YmdHis') . '.' . $updatePostImg->getClientOriginalExtension(); |
| 89 | + $updatePostImg->move($updatedPath, $postName); |
| 90 | + $updateInput['images'] = 'storage/post/updatedimg/' . $postName; |
| 91 | + } |
| 92 | + // dd($updatePostImg = $request->file('images')); |
| 93 | + |
| 94 | + $updatePost = Post::findOrFail($id); |
| 95 | + // dd($updatePost); |
| 96 | + $updatePost->update($updateInput); |
| 97 | + // dd($updatePost->update($updateInput)); |
| 98 | + |
| 99 | + return response()->json([ |
| 100 | + 'success' => true, |
| 101 | + 'message' => 'Post Updated Successfully!', |
| 102 | + 'data' => $updatePost, |
| 103 | + ], 200); |
| 104 | + } |
| 105 | + |
| 106 | + public function show($id) |
| 107 | + { |
| 108 | + $findpost = Post::find($id); |
| 109 | + if(!$findpost) |
| 110 | + { |
| 111 | + return response()->json([ |
| 112 | + 'status' => false, |
| 113 | + 'message' => 'Post Not Found' |
| 114 | + ]); |
| 115 | + } |
| 116 | + |
| 117 | + $findpost->first(); |
| 118 | + |
| 119 | + return response()->json([ |
| 120 | + 'status' => true, |
| 121 | + 'message' => 'Post founded successfully!', |
| 122 | + 'data' => $findpost |
| 123 | + ]); |
| 124 | + } |
| 125 | + |
| 126 | + public function delete($id) |
| 127 | + { |
| 128 | + $finddelete = Post::find($id); |
| 129 | + |
| 130 | + if(!$finddelete) |
| 131 | + { |
| 132 | + return response()->json([ |
| 133 | + 'status' => false, |
| 134 | + 'message' => 'Post Not found!' |
| 135 | + ]); |
| 136 | + } |
| 137 | + |
| 138 | + $finddelete->delete(); |
| 139 | + |
| 140 | + return response()->json([ |
| 141 | + 'status' => true, |
| 142 | + 'message' => 'Post Deleted Successfully!' |
| 143 | + ]); |
| 144 | + } |
| 145 | +} |
0 commit comments