HomeController.php 1.15 KB
<?php

namespace App\Http\Controllers;

use App\Models\Message;
use Illuminate\Http\Request;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('home');
    }

    public function send_message(Request $request)
    {
        $user_id = Auth()->user()->id;

        $message_params = [
            'text' => $request->input('text'),
            'reply_message_id' => $request->input('reply_message_id', null),
        ];

        $new_message = Message::add_message(
            $request,
            $user_id,
            $request->input('to_user_id'),
            $message_params
        );

        $user_type = Auth()->user()->is_worker ? 'worker' : 'employer';

        return response()->json([
            'success' => true,
            'url_redirect' => route($user_type . '.dialog', ['chat' => $new_message->chat_id_to])
        ]);
    }
}