feat: Implement Mutations CRUD
This commit is contained in:
parent
4fe89f0600
commit
8f2cf8e642
78
app/Http/Controllers/MutationController.php
Normal file
78
app/Http/Controllers/MutationController.php
Normal file
@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\StoreMutationRequest;
|
||||
use App\Models\Dynamic;
|
||||
use App\Models\Ledger;
|
||||
use App\Models\Mutation;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MutationController 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(StoreMutationRequest $request, Dynamic $dynamic, Ledger $ledger)
|
||||
{
|
||||
DB::transaction(function () use ($request, $ledger) {
|
||||
$ledger->mutations()->create([
|
||||
...$request->validated(),
|
||||
'user_id' => $request->user()->id,
|
||||
]);
|
||||
|
||||
$ledger->increment('score', $request->validated('amount'));
|
||||
});
|
||||
|
||||
return redirect()->route('dynamics.ledgers.show', [$dynamic, $ledger]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Display the specified resource.
|
||||
*/
|
||||
public function show(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(Request $request, Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
||||
{
|
||||
//
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy(Dynamic $dynamic, Ledger $ledger, Mutation $mutation)
|
||||
{
|
||||
//
|
||||
}
|
||||
}
|
||||
37
app/Http/Requests/StoreMutationRequest.php
Normal file
37
app/Http/Requests/StoreMutationRequest.php
Normal file
@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
|
||||
use App\Models\Ledger;
|
||||
|
||||
class StoreMutationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
$ledger = $this->route('ledger');
|
||||
|
||||
return $ledger && $this->user()->can('view', $ledger);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*
|
||||
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
return [
|
||||
'amount' => ['required', 'integer'],
|
||||
'description' => ['required', 'string'],
|
||||
'type' => ['nullable', 'string'],
|
||||
'status' => ['nullable', 'string'],
|
||||
];
|
||||
}
|
||||
}
|
||||
@ -1,6 +1,6 @@
|
||||
<script setup>
|
||||
import AppLayout from '@/layouts/AppLayout.vue';
|
||||
import { Head, Link } from '@inertiajs/vue3';
|
||||
import { Head, useForm } from '@inertiajs/vue3';
|
||||
import { route } from 'ziggy-js';
|
||||
|
||||
const props = defineProps({
|
||||
@ -22,6 +22,17 @@ const breadcrumbs = [
|
||||
href: route('dynamics.ledgers.show', { dynamic: props.dynamic.id, ledger: props.ledger.id }),
|
||||
},
|
||||
];
|
||||
|
||||
const form = useForm({
|
||||
amount: 0,
|
||||
description: '',
|
||||
});
|
||||
|
||||
function submit() {
|
||||
form.post(route('dynamics.ledgers.mutations.store', { dynamic: props.dynamic.id, ledger: props.ledger.id }), {
|
||||
onSuccess: () => form.reset(),
|
||||
});
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
@ -38,6 +49,29 @@ const breadcrumbs = [
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="mt-8">
|
||||
<h4 class="text-lg font-medium text-gray-900 dark:text-gray-100">Add Mutation</h4>
|
||||
<form @submit.prevent="submit" class="mt-6 space-y-6 bg-white dark:bg-gray-800 overflow-hidden shadow-sm sm:rounded-lg p-6">
|
||||
<div>
|
||||
<label for="amount" class="block text-sm font-medium text-gray-700 dark:text-gray-300">Amount</label>
|
||||
<input v-model="form.amount" id="amount" type="number" 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.amount" class="text-sm text-red-600">{{ form.errors.amount }}</div>
|
||||
</div>
|
||||
|
||||
<div>
|
||||
<label for="description" class="block text-sm font-medium text-gray-700 dark:text-gray-300">Description</label>
|
||||
<textarea v-model="form.description" id="description" 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.description" class="text-sm text-red-600">{{ form.errors.description }}</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">
|
||||
Add Mutation
|
||||
</button>
|
||||
</div>
|
||||
</form>
|
||||
</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">
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
<?php
|
||||
|
||||
use App\Http\Controllers\MutationController;
|
||||
use App\Http\Controllers\DynamicController;
|
||||
use App\Http\Controllers\LedgerController;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
@ -11,6 +12,7 @@ Route::middleware(['auth', 'verified'])->group(function () {
|
||||
|
||||
Route::resource('dynamics', DynamicController::class);
|
||||
Route::resource('dynamics.ledgers', LedgerController::class)->scoped();
|
||||
Route::resource('dynamics.ledgers.mutations', MutationController::class)->scoped();
|
||||
});
|
||||
|
||||
require __DIR__.'/settings.php';
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user