Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 51 additions & 3 deletions app/Http/Controllers/ServerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@
use App\Settings\GeneralSettings;
use Exception;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Client\Response;
use Illuminate\Http\Client\Response as ClientResponse;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import isn't used anywhere in this file, so could be removed completely instead of importing it under different name

use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -354,19 +355,66 @@ private function handleServerDeletion(Server $server): void
Cache::forget('user_credits_left:' . $server->user_id);
}

public function cancel(Server $server): RedirectResponse
public function cancel(Server $server): Response|RedirectResponse
{
if ($server->user_id !== Auth::id()) {
return back()->with('error', __('This is not your Server!'));
}

try {
$server->update(['canceled' => now()]);

if (request()->expectsJson()) {
return response()->noContent();
}

return redirect()->route('servers.index')
->with('success', __('Server canceled'));
} catch (Exception $e) {
report($e);

if (request()->expectsJson()) {
return response()->json(['error' => __('Server cancellation failed')], 500);
}

return redirect()->route('servers.index')
->with('error', __('Server cancellation failed'));
}
}

public function resume(Server $server): Response|RedirectResponse
{
if ($server->user_id !== Auth::id()) {
return back()->with('error', __('This is not your Server!'));
}

if ($server->canceled === null) {
if (request()->expectsJson()) {
return response()->json(['error' => __('Server is not canceled')], 422);
}

return redirect()->route('servers.index')
->with('error', __('Server is not canceled'));
}

try {
$server->update(['canceled' => null]);

if (request()->expectsJson()) {
return response()->noContent();
}

return redirect()->route('servers.index')
->with('success', __('Server cancellation has been revoked'));
} catch (Exception $e) {
report($e);

if (request()->expectsJson()) {
return response()->json(['error' => __('Server cancellation revoke failed')], 500);
}

return redirect()->route('servers.index')
->with('error', __('Server cancellation failed: ') . $e->getMessage());
->with('error', __('Server cancellation revoke failed'));
}
}

Expand Down
3 changes: 2 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
//normal routes
Route::get('notifications/readAll', [NotificationController::class, 'readAll'])->name('notifications.readAll');
Route::resource('notifications', NotificationController::class);
Route::patch('/servers/cancel/{server}', [ServerController::class, 'cancel'])->name('servers.cancel');
Route::patch('/servers/{server}/cancel', [ServerController::class, 'cancel'])->name('servers.cancel');
Route::patch('/servers/{server}/resume', [ServerController::class, 'resume'])->name('servers.resume');
Route::post('/servers/validateDeploymentVariables', [ServerController::class, 'validateDeploymentVariables'])->name('servers.validateDeploymentVariables');
Route::patch('/servers/{server}/billing_priority', [ServerController::class, 'updateBillingPriority'])->name('servers.updateBillingPriority');
Route::delete('/servers/{server}', [ServerController::class, 'destroy'])->name('servers.destroy');
Expand Down
55 changes: 46 additions & 9 deletions themes/default/views/servers/index.blade.php
Comment thread
MrWeez marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,6 @@ class="float-right mr-2 text-center btn btn-danger"

<script>
const handleServerCancel = (serverId) => {
// Handle server cancel with sweetalert
Swal.fire({
title: "{{ __('Cancel Server?') }}",
text: "{{ __('This will cancel your current server to the next billing period. It will get suspended when the current period runs out.') }}",
Expand All @@ -233,14 +232,56 @@ class="float-right mr-2 text-center btn btn-danger"
reverseButtons: true
}).then((result) => {
if (result.value) {
// Delete server
fetch("{{ route('servers.cancel', ['server' => ':serverId']) }}".replace(':serverId', serverId), {
method: 'PATCH',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}'
'X-CSRF-TOKEN': '{{ csrf_token() }}',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
}).then((response) => {
if (response.ok) {
window.location.reload();
} else {
throw new Error('Server error');
}
}).catch((error) => {
Swal.fire({
title: "{{ __('Error') }}",
text: "{{ __('Something went wrong, please try again later.') }}",
icon: 'error',
confirmButtonColor: '#d9534f',
})
})
}
})
}

const handleServerResume = (serverId) => {
Swal.fire({
title: "{{ __('Renew Subscription?') }}",
text: "{{ __('This will revoke the cancellation and keep your server active.') }}",
icon: 'question',
confirmButtonColor: '#5cb85c',
showCancelButton: true,
confirmButtonText: "{{ __('Yes, renew!') }}",
cancelButtonText: "{{ __('No, abort!') }}",
reverseButtons: true
}).then((result) => {
if (result.value) {
fetch("{{ route('servers.resume', ['server' => ':serverId']) }}".replace(':serverId', serverId), {
method: 'PATCH',
headers: {
'X-CSRF-TOKEN': '{{ csrf_token() }}',
'Accept': 'application/json',
'X-Requested-With': 'XMLHttpRequest'
}
}).then((response) => {
if (response.ok) {
window.location.reload();
} else {
throw new Error('Server error');
}
}).then(() => {
window.location.reload();
}).catch((error) => {
Swal.fire({
title: "{{ __('Error') }}",
Expand All @@ -249,7 +290,6 @@ class="float-right mr-2 text-center btn btn-danger"
confirmButtonColor: '#d9534f',
})
})
return
}
})
}
Expand All @@ -266,7 +306,6 @@ class="float-right mr-2 text-center btn btn-danger"
reverseButtons: true
}).then((result) => {
if (result.value) {
// Delete server
fetch("{{ route('servers.destroy', ['server' => ':serverId']) }}".replace(':serverId', serverId), {
method: 'DELETE',
headers: {
Expand All @@ -282,10 +321,8 @@ class="float-right mr-2 text-center btn btn-danger"
confirmButtonColor: '#d9534f',
})
})
return
}
});

}

document.addEventListener('DOMContentLoaded', () => {
Expand Down
Loading