Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add countdown module #764

Merged
merged 3 commits into from
Oct 15, 2024
Merged
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
40 changes: 40 additions & 0 deletions app/Http/Controllers/Api/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,46 @@ public function scoreSystemState(): JsonResponse
return response()->json(json_decode($state->value));
}

/**
* Return the current state of the countdown.
* The state is structured like this:
* {
* "state": string; // setup, running, stopped
* "direction": string; // up, down
* "time": {
* "seconds": number;
* "minutes": number;
* "hours": number;
* };
* }
*
* The definition of the states is as follows:
* setup: The countdown is not set up yet
* idle: The countdown was resetted
* running: The countdown is running
* stopped: The countdown is stopped
*/
public function countdownState(): JsonResponse
{
// get state with key countdown
$state = State::where('key', 'countdown')->first();

// if state does not exist, return setup
if (! $state) {
return response()->json([
'state' => 'setup',
'time' => [
'seconds' => 0,
'minutes' => 0,
'hours' => 0,
],
'direction' => 'up',
]);
}

return response()->json(json_decode($state->value));
}

/**
* Fresh users data
*/
Expand Down
75 changes: 75 additions & 0 deletions app/Http/Controllers/DashboardAdminCountdownController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace App\Http\Controllers;

use App\Models\State;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\Request;
use Inertia\Inertia;
use Inertia\Response;
use Illuminate\Validation\Rule;

class DashboardAdminCountdownController extends Controller
{
/**
* Display the dashboard admin countdown page
*/
public function index(): Response
{
// check if a state exists or we need to create a new one
$state = State::where('key', 'countdown')->first();
if (! $state) {
$state = new State;
$state->key = 'countdown';
}

return Inertia::render('Dashboard/Admin/Countdown/Index', [
'state' => json_decode($state->value),
]);
}

/**
* Execute the dashboard admin countdown submit action
*/
public function indexExecuteSubmit(): JsonResponse
{
// validate the request
$request = Request::validate([
'state' => ['required', 'in:setup,idle,running,stopped'],
'time.seconds' => ['required', 'numeric', 'min:0', 'max:59'],
'time.minutes' => ['required', 'numeric', 'min:0', 'max:59'],
'time.hours' => ['required', 'numeric', 'min:0', 'max:23'],
'direction' => ['required', 'in:up,down'],
]);

// check if a state exists or we need to create a new one
$state = State::where('key', 'countdown')->first();
if (! $state) {
$state = new State;
$state->key = 'countdown';
}

// update the state
$state->value = json_encode([
'state' => $request['state'],
'time' => $request['time'],
'direction' => $request['direction'],
]);

// save the state
$state->save();

// return success
return response()->json([
'success' => true,
]);
}

/*
* Display the dashboard admin countdown display page
*/
public function display(): Response
{
return Inertia::render('Dashboard/Admin/Countdown/Display');
}
}
8 changes: 4 additions & 4 deletions bootstrap/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
\Spatie\Permission\PermissionServiceProvider::class,
])
->withRouting(
web: __DIR__.'/../routes/web.php',
api: __DIR__.'/../routes/api.php',
commands: __DIR__.'/../routes/console.php',
web: __DIR__ . '/../routes/web.php',
api: __DIR__ . '/../routes/api.php',
commands: __DIR__ . '/../routes/console.php',
// channels: __DIR__.'/../routes/channels.php',
health: '/up',
)
->withMiddleware(function (Middleware $middleware) {
$middleware->redirectGuestsTo(fn () => route('app.login'));
$middleware->redirectGuestsTo(fn() => route('app.login'));
$middleware->redirectUsersTo(AppServiceProvider::HOME);

$middleware->web(\App\Http\Middleware\HandleInertiaRequests::class);
Expand Down
6 changes: 5 additions & 1 deletion database/seeders/ModuleDemoSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ public function run(): void
'key' => 'scoreSystem',
'active' => true,
],
[
'key' => 'countdown',
'active' => true,
],
];

foreach ($modules as $module) {
// check if module with key already exists
$existingModule = Module::where('key', $module['key'])->first();
if (! $existingModule) {
throw new \Exception('Module with key "'.$module['key'].'" not found.');
throw new \Exception('Module with key "' . $module['key'] . '" not found.');
}

// update module
Expand Down
6 changes: 5 additions & 1 deletion database/seeders/ModuleGerolsteinSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ public function run(): void
'key' => 'scoreSystem',
'active' => true,
],
[
'key' => 'countdown',
'active' => true,
],
];

