From b4fd0afc262fd97db97f4ea7cd0835dde7cdad0b Mon Sep 17 00:00:00 2001 From: Roardom Date: Wed, 28 Jun 2023 07:02:45 +0000 Subject: [PATCH] refactor: use route model binding for staff bots --- .../Controllers/Staff/ChatBotController.php | 24 ++++++++++--------- resources/views/Staff/chat/bot/edit.blade.php | 2 +- .../views/Staff/chat/bot/index.blade.php | 10 ++++---- routes/web.php | 14 +++++------ .../Staff/ChatBotControllerTest.php | 12 +++++----- .../Staff/ChatRoomControllerTest.php | 2 ++ 6 files changed, 34 insertions(+), 30 deletions(-) diff --git a/app/Http/Controllers/Staff/ChatBotController.php b/app/Http/Controllers/Staff/ChatBotController.php index 5e21a1ae0..88b2cb22e 100644 --- a/app/Http/Controllers/Staff/ChatBotController.php +++ b/app/Http/Controllers/Staff/ChatBotController.php @@ -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, ]); diff --git a/resources/views/Staff/chat/bot/edit.blade.php b/resources/views/Staff/chat/bot/edit.blade.php index 6ec22344f..74d28874d 100644 --- a/resources/views/Staff/chat/bot/edit.blade.php +++ b/resources/views/Staff/chat/bot/edit.blade.php @@ -47,7 +47,7 @@

{{ __('bot.edit-bot') }}: {{ $bot->name }}

-
+ @csrf @method('PATCH')

diff --git a/resources/views/Staff/chat/bot/index.blade.php b/resources/views/Staff/chat/bot/index.blade.php index cb2d2ba62..b6ec01ef8 100644 --- a/resources/views/Staff/chat/bot/index.blade.php +++ b/resources/views/Staff/chat/bot/index.blade.php @@ -50,7 +50,7 @@ @foreach($bots as $bot) - + {{ $bot->name }} @@ -77,7 +77,7 @@

  • @csrf