feat: Implement Ledgers CRUD

This commit is contained in:
Daan Meijer 2026-06-15 00:33:32 +02:00
parent 647a708160
commit 4fe89f0600
7 changed files with 290 additions and 4 deletions

View File

@ -0,0 +1,77 @@
<?php
namespace App\Http\Controllers;
use App\Http\Requests\StoreLedgerRequest;
use App\Models\Dynamic;
use App\Models\Ledger;
use Illuminate\Http\Request;
use Inertia\Inertia;
class LedgerController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(StoreLedgerRequest $request, Dynamic $dynamic)
{
$dynamic->ledgers()->create($request->validated());
return redirect()->route('dynamics.show', $dynamic);
}
/**
* Display the specified resource.
*/
public function show(Dynamic $dynamic, Ledger $ledger)
{
$this->authorize('view', $ledger);
$ledger->load('mutations.user');
return Inertia::render('Ledgers/Show', [
'dynamic' => $dynamic,
'ledger' => $ledger,
]);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Ledger $ledger)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Ledger $ledger)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Ledger $ledger)
{
//
}
}

View File

@ -0,0 +1,35 @@
<?php
namespace App\Http\Requests;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Foundation\Http\FormRequest;
use App\Models\Dynamic;
class StoreLedgerRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
$dynamic = $this->route('dynamic');
return $dynamic && $this->user()->can('view', $dynamic);
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255'],
'rules' => ['nullable', 'string'],
];
}
}

View File

@ -0,0 +1,66 @@
<?php
namespace App\Policies;
use App\Models\Dynamic;
use App\Models\Ledger;
use App\Models\User;
class LedgerPolicy
{
/**
* Determine whether the user can view any models.
*/
public function viewAny(User $user, Dynamic $dynamic): bool
{
return $user->can('view', $dynamic);
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Ledger $ledger): bool
{
return $user->can('view', $ledger->dynamic);
}
/**
* Determine whether the user can create models.
*/
public function create(User $user, Dynamic $dynamic): bool
{
return $user->can('view', $dynamic);
}
/**
* Determine whether the user can update the model.
*/
public function update(User $user, Ledger $ledger): bool
{
return false;
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Ledger $ledger): bool
{
return false;
}
/**
* Determine whether the user can restore the model.
*/
public function restore(User $user, Ledger $ledger): bool
{
return false;
}
/**
* Determine whether the user can permanently delete the model.
*/
public function forceDelete(User $user, Ledger $ledger): bool
{
return false;
}
}

View File

