<?php
namespace App\Http\Controllers;
use App\Classes\TransactionGeneration;
use App\ServiceConfig;
use App\ServiceConfigAccount;
use Illuminate\Http\Request;
class ServiceConfigAccountController extends Controller
{
protected $transaction;
public function __construct()
{
$this->transaction = new TransactionGeneration();
}
public function index()
{
$data = ServiceConfigAccount::query()
->with('service')->get();
return response()->json(['data' => $data]);
}
public function store(Request $request)
{
$values = $this->validate($request, [
'master' => 'required|int|max:1',
'phone' => 'required',
'email' => 'required|email',
'service_config_id' => 'required'
]);
$service = ServiceConfig::where('public_id', '=', $values['service_config_id'])->first();
if ($service) {
$serviceAcoount = ServiceConfigAccount::where('service_config_id', '=', $service->id)
->where('phone', '=', $values['phone'])
->where('email', '=', $values['email'])
->first();
if (!$serviceAcoount) {
ServiceConfigAccount::create([
'public_id' => $this->transaction->generateID(),
'master' => $values['master'],
'phone' => $values['phone'],
'email' => $values['email'],
'service_config_id' => $service->id
]);
return response()->json(['message' => trans('done')]);
} else {
return response()->json(['message' => trans('account_already_added')], 400);
}
} else {
return response()->json(['message' => trans('not_found')], 400);
}
}
public function edit($id): \Illuminate\Http\JsonResponse
{
$type = ServiceConfigAccount::where('public_id', '=', $id)
->with('service')
->first();
if ($type) {
return response()->json($type);
} else {
return response()->json(['message' => trans('not_found')], 400);
}
}
public function update(Request $request, $id)
{
$service = ServiceConfigAccount::where('public_id', '=', $id)->first();
if ($service) {
$values = $this->validate($request, [
'master' => 'required',
'phone' => 'required',
'email' => 'required',
'service_config_id' => 'required'
]);
$serviceAcoount = ServiceConfigAccount::where('service_config_id', '=', $service->id)
->where('phone', '=', $values['phone'])
->where('email', '=', $values['email'])
->first();
if (!$serviceAcoount) {
ServiceConfigAccount::where('id', '=', $service->id)
->update([
'master' => $values['master'],
'phone' => $values['phone'],
'email' => $values['email'],
]);
return response()->json(['message' => trans('done')]);
} else {
return response()->json(['message' => trans('account_already_added')], 400);
}
} else {
return response()->json(['message' => trans('not_found')], 400);
}
}
public function destroy($id): \Illuminate\Http\JsonResponse
{
$type = ServiceConfigAccount::where('public_id', '=', $id)->first();
if ($type) {
$type->delete();
return response()->json(['message' => trans('done')]);
} else
return response()->json(['message' => trans('not_found')], 400);
}
}