121 lines
3.4 KiB
PHP
121 lines
3.4 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Http\Requests\DataSettingRequest;
|
|
use App\Models\DataSetting;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Log;
|
|
use Illuminate\Support\Facades\Request;
|
|
|
|
class DataSettingController extends Controller
|
|
{
|
|
/**
|
|
* Display a listing of the resource.
|
|
*/
|
|
public function index()
|
|
{
|
|
return view("data-settings.index");
|
|
}
|
|
|
|
/**
|
|
* Show the form for creating a new resource.
|
|
*/
|
|
public function create()
|
|
{
|
|
return view("data-settings.create");
|
|
}
|
|
|
|
/**
|
|
* Store a newly created resource in storage.
|
|
*/
|
|
public function store(DataSettingRequest $request)
|
|
{
|
|
try{
|
|
DB::beginTransaction();
|
|
DataSetting::create($request->validated());
|
|
DB::commit();
|
|
return redirect()->route("data-settings.index")->with("success","Successfully created");
|
|
}catch(Exception $ex){
|
|
DB::rollBack();
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('error', 'Something went wrong while saving data. ' . $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Display the specified resource.
|
|
*/
|
|
public function show(DataSetting $dataSetting)
|
|
{
|
|
//
|
|
}
|
|
|
|
/**
|
|
* Show the form for editing the specified resource.
|
|
*/
|
|
public function edit(string $id)
|
|
{
|
|
try{
|
|
$data = DataSetting::findOrFail($id);
|
|
if(empty($data)){
|
|
return redirect()->route('data-settings.index')->with('error', 'Invalid id');
|
|
}
|
|
return view("data-settings.edit", compact("data"));
|
|
}catch(Exception $ex){
|
|
return redirect()->route("data-settings.index")->with("error", "Invalid id");
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update the specified resource in storage.
|
|
*/
|
|
public function update(DataSettingRequest $request,string $id)
|
|
{
|
|
try{
|
|
DB::beginTransaction();
|
|
$data = DataSetting::findOrFail($id);
|
|
$data->update($request->validated());
|
|
DB::commit();
|
|
return redirect()->route("data-settings.index")->with("success","Successfully updated");
|
|
}catch(Exception $ex){
|
|
DB::rollBack();
|
|
return redirect()->back()
|
|
->withInput()
|
|
->with('error', 'Something went wrong while saving data. ' . $ex->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove the specified resource from storage.
|
|
*/
|
|
public function destroy(string $id)
|
|
{
|
|
try{
|
|
DB::beginTransaction();
|
|
DataSetting::findOrFail($id)->delete();
|
|
DB::commit();
|
|
return response()->json(['success' => true, 'message' => 'Item deleted successfully.']);
|
|
}catch(Exception $e){
|
|
DB::rollBack();
|
|
Log::error($e->getMessage());
|
|
return response()->json(['success' => false, 'message' => 'Failed to delete item.'], 500);
|
|
}
|
|
}
|
|
|
|
public function getValueSetting(Request $request){
|
|
try{
|
|
$data = DataSetting::where('key', $request->key_name)->first();
|
|
return response()->json([
|
|
'success' => true,
|
|
'message' => "Successfully retrieved data",
|
|
"data"=> $data
|
|
]);
|
|
}catch(Exception $e){
|
|
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
}
|