<?php
namespace App\Http\Controllers;
use App\Classes\TransactionGeneration;
use App\ServiceConfig;
use App\ServiceConfigAccount;
use App\TypeService;
use Illuminate\Http\Request;
class ServiceConfigController extends Controller
{
protected $transaction;
public function __construct()
{
$this->transaction = new TransactionGeneration();
}
public function index()
{
$data = ServiceConfig::query()
->with('serviceAccounts')->get();
return response()->json(['data' => $data]);
}
public function store(Request $request)
{
$values = $this->validate($request, [
'name' => 'required|string:100',
'description' => 'required|string',
'notification_group' => 'required|string',
'notification_type' => 'required|string',
'min_amount' => 'required',
'max_amount' => 'required',
'notification_time' => 'required|int',
]);
if ($request->min_amount > $request->max_amount) {
return response()->json(['message' => trans('min_value_up_max_value')], 400);
} else {
ServiceConfig::create([
'public_id' => $this->transaction->generateID(),
'name' => $values['name'],
'description' => $values['description'],
'min_amount' => $values['min_amount'],
'max_amount' => $values['max_amount'],
'notification_time' => $values['notification_time'],
'notification_group' => strtolower($values['notification_group']),
'notification_type' => strtolower($values['notification_type']),
]);
return response()->json(['message' => trans('done')]);
}
}
public function edit($id): \Illuminate\Http\JsonResponse
{
$type = ServiceConfig::where('public_id', '=', $id)
->with('serviceAccounts')->first();
if ($type) {
return response()->json($type);
} else {
return response()->json(['message' => trans('not_found')], 400);
}
}
public function update(Request $request, $id)
{
$service = ServiceConfig::where('public_id', '=', $id)->first();
if ($service) {
$values = $this->validate($request, [
'name' => 'required|string:100',
'description' => 'required|string',
'min_amount' => 'required',
'max_amount' => 'required',
'status' => 'required',
'notification_time' => 'required|int',
'notification_group' => 'required|string',
'notification_type' => 'required|string',
]);
if ($values['min_amount'] > $values['max_amount']) {
return response()->json(['message' => trans('min_value_up_max_value')], 400);
} else {
ServiceConfig::where('public_id', '=', $id)
->update([
'name' => $values['name'],
'status' => $values['status'],
'description' => $values['description'],
'min_amount' => $values['min_amount'],
'max_amount' => $values['max_amount'],
'notification_time' => $values['notification_time'],
'notification_group' => strtolower($values['notification_group']),
'notification_type' => strtolower($values['notification_type']),
]);
return response()->json(['message' => trans('done')]);
}
} else {
return response()->json(['message' => trans('not_found')], 400);
}
}
public function destroy($id): \Illuminate\Http\JsonResponse
{
$type = ServiceConfig::where('public_id', '=', $id)->first();
if ($type) {
$account = ServiceConfigAccount::where('service_config_id', '=', $type->id)->first();
if ($account) {
return response()->json(['message' => trans('cannot_delete')], 400);
} else {
$type->delete();
return response()->json(['message' => trans('done')]);
}
} else
return response()->json(['message' => trans('not_found')], 400);
}
}