initial commit
This commit is contained in:
58
app/Http/Controllers/CustomerProvisioningController.php
Normal file
58
app/Http/Controllers/CustomerProvisioningController.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreCustomerRequest;
|
||||
use App\Jobs\ProvisionCustomerJob;
|
||||
use App\Models\Customer;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
|
||||
class CustomerProvisioningController extends Controller
|
||||
{
|
||||
public function store(StoreCustomerRequest $request): JsonResponse
|
||||
{
|
||||
$domain = $request->domain();
|
||||
|
||||
$customer = Customer::query()->create([
|
||||
'name' => $request->validated('name'),
|
||||
'domain' => $domain,
|
||||
'cpu' => $request->integer('cpu', config('hosting.defaults.cpu')),
|
||||
'ram' => $request->integer('ram', config('hosting.defaults.ram')),
|
||||
'disk' => $request->integer('disk', config('hosting.defaults.disk')),
|
||||
'status' => 'pending',
|
||||
'provisioning_step' => 'queued',
|
||||
]);
|
||||
|
||||
ProvisionCustomerJob::dispatch($customer->id);
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Provisioning started.',
|
||||
'customer' => $customer,
|
||||
], 202);
|
||||
}
|
||||
|
||||
public function show(Customer $customer): JsonResponse
|
||||
{
|
||||
return response()->json(['customer' => $customer]);
|
||||
}
|
||||
|
||||
public function index(): JsonResponse
|
||||
{
|
||||
return response()->json([
|
||||
'customers' => Customer::query()->latest()->get(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function destroy(Customer $customer): JsonResponse
|
||||
{
|
||||
if ($customer->status === 'pending' && $customer->provisioning_step === 'queued') {
|
||||
$customer->delete();
|
||||
|
||||
return response()->json(['message' => 'Queued customer removed.']);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'message' => 'Use deprovision endpoint for active customers.',
|
||||
], 422);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user