diff --git a/app/Http/Controllers/Admin/TourController.php b/app/Http/Controllers/Admin/TourController.php new file mode 100644 index 0000000..ccb4d6f --- /dev/null +++ b/app/Http/Controllers/Admin/TourController.php @@ -0,0 +1,247 @@ +with('file') + + ->when($request->filled('search'), function (Builder $query) use ($request) { + + $search = $request->input('search'); + + $query->whereAny([ + 'title', + 'summary', + 'description', + 'origin', + 'destination', + ], 'LIKE', "%$search%"); + + }) + + ->when($request->filled('sort'), function (Builder $query) use ($request) { + + switch ($request->input('sort')) { + + case 'price_asc': + $query->orderBy('price'); + break; + + case 'price_desc': + $query->orderByDesc('price'); + break; + + case 'duration_asc': + $query->orderBy('duration'); + break; + + case 'duration_desc': + $query->orderByDesc('duration'); + break; + + case 'created_at': + $query->orderByDesc('created_at'); + break; + } + + }, function (Builder $query) { + + $query->orderByDesc('created_at'); + + }) + + ->paginate(); + + return view('admin.tours.index', compact('tours')); + } + + + public function create() + { + return view('admin.tours.create'); + } + + + public function store(tourStoreRequest $request) + { + try { + + DB::beginTransaction(); + + $fileId = null; + + if ($request->hasFile('image')) { + + $file = $request->file('image'); + + $path = $file->store('tours', 'public'); + + $uploadedFile = File::create([ + 'name' => pathinfo( + $file->hashName(), + PATHINFO_FILENAME + ), + + 'extension' => $file->getClientOriginalExtension(), + + 'size' => $file->getSize(), + + 'original_name' => $file->getClientOriginalName(), + + 'path' => $path, + ]); + + $fileId = $uploadedFile->id; + } + + Tour::create([ + 'title' => $request->input('title'), + + 'summary' => $request->input('summary'), + + 'description' => $request->input('description'), + + 'origin' => $request->input('origin'), + + 'destination' => $request->input('destination'), + + 'file_id' => $fileId, + + 'duration' => $request->input('duration'), + + 'views' => 0, + + 'price' => $request->input('price'), + + 'status' => $request->input('status'), + ]); + + DB::commit(); + + } catch (\Exception $exception) { + + DB::rollBack(); + + Log::error($exception); + + return back() + ->withErrors([ + 'general' => 'خطایی رخ داده است' + ]) + ->withInput(); + } + + return redirect() + ->route('admin.tours.index') + ->withErrors([ + 'success' => 'تور با موفقیت ایجاد شد' + ]); + } + + + public function edit(Tour $tour) + { + $tour->load('file'); + + return view('admin.tours.edit', compact('tour')); + } + + + public function update(tourUpdateRequest $request, Tour $tour) { + try { + + DB::beginTransaction(); + + $fileId = $tour->file_id; + + if ($request->hasFile('image')) { + + $file = $request->file('image'); + + $path = $file->store('tours', 'public'); + + $uploadedFile = File::create([ + 'name' => pathinfo( + $file->hashName(), + PATHINFO_FILENAME + ), + + 'extension' => $file->getClientOriginalExtension(), + + 'size' => $file->getSize(), + + 'original_name' => $file->getClientOriginalName(), + + 'path' => $path, + ]); + + $fileId = $uploadedFile->id; + } + + $tour->update([ + 'title' => $request->input('title'), + + 'summary' => $request->input('summary'), + + 'description' => $request->input('description'), + + 'origin' => $request->input('origin'), + + 'destination' => $request->input('destination'), + + 'file_id' => $fileId, + + 'duration' => $request->input('duration'), + + 'price' => $request->input('price'), + + 'status' => $request->input('status'), + ]); + + DB::commit(); + + } catch (\Exception $exception) { + + DB::rollBack(); + + Log::error($exception); + + return back() + ->withErrors([ + 'general' => 'خطایی رخ داده است' + ]) + ->withInput(); + } + + return redirect() + ->route('admin.tours.index') + ->withErrors([ + 'success' => 'تور با موفقیت ویرایش شد' + ]); + } + + + public function destroy(Tour $tour) + { + $tour->delete(); + + return redirect() + ->route('admin.tours.index') + ->withErrors([ + 'success' => 'تور با موفقیت حذف شد' + ]); + } +}