app/Http/Controllers/MyCourseController.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 = [
            'course_id' => "required|integer",
            'user_id' => "required|integer"
        ];
        $data = $request->all();
        $validator = Validator::make($data, $rules);
        if ($validator->fails()) {
            return response()->json([
                "status" => "error",
                "message" => $validator->errors()
            ], 400);
        }
        $course_id = $request->input('course_id');
        $course = Course::find($course_id);
        if (!$course) {
            return response()->json([
                "status" => "error",
                "message" => "course not found"
            ], 404);
        }
        $user_id = $request->input('user_id');
        $user = $this->getUser($request->input('user_id'));
        if ($user['status'] == "error") {
            return response()->json([
                'status' => $user['status'],
                'message' => $user['message']
            ], 404);
        }
        $isExist = MyCourse::where('course_id', $course_id)
            ->where('user_id', $user_id)->get();
        if ($isExist->count() > 0) {
            return response()->json([
                "status" => "error",
                "message" => "user already take this course"
            ], 409);
        }
        if ($course->type === 'premium') {
            if ($course->price === 0) {
                return response()->json([
                    'status' => 'error',
                    'message' => 'Price can\'t be 0'
                ]);
            }
            $order = postOrder([
                'user' => $user['data'],
                'course' => $course->toArray()
            ]);
            if ($order['status'] === 'error') {
                return  response()->json([
                    'status' => $order['status'],
                    'message' => $order['message'],
                ], $order['http_code']);
            }
            return response()->json([
                'status' => $order['status'],
                'data' => $order['data']
            ]);
        } else {
            $myCourse = MyCourse::create($data);
            return response()->json([
                "status" => "success",
                "data" => $myCourse
            ]);
        }
    }
public function store ( Request $ request ) { $ rules = [ 'course_id' => " required|integer " , 'user_id' => " required|integer " ] ; $ data = $ request -> all ( ) ; $ validator = Validator :: make ( $ data , $ rules ) ; if ( $ validator -> fails ( ) ) { return response ( ) -> json ( [ " status " => " error " , " message " => $ validator -> errors ( ) ] , 400 ) ; } $ course_id = $ request -> input ( 'course_id' ) ; $ course = Course :: find ( $ course_id ) ; if ( ! $ course ) { return response ( ) -> json ( [ " status " => " error " , " message " => " course not found " ] , 404 ) ; } $ user_id = $ request -> input ( 'user_id' ) ; $ user = $ this -> getUser ( $ request -> input ( 'user_id' ) ) ; if ( $ user [ 'status' ] == " error " ) { return response ( ) -> json ( [ 'status' => $ user [ 'status' ] , 'message' => $ user [ 'message' ] ] , 404 ) ; } $ isExist = MyCourse :: where ( 'course_id' , $ course_id ) -> where ( 'user_id' , $ user_id ) -> get ( ) ; if ( $ isExist -> count ( ) > 0 ) { return response ( ) -> json ( [ " status " => " error " , " message " => " user already take this course " ] , 409 ) ; } if ( $ course -> type === 'premium' ) { if ( $ course -> price === 0 ) { return response ( ) -> json ( [ 'status' => 'error' , 'message' => 'Price can\'t be 0' ] ) ; } $ order = postOrder ( [ 'user' => $ user [ 'data' ] , 'course' => $ course -> toArray ( ) ] ) ; if ( $ order [ 'status' ] === 'error' ) { return response ( ) -> json ( [ 'status' => $ order [ 'status' ] , 'message' => $ order [ 'message' ] , ] , $ order [ 'http_code' ] ) ; } return response ( ) -> json ( [ 'status' => $ order [ 'status' ] , 'data' => $ order [ 'data' ] ] ) ; } else { $ myCourse = MyCourse :: create ( $ data ) ; return response ( ) -> json ( [ " status " => " success " , " data " => $ myCourse ] ) ; } }
Store a newly created resource in storage.
Store a newly created resource in storage .