Files
cheerstothe_season_2.0/app/Http/Controllers/Admin/ManageLocationController.php
2024-06-28 19:48:56 +05:30

151 lines
4.4 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\ManageState;
use App\Models\TimeInterval;
use Illuminate\Http\Request;
use Exception;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
class ManageLocationController extends Controller
{
/*
Created By : Sayali parab
Created at : 07 June 2024
Use : To get the page.
*/
public function index()
{
$location = ManageState::with('timeInterval')->orderBy('id', 'asc')->get();
return view('Admin.pages.manage_states.manage_states', compact('location'));
}
/*
Created By : Sayali parab
Created at : 07 June 2024
Use : To store the location.
*/
public function store(Request $request)
{
// dd($request);
$request->validate([
'location_name' => 'required|string|max:255',
'timeHours' => 'required|numeric|min:1',
'timeInterval' => 'required|in:day,week,month'
]);
try {
if (ManageState::where('name', $request->location_name)->exists()) {
return response()->json(['success' => false, 'error' => 'Location name already exists', 'status' => 422]);
}
// new location
$location = new ManageState();
$location->name = $request->location_name;
$location->save();
// time interval
$timeInterval = new TimeInterval();
$timeInterval->manage_state_xid = $location->id;
$timeInterval->time_hours = $request->timeHours;
$timeInterval->time_interval = $request->timeInterval;
$timeInterval->quantity = $request->timeQuantity;
$timeInterval->save();
return response()->json(['success' => true, 'status' => 200]);
} catch (\Exception $e) {
return response()->json(['success' => false, 'error' => $e->getMessage(), 'status' => 500]);
}
}
/*
Created By : Sayali parab
Created at : 07 June 2024
Use : To change status.
*/
public function change_location_status(Request $request)
{
try {
DB::beginTransaction();
$status = ManageState::find($request->location_id);
$status->is_active = $request->status;
$status->save();
// return response()->json(['status' => 200]);
DB::commit();
return jsonResponseWithSuccessMessage(__('success.update_data'));
} catch (Exception $e) {
Log::error("Update Status function Load Failed " . $e->getMessage());
return jsonResponseWithErrorMessage(__('auth.something_went_wrong'), 500);
}
}
/*
Created By : Sayali parab
Created at : 07 June 2024
Use : To delete location.
*/
public function delete_location($id)
{
try {
DB::beginTransaction();
$passport = ManageState::find($id);
$passport->delete();
DB::commit();
return response()->json(['success' => true, 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("delete_location function Load Failed " . $e->getMessage());
return response()->json(['success' => false, 'status' => 500, 'message' => __('auth.something_went_wrong')]);
}
}
/*
Created By : Sayali parab
Created at : 07 June 2024
Use : To update location.
*/
public function update(Request $request)
{
try {
DB::beginTransaction();
// Check if the location name already exists, excluding the current location
if (ManageState::where('name', $request->location_name)->where('id', '!=', $request->id)->exists()) {
return response()->json(['success' => false, 'error' => 'Location name already exists', 'status' => 422]);
}
$location_data = ManageState::find($request->id);
$location_data->name = $request->location_name;
$location_data->save();
DB::commit();
return response()->json(['success' => true, 'message' => __('success.update_data'), 'status' => 200]);
} catch (Exception $e) {
DB::rollBack();
Log::error("Failed to update location: " . $e->getMessage());
return response()->json(['success' => false, 'error' => __('auth.something_went_wrong'), 'status' => 500]);
}
}
}