@ -3,7 +3,9 @@
namespace App\Providers; namespace App\Providers;
use App\Models\Dynamic; use App\Models\Dynamic;
use App\Models\Ledger;
use App\Policies\DynamicPolicy; use App\Policies\DynamicPolicy;
use App\Policies\LedgerPolicy;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider; use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider class AuthServiceProvider extends ServiceProvider
@ -15,6 +17,7 @@ class AuthServiceProvider extends ServiceProvider
*/ */
protected $policies = [ protected $policies = [
Dynamic::class => DynamicPolicy::class, Dynamic::class => DynamicPolicy::class,
Ledger::class => LedgerPolicy::class,
]; ];
/** /**

View File

@ -1,6 +1,6 @@
<script setup> <script setup>
import AppLayout from '@/layouts/AppLayout.vue'; import AppLayout from '@/layouts/AppLayout.vue';
import { Head, Link } from '@inertiajs/vue3'; import { Head, Link, useForm } from '@inertiajs/vue3';
import { route } from 'ziggy-js'; import { route } from 'ziggy-js';
const props = defineProps({ const props = defineProps({
@ -17,6 +17,17 @@ const breadcrumbs = [
href: route('dynamics.show', props.dynamic.id), href: route('dynamics.show', props.dynamic.id),
}, },
]; ];
const form = useForm({
name: '',
rules: '',
});
function submit() {
form.post(route('dynamics.ledgers.store', props.dynamic.id), {
onSuccess: () => form.reset(),
});
}
</script> </script>
<template> <template>
@ -44,18 +55,47 @@ const breadcrumbs = [
<div class="mt-8"> <div class="mt-8">
<div class="flex justify-between items-center mb-6"> <div class="flex justify-between items-center mb-6">
<h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Ledgers</h4> <h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Ledgers</h4>
<!-- TODO: Add link to create ledger -->
</div> </div>
<ul class="mt-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6"> <ul class="mt-4 grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
<li v-for="ledger in dynamic.ledgers" :key="ledger.id" class="p-6 bg-white dark:bg-gray-700 border-b border-gray-200 dark:border-gray-600"> <li v-for="ledger in dynamic.ledgers" :key="ledger.id" class="p-6 bg-white dark:bg-gray-700 border-b border-gray-200 dark:border-gray-600">
<h5 class="text-lg font-semibold">{{ ledger.name }}</h5> <Link :href="route('dynamics.ledgers.show', { dynamic: dynamic.id, ledger: ledger.id })">
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Score: {{ ledger.score }}</p> <h5 class="text-lg font-semibold">{{ ledger.name }}</h5>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Score: {{ ledger.score }}</p>
</Link>
</li> </li>
</ul> </ul>
<div v-if="dynamic.ledgers.length === 0" class="mt-4 text-gray-500"> <div v-if="dynamic.ledgers.length === 0" class="mt-4 text-gray-500">
No ledgers found for this dynamic. No ledgers found for this dynamic.
</div> </div>
</div> </div>
<div class="mt-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">
<h3 class="text-lg font-medium">Create a New Ledger</h3>
<form @submit.prevent="submit" class="mt-6 space-y-6">
<div>
<label for="name" class="block text-sm font-medium text-gray-700 dark:text-gray-300">Name</label>
<input v-model="form.name" id="name" type="text" class="mt-1 block w-full border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm" />
<div v-if="form.errors.name" class="text-sm text-red-600">{{ form.errors.name }}</div>
</div>
<div>
<label for="rules" class="block text-sm font-medium text-gray-700 dark:text-gray-300">Rules</label>
<textarea v-model="form.rules" id="rules" rows="4" class="mt-1 block w-full border-gray-300 dark:border-gray-700 dark:bg-gray-900 dark:text-gray-300 focus:border-indigo-500 dark:focus:border-indigo-600 focus:ring-indigo-500 dark:focus:ring-indigo-600 rounded-md shadow-sm"></textarea>
<div v-if="form.errors.rules" class="text-sm text-red-600">{{ form.errors.rules }}</div>
</div>
<div class="flex items-center gap-4">
<button type="submit" :disabled="form.processing" class="inline-flex items-center px-4 py-2 bg-gray-800 dark:bg-gray-200 border border-transparent rounded-md font-semibold text-xs text-white dark:text-gray-800 uppercase tracking-widest hover:bg-gray-700 dark:hover:bg-white focus:bg-gray-700 dark:focus:bg-white active:bg-gray-900 dark:active:bg-gray-300 focus:outline-none focus:ring-2 focus:ring-indigo-500 focus:ring-offset-2 dark:focus:ring-offset-gray-800 transition ease-in-out duration-150">
Create Ledger
</button>
</div>
</form>
</div>
</div>
</div>
</div> </div>
</div> </div>
</AppLayout> </AppLayout>

View File

@ -0,0 +1,63 @@
<script setup>
import AppLayout from '@/layouts/AppLayout.vue';
import { Head, Link } from '@inertiajs/vue3';
import { route } from 'ziggy-js';
const props = defineProps({
dynamic: Object,
ledger: Object,
});
const breadcrumbs = [
{
name: 'Dynamics',
href: route('dynamics.index'),
},
{
name: props.dynamic.name,
href: route('dynamics.show', props.dynamic.id),
},
{
name: props.ledger.name,
href: route('dynamics.ledgers.show', { dynamic: props.dynamic.id, ledger: props.ledger.id }),
},
];
</script>
<template>
<Head :title="ledger.name" />
<AppLayout :breadcrumbs="breadcrumbs">
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 text-gray-900 dark:text-gray-100">
<h3 class="text-lg font-medium">{{ ledger.name }}</h3>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">Score: {{ ledger.score }}</p>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ ledger.rules }}</p>
</div>
</div>
<div class="mt-8">
<h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Mutations</h4>
<ul class="mt-4 space-y-4">
<li v-for="mutation in ledger.mutations" :key="mutation.id" class="p-4 bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg">
<div class="flex justify-between">
<span class="font-semibold">{{ mutation.user.name }}</span>
<span :class="{
'text-green-500': mutation.amount > 0,
'text-red-500': mutation.amount < 0,
}">{{ mutation.amount > 0 ? '+' : '' }}{{ mutation.amount }}</span>
</div>
<p class="mt-2 text-sm text-gray-600 dark:text-gray-400">{{ mutation.description }}</p>
<div class="mt-2 text-xs text-gray-500">{{ new Date(mutation.created_at).toLocaleString() }}</div>
</li>
</ul>
<div v-if="ledger.mutations.length === 0" class="mt-4 text-gray-500">
No mutations found for this ledger.
</div>
</div>
</div>
</div>
</AppLayout>
</template>

View File

@ -1,6 +1,7 @@
<?php <?php
use App\Http\Controllers\DynamicController; use App\Http\Controllers\DynamicController;
use App\Http\Controllers\LedgerController;
use Illuminate\Support\Facades\Route; use Illuminate\Support\Facades\Route;
Route::inertia('/', 'Welcome')->name('home'); Route::inertia('/', 'Welcome')->name('home');
@ -9,6 +10,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
Route::inertia('dashboard', 'Dashboard')->name('dashboard'); Route::inertia('dashboard', 'Dashboard')->name('dashboard');
Route::resource('dynamics', DynamicController::class); Route::resource('dynamics', DynamicController::class);
Route::resource('dynamics.ledgers', LedgerController::class)->scoped();
}); });
require __DIR__.'/settings.php'; require __DIR__.'/settings.php';