cms-frontend/app/Http/Controllers/Api/TopUpSettingController.php

120 lines
3.9 KiB
PHP

<?php
namespace App\Http\Controllers\Api;
use App\Http\Controllers\Controller;
use App\Services\Api\ApiService;
use Illuminate\Http\Request;
class TopUpSettingController extends Controller
{
protected ApiService $apiService;
public function __construct(ApiService $apiService)
{
$this->apiService = $apiService;
}
public function index()
{
try {
$response = $this->apiService->get('/top-up-settings');
if ($response->successful()) {
return view('pages.top-up-settings', [
'settings' => $response->json()['data']
]);
}
return back()->with('error', 'Unable to fetch top-up settings.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function store(Request $request)
{
$validated = $request->validate([
'name' => 'required|string|max:255|unique:top_up_settings,name',
'value' => 'required|numeric|min:0',
'description' => 'required|string',
'type' => 'required|string|in:minimum,maximum,fee,bonus',
'status' => 'required|boolean'
]);
try {
$response = $this->apiService->post('/top-up-settings', $validated);
if ($response->successful()) {
return redirect()->route('top-up-settings')
->with('success', 'Top-up setting created successfully.');
}
return back()->withErrors($response->json()['errors']);
} catch (\Exception $e) {
return back()->with('error', 'Unable to create top-up setting.');
}
}
public function update(Request $request, $id)
{
$validated = $request->validate([
'name' => 'required|string|max:255|unique:top_up_settings,name,'.$id,
'value' => 'required|numeric|min:0',
'description' => 'required|string',
'type' => 'required|string|in:minimum,maximum,fee,bonus',
'status' => 'required|boolean'
]);
try {
$response = $this->apiService->put("/top-up-settings/{$id}", $validated);
if ($response->successful()) {
return redirect()->route('top-up-settings')
->with('success', 'Top-up setting updated successfully.');
}
return back()->withErrors($response->json()['errors']);
} catch (\Exception $e) {
return back()->with('error', 'Unable to update top-up setting.');
}
}
public function destroy($id)
{
try {
$response = $this->apiService->delete("/top-up-settings/{$id}");
if ($response->successful()) {
return redirect()->route('top-up-settings')
->with('success', 'Top-up setting deleted successfully.');
}
return back()->with('error', 'Unable to delete top-up setting.');
} catch (\Exception $e) {
return back()->with('error', 'Service unavailable.');
}
}
public function batchUpdate(Request $request)
{
$validated = $request->validate([
'settings' => 'required|array',
'settings.*.id' => 'required|integer',
'settings.*.value' => 'required|numeric|min:0',
'settings.*.status' => 'required|boolean'
]);
try {
$response = $this->apiService->post('/top-up-settings/batch-update', $validated);
if ($response->successful()) {
return response()->json(['message' => 'Settings updated successfully']);
}
return response()->json(['error' => 'Unable to update settings'], 400);
} catch (\Exception $e) {
return response()->json(['error' => 'Service unavailable'], 503);
}
}
}