ChatController.php 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Jobs\SetChatTitleJob;
  4. use App\Models\Chat;
  5. use App\Services\MessageService;
  6. use Illuminate\Http\Request;
  7. use Illuminate\Support\Facades\Bus;
  8. class ChatController extends Controller
  9. {
  10. /**
  11. * @throws \Throwable
  12. */
  13. public function store(MessageService $service, Request $request)
  14. {
  15. $chat = $request->user()->chats()->create();
  16. $service->create($chat, $request->string('message'));
  17. $batch = Bus::batch([new SetChatTitleJob($chat)])
  18. ->name('Generate Chat Title')
  19. ->onQueue('secondary')
  20. ->dispatch();
  21. $chat->batches()->attach($batch->id);
  22. return redirect()->route('chats.show', $chat->id);
  23. }
  24. public function create()
  25. {
  26. return inertia('Chat/Create');
  27. }
  28. public function show(Chat $chat)
  29. {
  30. $chat->load('messages');
  31. return inertia('Chat/View', [
  32. 'chat' => fn() => $chat->toResource(),
  33. ]);
  34. }
  35. public function destroy(Chat $chat)
  36. {
  37. $chat->delete();
  38. return redirect()->route('chats.index');
  39. }
  40. }