Skip to content

Commit

Permalink
Added a form to add new punishments reasons for a specified game server.
Browse files Browse the repository at this point in the history
  • Loading branch information
d3m37r4 committed Dec 8, 2024
1 parent 03816b4 commit 03ea2ae
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 88 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ public function show(GameServer $gameServer)
'ip' => $gameServer->ip,
'port' => $gameServer->port,
],
'punishmentReasons' => $gameServer->punishmentReasons->map(function ($punishmentReason) {
'punishmentReasons' => $gameServer->punishmentReasons->sortBy(function($punishmentReason) {
return $punishmentReason->id;
})->values()->map(function ($punishmentReason) {
return [
'id' => $punishmentReason->id,
'name' => $punishmentReason->name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,38 @@
namespace App\Http\Controllers\Dashboard\GameServer;

use App\Http\Controllers\Controller;
use App\Http\Requests\Dashboard\GameServer\Reason\StoreReasonRequest;
use App\Http\Requests\Dashboard\GameServer\Reason\UpdateReasonRequest;
use App\Http\Requests\Dashboard\GameServer\PunishmentReason\StoreRequest;
use App\Http\Requests\Dashboard\GameServer\PunishmentReason\UpdateRequest;
use App\Models\GameServer\GameServer;
use App\Models\GameServer\PunishmentReason;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Request;

class PunishmentReasonController extends Controller
{
/**
* Show the form for creating a new reason.
*
* @param GameServer $server
* @return View
* Show the form for creating a new punishment reason.
*/
public function create(GameServer $server): View
public function create(GameServer $gameServer)
{
return view('admin.servers.reasons.create', compact('server'));
return inertia('Dashboard/GameServers/PunishmentReasons/Create', [
'title' => 'Новая причина наказания',
'gameServer' => $gameServer->only('id'),
]);
}

/**
* Store a newly created reason in storage.
*
* @param StoreReasonRequest $request
* @param GameServer $server
* @return RedirectResponse
* Store a newly created punishment reason in storage.
*/
public function store(StoreReasonRequest $request, GameServer $server): RedirectResponse
public function store(StoreRequest $request, GameServer $gameServer)
{
$reason = PunishmentReason::create($request->safe()->except('months', 'days', 'hours', 'minutes'));
$punishmentReason = PunishmentReason::create($request->safe()
->except('months', 'days', 'hours', 'minutes'));

return redirect()->route('admin.servers.show', $server->id)->with([
return redirect()->route('dashboard.game-servers.show', $gameServer)->with([
'status' => 'success',
'message' => "Причина наказания \"$reason->title\" успешно добавлена!"
'message' => "Причина наказания \"$punishmentReason->name\" добавлена!"
]);
}

Expand All @@ -55,12 +53,12 @@ public function edit(GameServer $server, PunishmentReason $reason): View
/**
* Update the specified reason in storage.
*
* @param UpdateReasonRequest $request
* @param UpdateRequest $request
* @param GameServer $server
* @param PunishmentReason $reason
* @return RedirectResponse
*/
public function update(UpdateReasonRequest $request, GameServer $server, PunishmentReason $reason): RedirectResponse
public function update(UpdateRequest $request, GameServer $server, PunishmentReason $reason): RedirectResponse
{
$reason->update($request->safe()->except('months', 'days', 'hours', 'minutes'));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace App\Http\Requests\Dashboard\GameServer\PunishmentReason;

use Carbon\CarbonInterval;
use App\Models\GameServer\GameServer;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

/**
* @property GameServer $game_server
*/
class StoreRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize(): bool
{
return true;
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
'name' => ['required', 'string', 'max:255',
Rule::unique('punishment_reasons')->where('game_server_id', $this->game_server->id)
],
'months' => ['required', 'numeric', 'min:0'],
'days' => ['required', 'numeric', 'min:0'],
'hours' => ['required', 'numeric', 'min:0'],
'minutes' => ['required', 'numeric', 'min:0'],
'time' => ['required', 'numeric', 'min:0'],
'game_server_id' => ['required', 'integer'],
];
}

/**
* Prepare the data for validation.
*
* @return void
*/
protected function prepareForValidation(): void
{
$this->merge([
'game_server_id' => $this->game_server->id,
'time' => CarbonInterval::months($this->input('months'))
->days($this->input('days'))
->hours($this->input('hours'))
->minutes($this->input('minutes'))
->totalMinutes
]);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<?php

namespace App\Http\Requests\Dashboard\GameServer\Reason;
namespace App\Http\Requests\Dashboard\GameServer\PunishmentReason;

use Illuminate\Validation\Rule;
use Request;

/**
* @property mixed reason
*/
class UpdateReasonRequest extends StoreReasonRequest
class UpdateRequest extends StoreRequest
{
public function rules(): array
{
Expand Down

This file was deleted.

1 change: 1 addition & 0 deletions app/Models/GameServer/PunishmentReason.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
/**
* @method static create(array $array)
* @method map(Closure $param)
* @method sortBy(Closure $param)
*/
class PunishmentReason extends Model
{
Expand Down
132 changes: 132 additions & 0 deletions resources/js/Pages/Dashboard/GameServers/PunishmentReasons/Create.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<script setup>
import DashboardLayout from '@/Layouts/Dashboard.vue';
import { useForm } from '@inertiajs/vue3';
import InputError from "@/Components/InputError.vue";
import BackButton from "@/Components/Buttons/BackButton.vue";
import CreateButton from "@/Components/Buttons/CreateButton.vue";
defineOptions({
layout: DashboardLayout
});
const props = defineProps({
title: String,
gameServer: Object,
});
const form = useForm({
name: '',
months: 0,
days: 0,
hours: 0,
minutes: 0,
});
const store = () => {
form.post(route('dashboard.game-servers.punishment-reasons.store', props.gameServer));
}
</script>

<template>
<div class="ml-4 space-y-4">
<div class="flex items-center space-x-4 mx-4">
<div class="grow">
<h1 class="text-xl">{{ title }}</h1>
</div>
<div class="flex-none">
<BackButton title="Назад" :href="route('dashboard.game-servers.show', props.gameServer)" />
</div>
</div>
<form @submit.prevent="store">
<div class="bg-base-200 rounded-box p-4">
<div class="grid grid-cols-6 gap-x-6">
<div class="col-span-3">
<label for="name" class="label">
<span class="text-base label-text">{{ ('Название причины') }}</span>
</label>
<input
id="name"
name="name"
type="text"
class="input input-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
v-model="form.name"
required
autofocus />
<InputError :message="form.errors.name" />
</div>
</div>
<div class="mt-2">
<label class="label">
<span class="text-base label-text">{{ ('Длительность наказания') }}</span>
</label>
<div class="grid grid-cols-6 gap-x-6">
<div>
<label for="months" class="label">
<span class="label-text">{{ ('Месяцы') }}</span>
</label>
<input
id="months"
name="months"
type="number"
class="input input-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
v-model.number="form.months"
min="0"
value="0" />
<InputError :message="form.errors.months" />
</div>
<div>
<label for="days" class="label">
<span class="label-text">{{ ('Дни') }}</span>
</label>
<input
id="days"
name="days"
type="number"
class="input input-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
v-model.number="form.days"
min="0"
value="0" />
<InputError :message="form.errors.days" />
</div>
<div>
<label for="hours" class="label">
<span class="label-text">{{ ('Часы') }}</span>
</label>
<input
id="hours"
name="hours"
type="number"
class="input input-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
v-model.number="form.hours"
min="0"
value="0" />
<InputError :message="form.errors.hours" />
</div>
<div>
<label for="minutes" class="label">
<span class="label-text">{{ ('Минуты') }}</span>
</label>
<input
id="minutes"
name="minutes"
type="number"
class="input input-bordered w-full focus:ring-1 focus:ring-offset-2 focus:ring-offset-base-200 focus:ring-orange-500"
v-model.number="form.minutes"
min="0"
value="0" />
<InputError :message="form.errors.minutes" />
</div>
</div>
<div class="flex mt-2">
<span class="label-text">
{{ ('Оставьте все значения равными нулю для указания бессрочного действия наказания.') }}
</span>
</div>
</div>
</div>
<div class="flex justify-end m-4">
<CreateButton title="Добавить" :disabled="!form.isDirty" />
</div>
</form>
</div>
</template>
6 changes: 3 additions & 3 deletions resources/js/Pages/Dashboard/GameServers/Show.vue
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,11 @@ const isActive = (tabId) => {
</div>

<div id="tab-reasons" v-show="isActive('tab-reasons')" class="">
<div class="pb-4">
<div class="pb-4 flex justify-end">
<CreateButton
title="Добавить"
class="btn-sm"
:href="route('dashboard.game-servers.punishment-reasons.create', gameServer.id)" />
:href="route('dashboard.game-servers.punishment-reasons.create', gameServer)" />
</div>
<div v-if="punishmentReasons.length > 0" class="bg-base-200 rounded-box p-4">
<div class="overflow-x-auto">
Expand All @@ -219,7 +219,7 @@ const isActive = (tabId) => {
<td>{{ punishmentReason.updated_at }}</td>
<td>
<div class="lg:tooltip" data-tip="Редактировать">
<Link :href="route('dashboard.game-servers.punishment-reasons.edit', [gameServer.id, punishmentReason.id])" class="btn btn-sm btn-circle btn-outline btn-accent me-1">
<Link :href="route('dashboard.game-servers.punishment-reasons.edit', [gameServer, punishmentReason])" class="btn btn-sm btn-circle btn-outline btn-accent me-1">
<svg class="h-5 w-5" viewBox="0 0 24 24" fill="currentColor"><g id="info"/><g id="icons"><g id="edit"><path d="M2,20c0,1.1,0.9,2,2,2h2.6L2,17.4V20z"/><path d="M21.6,5.6l-3.2-3.2c-0.8-0.8-2-0.8-2.8,0l-0.2,0.2C15,3,15,3.6,15.4,4L20,8.6c0.4,0.4,1,0.4,1.4,0l0.2-0.2 C22.4,7.6,22.4,6.4,21.6,5.6z"/><path d="M14,5.4c-0.4-0.4-1-0.4-1.4,0l-9.1,9.1C3,15,3,15.6,3.4,16L8,20.6c0.4,0.4,1,0.4,1.4,0l9.1-9.1c0.4-0.4,0.4-1,0-1.4 L14,5.4z"/></g></g></svg>
</Link>
</div>
Expand Down

0 comments on commit 03ea2ae

Please sign in to comment.