app/Http/Controllers/MessagesController.php
/**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
Store a newly created resource in storage.
Store a newly created resource in storage .
public function store(Request $request)
    {
        $rules = [
            'message' => 'required|string|max:1000',
            'recipient' => 'required'
        ];
        $validator = Validator::make($request->all(), $rules);
        if($validator->fails()){
            return response()->json($validator->errors(), 400);
        }
        $conversation = Conversation::find($request['conversation_id']);
        if($conversation == null){
            $chats_with_receiver = Conversation::where(function($conv){
                $conv->where('sender_id', Auth::user()['id'])->where('receiver_id', request()['recipient']);
            })->orWhere(function($conv){
                $conv->where('receiver_id', Auth::user()['id'])->where('sender_id', request()['recipient']);
            })->get();
            if(count($chats_with_receiver) > 0){
                return response()->json(["error" => "conversation id does not match"], 401);
            }
            $conversation = Conversation::create([
                "receiver_id" => $request["recipient"], 
                "sender_id" => Auth::user()['id']
            ]);
            $message = Message::create([
                "sender_id" => Auth::user()['id'],
                "message" => $request["message"]
            ]);
        } else if (!($conversation['sender_id'] == Auth::id() || $conversation['receiver_id'] == Auth::id())){
            return response()->json(["error" => "not authorized"], 401);
        }
        $message = Message::create([
            "sender_id" => Auth::user()['id'],
            "message" => $request["message"]
        ]);
        $message->conversation_id = $conversation["id"];
        $message->save();
        return response()->json(["success" => $message], 201);
    }
public function store ( Request $ request ) { $ rules = [ 'message' => 'required|string|max:1000' , 'recipient' => 'required' ] ; $ validator = Validator :: make ( $ request -> all ( ) , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( $ validator -> errors ( ) , 400 ) ; } $ conversation = Conversation :: find ( $ request [ 'conversation_id' ] ) ; if ( $ conversation == null ) { $ chats_with_receiver = Conversation :: where ( function ( $ conv ) { $ conv -> where ( 'sender_id' , Auth :: user ( ) [ 'id' ] ) -> where ( 'receiver_id' , request ( ) [ 'recipient' ] ) ; } ) -> orWhere ( function ( $ conv ) { $ conv -> where ( 'receiver_id' , Auth :: user ( ) [ 'id' ] ) -> where ( 'sender_id' , request ( ) [ 'recipient' ] ) ; } ) -> get ( ) ; if ( count ( $ chats_with_receiver ) > 0 ) { return response ( ) -> json ( [ " error " => " conversation id does not match " ] , 401 ) ; } $ conversation = Conversation :: create ( [ " receiver_id " => $ request [ " recipient " ] , " sender_id " => Auth :: user ( ) [ 'id' ] ] ) ; $ message = Message :: create ( [ " sender_id " => Auth :: user ( ) [ 'id' ] , " message " => $ request [ " message " ] ] ) ; } else if ( ! ( $ conversation [ 'sender_id' ] == Auth :: id ( ) || $ conversation [ 'receiver_id' ] == Auth :: id ( ) ) ) { return response ( ) -> json ( [ " error " => " not authorized " ] , 401 ) ; } $ message = Message :: create ( [ " sender_id " => Auth :: user ( ) [ 'id' ] , " message " => $ request [ " message " ] ] ) ; $ message -> conversation_id = $ conversation [ " id " ] ; $ message -> save ( ) ; return response ( ) -> json ( [ " success " => $ message ] , 201 ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .
// Return if request does not meet requirements // Block users not in conversation from updating // Return existing conversations, if any exist