refactor: use route model binding for staff bots

This commit is contained in:
Roardom
2023-06-28 07:02:45 +00:00
parent 3450577c00
commit b4fd0afc26
6 changed files with 34 additions and 30 deletions

View File

@@ -37,22 +37,22 @@ class ChatBotController extends Controller
/**
* Show the form for editing the specified Bot resource.
*/
public function edit(Request $request, int $id): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
public function edit(Request $request, Bot $bot): \Illuminate\Contracts\View\Factory|\Illuminate\Contracts\View\View
{
return view('Staff.chat.bot.edit', [
'user' => $request->user(),
'bot' => Bot::findOrFail($id),
'bot' => $bot,
]);
}
/**
* Update the specified Bot resource in storage.
*/
public function update(UpdateChatBotRequest $request, int $id): \Illuminate\Http\RedirectResponse
public function update(UpdateChatBotRequest $request, Bot $bot): \Illuminate\Http\RedirectResponse
{
Bot::findOrFail($id)->update($request->validated());
$bot->update($request->validated());
return to_route('staff.bots.edit', ['id' => $id])
return to_route('staff.bots.index')
->withSuccess("The Bot Has Been Updated");
}
@@ -61,9 +61,11 @@ class ChatBotController extends Controller
*
* @throws Exception
*/
public function destroy(int $id): \Illuminate\Http\RedirectResponse
public function destroy(Bot $bot): \Illuminate\Http\RedirectResponse
{
Bot::where('is_protected', '=', 0)->findOrFail($id)->delete();
abort_if($bot->is_protected, 403);
$bot->delete();
return to_route('staff.bots.index')
->withSuccess('The Humans Vs Machines War Has Begun! Humans: 1 and Bots: 0');
@@ -72,9 +74,9 @@ class ChatBotController extends Controller
/**
* Disable the specified Bot resource in storage.
*/
public function disable(int $id): \Illuminate\Http\RedirectResponse
public function disable(Bot $bot): \Illuminate\Http\RedirectResponse
{
Bot::findOrFail($id)->update([
$bot->update([
'active' => 0,
]);
@@ -85,9 +87,9 @@ class ChatBotController extends Controller
/**
* Enable the specified Bot resource in storage.
*/
public function enable(int $id): \Illuminate\Http\RedirectResponse
public function enable(Bot $bot): \Illuminate\Http\RedirectResponse
{
Bot::findOrFail($id)->update([
$bot->update([
'active' => 1,
]);

View File

@@ -47,7 +47,7 @@
<section class="panelV2">
<h2 class="panel__heading">{{ __('bot.edit-bot') }}: {{ $bot->name }}</h2>
<div class="panel__body">
<form class="form" method="POST" action="{{ route('staff.bots.update', ['id' => $bot->id]) }}">
<form class="form" method="POST" action="{{ route('staff.bots.update', ['bot' => $bot]) }}">
@csrf
@method('PATCH')
<p class="form__group">

View File

@@ -50,7 +50,7 @@
@foreach($bots as $bot)
<tr>
<td>
<a href="{{ route('staff.bots.edit', ['id' => $bot->id]) }}">
<a href="{{ route('staff.bots.edit', ['bot' => $bot]) }}">
{{ $bot->name }}
</a>
</td>
@@ -77,7 +77,7 @@
<li class="data-table__action">
<form
method="POST"
action="{{ route('staff.bots.disable', ['id' => $bot->id]) }}"
action="{{ route('staff.bots.disable', ['bot' => $bot]) }}"
>
@csrf
<button class="form__button form__button--text">
@@ -89,7 +89,7 @@
<li class="data-table__action">
<form
method="POST"
action="{{ route('staff.bots.enable', ['id' => $bot->id]) }}"
action="{{ route('staff.bots.enable', ['bot' => $bot]) }}"
>
@csrf
<button class="form__button form__button--text">
@@ -102,7 +102,7 @@
<li class="data-table__action">
<a
class="form__button form__button--text"
href="{{ route('staff.bots.edit', ['id' => $bot->id]) }}"
href="{{ route('staff.bots.edit', ['bot' => $bot]) }}"
>
{{ __('common.edit') }}
</a>
@@ -110,7 +110,7 @@
<li class="data-table__action">
<form
class="data-table__action"
action="{{ route('staff.bots.destroy', ['id' => $bot->id]) }}"
action="{{ route('staff.bots.destroy', ['bot' => $bot]) }}"
method="POST"
>
@csrf

View File

@@ -694,14 +694,14 @@ Route::middleware('language')->group(function (): void {
});
// Chat Bots System
Route::prefix('chat')->group(function (): void {
Route::prefix('bots')->group(function (): void {
Route::name('staff.bots.')->group(function (): void {
Route::get('/bots', [App\Http\Controllers\Staff\ChatBotController::class, 'index'])->name('index');
Route::get('/bots/{id}/edit', [App\Http\Controllers\Staff\ChatBotController::class, 'edit'])->name('edit');
Route::patch('/bots/{id}/update', [App\Http\Controllers\Staff\ChatBotController::class, 'update'])->name('update');
Route::delete('/bots/{id}/destroy', [App\Http\Controllers\Staff\ChatBotController::class, 'destroy'])->name('destroy');
Route::post('/bots/{id}/disable', [App\Http\Controllers\Staff\ChatBotController::class, 'disable'])->name('disable');
Route::post('/bots/{id}/enable', [App\Http\Controllers\Staff\ChatBotController::class, 'enable'])->name('enable');
Route::get('/', [App\Http\Controllers\Staff\ChatBotController::class, 'index'])->name('index');
Route::get('/{bot}/edit', [App\Http\Controllers\Staff\ChatBotController::class, 'edit'])->name('edit');
Route::patch('/{bot}/update', [App\Http\Controllers\Staff\ChatBotController::class, 'update'])->name('update');
Route::delete('/{bot}/destroy', [App\Http\Controllers\Staff\ChatBotController::class, 'destroy'])->name('destroy');
Route::post('/{bot}/disable', [App\Http\Controllers\Staff\ChatBotController::class, 'disable'])->name('disable');
Route::post('/{bot}/enable', [App\Http\Controllers\Staff\ChatBotController::class, 'enable'])->name('enable');
});
});

View File

@@ -41,7 +41,7 @@ class ChatBotControllerTest extends TestCase
'is_protected' => false,
]);
$response = $this->actingAs($user)->delete(route('staff.bots.destroy', ['id' => $bot->id]));
$response = $this->actingAs($user)->delete(route('staff.bots.destroy', ['bot' => $bot]));
$response->assertRedirect(route('staff.bots.index'));
}
@@ -55,7 +55,7 @@ class ChatBotControllerTest extends TestCase
$user = $this->createStaffUser();
$bot = Bot::factory()->create();
$response = $this->actingAs($user)->post(route('staff.bots.disable', ['id' => $bot->id]));
$response = $this->actingAs($user)->post(route('staff.bots.disable', ['bot' => $bot]));
$response->assertRedirect(route('staff.bots.index'));
}
@@ -69,7 +69,7 @@ class ChatBotControllerTest extends TestCase
$user = $this->createStaffUser();
$bot = Bot::factory()->create();
$response = $this->actingAs($user)->get(route('staff.bots.edit', ['id' => $bot->id]));
$response = $this->actingAs($user)->get(route('staff.bots.edit', ['bot' => $bot]));
$response->assertOk();
$response->assertViewIs('Staff.chat.bot.edit');
@@ -86,7 +86,7 @@ class ChatBotControllerTest extends TestCase
$user = $this->createStaffUser();
$bot = Bot::factory()->create();
$response = $this->actingAs($user)->post(route('staff.bots.enable', ['id' => $bot->id]));
$response = $this->actingAs($user)->post(route('staff.bots.enable', ['bot' => $bot]));
$response->assertRedirect(route('staff.bots.index'));
}
@@ -116,7 +116,7 @@ class ChatBotControllerTest extends TestCase
$user = $this->createStaffUser();
$bot = Bot::factory()->create();
$response = $this->actingAs($user)->patch(route('staff.bots.update', ['id' => $bot->id]), [
$response = $this->actingAs($user)->patch(route('staff.bots.update', ['bot' => $bot]), [
'position' => $bot->position,
'name' => $bot->name,
'command' => $bot->command,
@@ -140,6 +140,6 @@ class ChatBotControllerTest extends TestCase
'invites' => $bot->invites,
]);
$response->assertRedirect(route('staff.bots.edit', ['id' => $bot->id]));
$response->assertRedirect(route('staff.bots.index'));
}
}

View File

@@ -5,6 +5,7 @@ namespace Tests\Feature\Http\Controllers\Staff;
use App\Models\Chatroom;
use App\Models\Group;
use App\Models\User;
use Database\Seeders\ChatroomTableSeeder;
use Database\Seeders\GroupsTableSeeder;
use Tests\TestCase;
@@ -35,6 +36,7 @@ class ChatRoomControllerTest extends TestCase
public function destroy_returns_an_ok_response(): void
{
$this->seed(GroupsTableSeeder::class);
$this->seed(ChatroomTableSeeder::class);
$user = $this->createStaffUser();
$chatroom = Chatroom::factory()->create();