Created
April 14, 2019 17:57
-
-
Save Davisonpro/da655b2114115194bd085e2d5f0aedc5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
public function updateProduct($productId ) { | |
$api = $this->api; | |
$payload = $api->request()->post(); | |
$product = new ProductObject( (int) $productId ); | |
if(!Validate::isLoadedObject($product)) { | |
$api->response->setStatus(404); | |
return $api->response([ | |
'success' => false, | |
'message' => 'Product was not found' | |
]); | |
} | |
if (ArrayUtils::has($payload, 'name')) { | |
$name = ArrayUtils::get($payload, 'name'); | |
if ( !Validate::isGenericName($name) ) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'Enter a valid product name' | |
]); | |
} | |
$product->name = $name; | |
} | |
if (ArrayUtils::has($payload, 'description')) { | |
$description = ArrayUtils::get($payload, 'description'); | |
if (!Validate::isCleanHtml($description)) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'Enter a valid description of the product' | |
]); | |
} | |
$product->description = $description; | |
} | |
if (ArrayUtils::has($payload, 'description')) { | |
$price = ArrayUtils::get($payload, 'price'); | |
if (!Validate::isPrice($price)) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'Enter a valid price of the product' | |
]); | |
} | |
$product->price = $price; | |
} | |
if (ArrayUtils::has($payload, 'category_id')) { | |
$category_id = ArrayUtils::get($payload, 'category_id'); | |
if(!Validate::isInt($category_id)) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'Enter a valid category ID of the product' | |
]); | |
} | |
$category = new CategoryObject( (int) $category_id ); | |
if (!Validate::isLoadedObject($category)) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'The category ID (' . $category_id . ') does not exist' | |
]); | |
} | |
$product->category_id = $category->id; | |
} | |
return $api->response([ | |
'success' => false, | |
'message' => 'Unable to update product' | |
]); | |
$ok = $product->save(); | |
// or product->update() | |
if (!$ok) { | |
return $api->response([ | |
'success' => false, | |
'message' => 'Unable to update product' | |
]); | |
} | |
return $api->response([ | |
'success' => true, | |
'message' => 'Product updated successfully' | |
]); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment