/** * Register a product via barcode and uid (user id) * * @return \Illuminate\Http\JsonResponse */ public function scan() { header("Access-Control-Allow-Origin: *"); $response = new YYResponse(FALSE, 'Something went wrong.'); $validator = Validator::make(Request::all(), ['barcode' => 'required|integer', 'uid' => 'required|integer']); if ($validator->fails()) { $response->errors = $validator->errors(); return response()->json($response); } $product = Product::where(['barcode' => Request::get('barcode')])->get(); $product = empty($product[0]) ? NULL : $product[0]; if (empty($product)) { $product = new Product(); $product->barcode = Request::get('barcode'); $product->uid = Request::get('uid'); $product->save(); } if (!empty($product)) { $response->state = TRUE; $response->message = NULL; $response->data = $product; $response->data->reviews = Product::getReviewsById($product->id); $response->data->rating = Product::getRatingById($product->id); $response->data->rating->average = $response->data->rating->count > 0 ? $response->data->rating->total / $response->data->rating->count : 0; $response->data->review = Product::getReviewsById($product->id, $product->uid); } return response()->json($response); }