|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Controllers; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use App\Models\Gallery; |
| 7 | +use Illuminate\Http\Request; |
| 8 | +use Illuminate\Http\JsonResponse; |
| 9 | +use Illuminate\Support\Facades\Storage; |
| 10 | +use Illuminate\Support\Facades\Validator; |
| 11 | + |
| 12 | +class GalleryController extends Controller |
| 13 | +{ |
| 14 | + /** |
| 15 | + * Display a listing of the resource. |
| 16 | + */ |
| 17 | + public function index(Request $request) |
| 18 | + { |
| 19 | + try |
| 20 | + { |
| 21 | + $perPage = $request->get('showing', 10); |
| 22 | + $search = $request->get('search', ''); |
| 23 | + |
| 24 | + $data = Gallery::where(function($query) use ($search) { |
| 25 | + $query->where('name_gallery', 'LIKE', "%{$search}%"); |
| 26 | + $query->orWhere('description_gallery', 'LIKE', "%{$search}%"); |
| 27 | + })->latest()->paginate($perPage); |
| 28 | + |
| 29 | + return response()->json([ |
| 30 | + 'data' => $data, |
| 31 | + 'success' => true, |
| 32 | + ], JsonResponse::HTTP_OK); |
| 33 | + } |
| 34 | + catch (Exception $e) |
| 35 | + { |
| 36 | + return response()->json([ |
| 37 | + 'data' => [], |
| 38 | + 'success' => false, |
| 39 | + 'message' => $e->getMessage() |
| 40 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Store a newly created resource in storage. |
| 46 | + */ |
| 47 | + public function store(Request $request) |
| 48 | + { |
| 49 | + $validatedData = Validator::make($request->all(), [ |
| 50 | + 'name_gallery' => 'required', |
| 51 | + 'description_gallery' => '', |
| 52 | + 'image' => 'image|mimes:jpg,jpeg,png|max:3072' |
| 53 | + ]); |
| 54 | + |
| 55 | + try |
| 56 | + { |
| 57 | + if ($validatedData->fails()){ |
| 58 | + return response()->json(['success' => false, 'message' => $validatedData->errors()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 59 | + } |
| 60 | + |
| 61 | + if ($request->hasFile('image')) { |
| 62 | + $image = $request->file('image'); |
| 63 | + $filename = time() . '_' . $image->getClientOriginalName(); |
| 64 | + $image->storeAs('public/images', $filename); |
| 65 | + } |
| 66 | + |
| 67 | + $data = Gallery::create([ |
| 68 | + 'name_gallery' => $request->input('name_gallery'), |
| 69 | + 'description_gallery' => $request->input('description_gallery'), |
| 70 | + 'image' => $filename |
| 71 | + ]); |
| 72 | + |
| 73 | + return response()->json([ |
| 74 | + 'data' => $data, |
| 75 | + 'success' => true, |
| 76 | + 'message' => 'Data created successfully' |
| 77 | + ], JsonResponse::HTTP_CREATED); |
| 78 | + } |
| 79 | + catch (Exception $e) |
| 80 | + { |
| 81 | + return response()->json([ |
| 82 | + 'data' => [], |
| 83 | + 'success' => false, |
| 84 | + 'message' => $e->getMessage() |
| 85 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Display the specified resource. |
| 91 | + */ |
| 92 | + public function show($id) |
| 93 | + { |
| 94 | + try |
| 95 | + { |
| 96 | + $data = Gallery::findOrFail($id); |
| 97 | + |
| 98 | + return response()->json([ |
| 99 | + 'data' => $data, |
| 100 | + 'success' => true, |
| 101 | + ], JsonResponse::HTTP_OK); |
| 102 | + } |
| 103 | + catch (Exception $e) |
| 104 | + { |
| 105 | + return response()->json([ |
| 106 | + 'data' => [], |
| 107 | + 'success' => false, |
| 108 | + 'message' => $e->getMessage() |
| 109 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Show the form for editing the specified resource. |
| 115 | + */ |
| 116 | + public function edit($id) |
| 117 | + { |
| 118 | + try |
| 119 | + { |
| 120 | + $data = Gallery::findOrFail($id); |
| 121 | + |
| 122 | + return response()->json([ |
| 123 | + 'data' => $data, |
| 124 | + 'success' => true, |
| 125 | + ], JsonResponse::HTTP_OK); |
| 126 | + } |
| 127 | + catch (Exception $e) |
| 128 | + { |
| 129 | + return response()->json([ |
| 130 | + 'data' => [], |
| 131 | + 'success' => false, |
| 132 | + 'message' => $e->getMessage() |
| 133 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Update the specified resource in storage. |
| 139 | + */ |
| 140 | + public function update(Request $request, $id) |
| 141 | + { |
| 142 | + try |
| 143 | + { |
| 144 | + $data = Gallery::findOrFail($id); |
| 145 | + $filename = $data->image; |
| 146 | + |
| 147 | + if ($request->hasFile('image')) { |
| 148 | + $validatedData = Validator::make($request->all(), [ |
| 149 | + 'name_gallery' => 'required', |
| 150 | + 'image' => 'image|mimes:jpg,jpeg,png|max:3072' |
| 151 | + ]); |
| 152 | + |
| 153 | + if ($validatedData->fails()){ |
| 154 | + return response()->json(['success' => false, 'message' => $validatedData->errors()], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 155 | + } |
| 156 | + |
| 157 | + Storage::delete('public/images/' . $filename); |
| 158 | + |
| 159 | + $image = $request->file('image'); |
| 160 | + $filename = time() . '_' . $image->getClientOriginalName(); |
| 161 | + $image->storeAs('public/images', $filename); |
| 162 | + } |
| 163 | + |
| 164 | + $data->update([ |
| 165 | + 'name_gallery' => $request->input('name_gallery'), |
| 166 | + 'description_gallery' => $request->input('description_gallery'), |
| 167 | + 'image' => $filename |
| 168 | + ]); |
| 169 | + |
| 170 | + return response()->json([ |
| 171 | + 'data' => $data, |
| 172 | + 'success' => true, |
| 173 | + 'message' => 'Data updated successfully' |
| 174 | + ], JsonResponse::HTTP_OK); |
| 175 | + } |
| 176 | + catch (Exception $e) |
| 177 | + { |
| 178 | + return response()->json([ |
| 179 | + 'data' => [], |
| 180 | + 'success' => false, |
| 181 | + 'message' => $e->getMessage() |
| 182 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * Remove the specified resource from storage. |
| 188 | + */ |
| 189 | + public function destroy($id) |
| 190 | + { |
| 191 | + try |
| 192 | + { |
| 193 | + $data = Gallery::findOrFail($id); |
| 194 | + |
| 195 | + if ($data->image) { |
| 196 | + Storage::delete('public/images/' . $data->image); |
| 197 | + } |
| 198 | + |
| 199 | + $data->delete(); |
| 200 | + |
| 201 | + return response()->json([ |
| 202 | + 'data' => $data, |
| 203 | + 'success' => true, |
| 204 | + 'message' => 'Data deleted successfully' |
| 205 | + ], JsonResponse::HTTP_OK); |
| 206 | + } |
| 207 | + catch (Exception $e) |
| 208 | + { |
| 209 | + return response()->json([ |
| 210 | + 'data' => [], |
| 211 | + 'success' => false, |
| 212 | + 'message' => $e->getMessage() |
| 213 | + ], JsonResponse::HTTP_INTERNAL_SERVER_ERROR); |
| 214 | + } |
| 215 | + } |
| 216 | +} |
0 commit comments