app/Http/Controllers/ReviewController.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 = [
            "user_id" => "required|integer",
            "course_id" => "required|integer",
            "rate" => "required|integer|min:1|max:5",
            "note" => "string"
        ];
        $data = $request->all();
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) {
            return response()->json([
                "status" => "error",
                "message" => $validator->errors()
            ], 400);
        }
        $course = Course::find($request->input('course_id'));
        if (!$course) {
            return response()->json([
                "status" => "error",
                "message" => "course not found"
            ], 404);
        }
        $user = $this->getUser($request->input('user_id'));
        if ($user['status'] == "error") {
            return response()->json([
                'status' => $user['status'],
                'message' => $user['message']
            ], ($user['http_code']) ? $user['http_code'] : 404);
        }
        $isTaked = MyCourse::where('course_id', $request->input('course_id'))
            ->where('user_id', $request->input('user_id'));
        if ($isTaked->count() == 0) {
            return response()->json([
                "status" => "error",
                "message" => "You haven't taken this course"
            ], 400);
        }
        $isExist = Review::where('course_id', $request->input('course_id'))
            ->where('user_id', $request->input('user_id'))->get();
        if ($isExist->count() > 0) {
            return response()->json([
                "status" => "error",
                "message" => "user already review this course"
            ], 409);
        }
        $review = Review::create($data);
        return response()->json([
            "status" => "success",
            "data" => $review
        ]);
    }
public function store ( Request $ request ) { $ rules = [ " user_id " => " required|integer " , " course_id " => " required|integer " , " rate " => " required|integer|min:1|max:5 " , " note " => " string " ] ; $ data = $ request -> all ( ) ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ " status " => " error " , " message " => $ validator -> errors ( ) ] , 400 ) ; } $ course = Course :: find ( $ request -> input ( 'course_id' ) ) ; if ( ! $ course ) { return response ( ) -> json ( [ " status " => " error " , " message " => " course not found " ] , 404 ) ; } $ user = $ this -> getUser ( $ request -> input ( 'user_id' ) ) ; if ( $ user [ 'status' ] == " error " ) { return response ( ) -> json ( [ 'status' => $ user [ 'status' ] , 'message' => $ user [ 'message' ] ] , ( $ user [ 'http_code' ] ) ? $ user [ 'http_code' ] : 404 ) ; } $ isTaked = MyCourse :: where ( 'course_id' , $ request -> input ( 'course_id' ) ) -> where ( 'user_id' , $ request -> input ( 'user_id' ) ) ; if ( $ isTaked -> count ( ) == 0 ) { return response ( ) -> json ( [ " status " => " error " , " message " => " You haven't taken this course " ] , 400 ) ; } $ isExist = Review :: where ( 'course_id' , $ request -> input ( 'course_id' ) ) -> where ( 'user_id' , $ request -> input ( 'user_id' ) ) -> get ( ) ; if ( $ isExist -> count ( ) > 0 ) { return response ( ) -> json ( [ " status " => " error " , " message " => " user already review this course " ] , 409 ) ; } $ review = Review :: create ( $ data ) ; return response ( ) -> json ( [ " status " => " success " , " data " => $ review ] ) ; }
Store a newly created resource in storage.
Store a newly created resource in storage .