foreach ($modules as $module) {
// check if module with key already exists
$existingModule = Module::where('key', $module['key'])->first();
if (! $existingModule) {
throw new \Exception('Module with key "'.$module['key'].'" not found.');
throw new \Exception('Module with key "' . $module['key'] . '" not found.');
}

// update module
Expand Down
4 changes: 4 additions & 0 deletions database/seeders/ModuleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public function run(): void
'key' => 'scoreSystem',
'active' => false,
],
[
'key' => 'countdown',
'active' => false,
],
];

foreach ($modules as $module) {
Expand Down
24 changes: 15 additions & 9 deletions database/seeders/RoleSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,18 @@ public function run(): void
{
// create permissions if they don't exist
$permissions = [];
foreach ([
'view statistics',
'view hidden event details',
'manage events',
'manage users',
'delete users',
'manage random generator',
'manage score system',
] as $permission) {
foreach (
[
'view statistics',
'view hidden event details',
'manage events',
'manage users',
'delete users',
'manage random generator',
'manage score system',
'manage countdown',
] as $permission
) {
if (! Permission::where('name', $permission)->exists()) {
Permission::create(['name' => $permission]);
}
Expand All @@ -43,6 +46,7 @@ public function run(): void
$stageTutorRole = Role::where('name', 'stage tutor')->first();
$stageTutorRole->givePermissionTo($permissions['manage random generator']);
$stageTutorRole->givePermissionTo($permissions['manage score system']);
$stageTutorRole->givePermissionTo($permissions['manage countdown']);

// create esa role if it doesn't exist
if (! Role::where('name', 'esa')->exists()) {
Expand All @@ -62,6 +66,7 @@ public function run(): void
$adminRole->givePermissionTo($permissions['manage events']);
$adminRole->givePermissionTo($permissions['manage random generator']);
$adminRole->givePermissionTo($permissions['manage score system']);
$adminRole->givePermissionTo($permissions['manage countdown']);
$adminRole->givePermissionTo($permissions['manage users']);

// create super admin role if it doesn't exist
Expand All @@ -74,6 +79,7 @@ public function run(): void
$superAdminRole->givePermissionTo($permissions['manage events']);
$superAdminRole->givePermissionTo($permissions['manage random generator']);
$superAdminRole->givePermissionTo($permissions['manage score system']);
$superAdminRole->givePermissionTo($permissions['manage countdown']);
$superAdminRole->givePermissionTo($permissions['manage users']);
$superAdminRole->givePermissionTo($permissions['delete users']);

Expand Down
33 changes: 33 additions & 0 deletions resources/js/components/app/Navbar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,22 @@
>
Punktesystem
</InertiaLink>
<InertiaLink
v-if="
modules['countdown']?.active &&
user.permissionsArray.includes('manage countdown')
"
href="/dashboard/admin/countdown"
:class="{
'border-red-500 text-red-900':
$page.url == '/dashboard/admin/countdown',
'border-transparent text-red-500 hover:border-red-300 hover:text-red-700':
$page.url != '/dashboard/admin/countdown',
}"
class="inline-flex items-center border-b-[3px] px-1 pt-1 text-sm font-medium"
>
Countdown
</InertiaLink>
<InertiaLink
v-if="user.permissionsArray.includes('manage users')"
href="/dashboard/admin/users"
Expand Down Expand Up @@ -210,6 +226,23 @@
>
Punktesystem
</DisclosureButton>
<DisclosureButton
v-if="
modules['countdown']?.active &&
user.permissionsArray.includes('manage countdown')
"
:as="InertiaLink"
href="/dashboard/admin/countdown"
:class="{
'border-red-500 bg-red-100 text-red-900 dark:bg-black dark:text-red-500':
$page.url == '/dashboard/admin/countdown',
'border-transparent text-red-600 hover:border-red-300 hover:bg-red-50 hover:text-red-800 dark:text-red-400 dark:hover:bg-gray-900 dark:hover:text-red-100':
$page.url != '/dashboard/admin/countdown',
}"
class="block border-l-4 py-2 pl-3 pr-4 text-base font-medium"
>
Countdown
</DisclosureButton>
<DisclosureButton
v-if="user.permissionsArray.includes('manage users')"
:as="InertiaLink"
Expand Down
Loading