<?php
namespace App\Http\Controllers;
use App\Helpers\Helper;
use App\Models\IntegrasiWhatsapp;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class IntegrasiWhatsappController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
$datas = IntegrasiWhatsapp::orderBy('created_at','DESC')->paginate(10);
return response()->view('integrasi-whatsapp.index', compact('datas'));
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
$providers = IntegrasiWhatsapp::providerList();
return view('integrasi-whatsapp.create',compact('providers'));
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
$request->validate([
'provider'=>'required',
'api_key'=>'required',
]);
DB::beginTransaction();
try {
if($request->used){
IntegrasiWhatsapp::where('used',1)->update(['used'=>0]);
}
$integrasi = IntegrasiWhatsapp::create([
'provider'=>$request->provider,
'api_key'=>$request->api_key,
'used'=>$request->used ? 1 : 0
]);
Helper::addUserLog('menambah integrasi whatsapp '.$request->provider);
DB::commit();
return redirect()->route('integrasi-whatsapp.index')->with('success','Berhasil menambah integrasi whatsapp');
} catch (\Throwable $th) {
//throw $th;
DB::rollBack();
Log::error($th);
return redirect()->back()->with('error','Gagal menambah integrasi whatsapp')->withInput($request->all());
}
}
/**
* Display the specified resource.
*/
public function show(IntegrasiWhatsapp $integrasiWhatsapp)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(IntegrasiWhatsapp $integrasiWhatsapp)
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
$providers = IntegrasiWhatsapp::providerList();
return view('integrasi-whatsapp.edit',[
'providers'=>$providers,
'data'=>$integrasiWhatsapp
]);
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, IntegrasiWhatsapp $integrasiWhatsapp)
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
if($request->update_used){
IntegrasiWhatsapp::where('used',1)->update(['used'=>0]);
$integrasiWhatsapp->update([
'used'=>$request->used ? 1 : 0
]);
return redirect()->route('integrasi-whatsapp.index');
}
$request->validate([
'provider'=>'required',
'api_key'=>'required',
]);
DB::beginTransaction();
try {
if($request->used){
IntegrasiWhatsapp::where('used',1)->update(['used'=>0]);
}
$integrasiWhatsapp->update([
'provider'=>$request->provider,
'api_key'=>$request->api_key,
'used'=>$request->used ? 1 : 0
]);
Helper::addUserLog('mengubah integrasi whatsapp '.$request->provider);
DB::commit();
return redirect()->route('integrasi-whatsapp.index')->with('success','Berhasil mengubah integrasi whatsapp');
} catch (\Throwable $th) {
//throw $th;
DB::rollBack();
Log::error($th);
return redirect()->back()->with('error','Gagal mengubah integrasi whatsapp')->withInput($request->all());
}
}
/**
* Remove the specified resource from storage.
*/
public function destroy(IntegrasiWhatsapp $integrasiWhatsapp)
{
if (!Helper::hakAkses('pengaturan', 'view')) {
return abort(404);
}
DB::beginTransaction();
try {
$integrasiWhatsapp->delete();
Helper::addUserLog('menghapus integrasi whatsapp '.$integrasiWhatsapp->provider);
DB::commit();
return redirect()->route('integrasi-whatsapp.index')->with('success','Berhasil menghapus integrasi whatsapp');
} catch (\Throwable $th) {
//throw $th;
DB::rollBack();
Log::error($th);
return redirect()->back()->with('error','Gagal menghapus integrasi whatsapp');
}
}
}
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]