示例#1
1
 /**
  * Inserts a value from JSON input. The input should be formatted like this:
  * {
  *      'chart_id': <chart id> [integer]
  *      'auth': <chart auth> [string]
  *      'value': <chart value> [double]
  * }
  *
  * @return \Illuminate\Http\JsonResponse
  */
 public function insert()
 {
     if (Input::isJson()) {
         $input = Input::json()->all();
         try {
             $chart = Chart::findOrFail($input['chart_id']);
         } catch (ModelNotFoundException $e) {
             return response()->json(['success' => false, 'message' => 'Chart not found'], 400);
         }
         // Check if correct chart password
         if ($input['auth'] === $chart['auth']) {
             $val = $input['value'];
             if (!$chart->insertValue($val)) {
                 // Error while inserting
                 return response()->json(['success' => false, 'message' => 'Error inserting value'], 400);
             }
             // Successful insert
             return response()->json(['success' => true, 'message' => 'Value successfully inserted'], 200);
         } else {
             return response()->json(['success' => false, 'message' => 'Incorrect auth for chart'], 401);
         }
     } else {
         return response()->json(['success' => false, 'message' => 'Incorrect input type. Expected JSON'], 400);
     }
 }