} else { echo json_encode(array('error' => true, 'message' => 'Salida fallida. Nombre de usuario incorrecto')); } }); $app->post('/products', 'authenticate', function () use($app) { $json = $app->request->getBody(); $data = json_decode($json); $name = $data->name; $price = $data->price; $url_image = $data->url; $response = array(); global $user_id; $db = new products(); $idProduct = $db->createProduct($user_id, $name, $price, $url_image); if ($idProduct != NULL) { $db->updateProduct($user_id, $idProduct, $price); echo json_encode(array('error' => false, 'message' => 'Producto creado satisfactoriamente', 'id' => $idProduct)); } else { echo json_encode(array('error' => true, 'message' => 'Error en la creacion del producto. Por favor intente de nuevo')); } }); $app->put('/products/:id', 'authenticate', function ($idProduct) use($app) { $name = NULL; $price = NULL; $url_image = NULL; $json = $app->request->getBody(); $data = json_decode($json); if (!empty($data->name)) { $name = $data->name; } if (!empty($data->price)) {
function updateProduct() { include_once "products.php"; $product = new products(); $fid = $_REQUEST['fid']; $fname = $_REQUEST['fname']; $fcateg = $_REQUEST['fcateg']; $fprice = $_REQUEST['fprice']; $pic = $_REQUEST['pic']; if (!$product->updateProduct($fid, $fname, $fcateg, $fprice, $pic)) { echo '{"result":0,"message":"Could not add the product"}'; return; } echo '{"result":1,"message":"Succesfully added product"}'; }