model = new ProductModel(); } public function __invoke(ServerRequestInterface $request) { $body = json_decode((string)$request->getBody(), true) ?? []; try { v::key('company_id', v::intType()->positive()) ->key('product_name', v::stringType()->notEmpty()->alnum(' ')) ->key('product_price', v::number()->positive()) ->key('category_id', v::intType()->positive()) ->key('product_is_kitchen', v::optional(v::boolType())) ->assert($body); } catch (ValidationException $e) { return ResponseLib::sendFail("Validation failed: " . $e->getFullMessage(), [], "E_VALIDATE")->withStatus(400); } $companyId = $body['company_id']; $productName = $body['product_name']; $productPrice = (float) $body['product_price']; $categoryId = $body['category_id']; $productIsKitchen = $body['product_is_kitchen'] ?? false; $created = $this->model->createProduct( $productName, $productPrice, (int)$categoryId, (int)$companyId, $productIsKitchen ); return $created ? ResponseLib::sendOk(['created' => true]) : ResponseLib::sendFail("Failed to Create Product", [], "E_DATABASE")->withStatus(402); } }