public function setUp()
    {
        $this->db = DB::getInstance();
        $this->curl = new CURL(self::URL);
        $this->db->query('
			DELETE FROM owner;
			ALTER SEQUENCE owner_id_seq RESTART 1;
		');
    }
Example #2
0
 public function delete($categoryId)
 {
     $conn = DB::connect();
     $deleteCategorySql = 'UPDATE categories SET isDeleted = 1 WHERE id="' . $categoryId . '"';
     if ($conn->query($deleteCategorySql)) {
         View::$viewBag['successMessage'] = "Category successfully deleted";
     } else {
         View::$viewBag['errors'][] = "Database error";
     }
 }
Example #3
0
 public function __construct($class)
 {
     $this->db = DB::getInstance();
     $this->class = $class;
     $reflectionClass = new ReflectionClass($class);
     $docComment = $reflectionClass->getDocComment();
     $docComment = str_replace(' ', '', $docComment);
     if (preg_match('/@Entity/', $docComment)) {
         preg_match('/@Table\\(name="([a-z]+)"\\)/', $docComment, $matches);
         $this->table = $matches[1];
         foreach ($reflectionClass->getProperties() as $reflectionProperty) {
             $field = new stdClass();
             $field->name = $reflectionProperty->getName();
             $docComment = $reflectionProperty->getDocComment();
             $docComment = str_replace(' ', '', $docComment);
             if (!preg_match('/@Transient/', $docComment)) {
                 if (preg_match('/@Id/', $docComment)) {
                     $this->id = $field->name;
                 }
                 preg_match('/@Column\\(name="([a-z_]+)"\\)/', $docComment, $matches);
                 $field->column = $matches[1];
                 if (preg_match('/@NotNull/', $docComment)) {
                     $field->notnull = true;
                 }
                 preg_match('/@Type([a-z]+)/', $docComment, $matches);
                 $field->type = $matches[1];
                 if (preg_match('/@Size\\(max=\\"([a-z]+)\\"\\)/', $docComment, $matches)) {
                     $field->size = $matches[1];
                 }
                 // 					if(preg_match('/@Default(value=\"([a-z]+)\"\)/',$docComment,$matches))
                 // 					{
                 // 						$field->default = $matches[1];
                 // 					}
                 $this->fields[$field->name] = $field;
             }
         }
     }
 }
Example #4
0
 public function checkout()
 {
     $db = DB::connect();
     $products = $_SESSION['cart']['products'];
     $productsId = [];
     $productsPrice = 0;
     foreach ($products as $id => $product) {
         $productsId[] = $id;
         $getProductPriceSql = 'SELECT price FROM products WHERE id="' . $id . '"';
         $productsPrice += $db->query($getProductPriceSql)->fetch()["price"] * $product["quantity"];
     }
     $userInfoSql = 'SELECT id, cash FROM users WHERE username="******"';
     $userInfo = $db->query($userInfoSql)->fetch();
     if ($productsPrice > $userInfo["cash"]) {
         View::$viewBag['errors'][] = "You don't have enough money";
     } else {
         $removeUserCashSql = 'UPDATE users SET cash = cash - "' . $productsPrice . '" WHERE id="' . $userInfo['id'] . '"';
         $db->query($removeUserCashSql);
         foreach ($products as $id => $product) {
             $productExistsSql = 'SELECT product_id FROM product_user WHERE product_id = "' . $id . '"
                                 AND user_id = "' . $userInfo['id'] . '"';
             if ($db->query($productExistsSql)->rowCount() == 0) {
                 $buyProductSql = 'INSERT INTO product_user(product_id, user_id, quantity)
                           VALUES("' . $id . '", "' . $userInfo['id'] . '", "' . $product["quantity"] . '")';
             } else {
                 $buyProductSql = 'UPDATE product_user SET quantity = quantity + "' . $product["quantity"] . '"
                                   WHERE  product_id = "' . $id . '" AND user_id = "' . $userInfo['id'] . '"';
             }
             $db->query($buyProductSql);
             $removeQuantitySql = 'UPDATE products SET quantity = quantity - "' . $product["quantity"] . '" WHERE id="' . $id . '"';
             $db->query($removeQuantitySql);
             unset($_SESSION['cart']['products'][$id]);
         }
         header("Location: " . __MAIN_URL__ . "Users/Products");
         exit;
     }
 }
Example #5
0
 public function addProductToUser($username, $productId, $quantity)
 {
     $errors = [];
     if ($quantity <= 0) {
         $errors[] = "Invalid quantity";
     }
     $db = DB::connect();
     $getUserId = 'SELECT id FROM users WHERE username =  "******"';
     $userId = $db->query($getUserId);
     if ($userId->rowCount() > 0) {
         $userId = $userId->fetch()["id"];
     } else {
         $errors[] = "Invalid username";
     }
     $checkProductSql = 'SELECT id FROM products WHERE id = "' . $productId . '"';
     if ($db->query($checkProductSql)->rowCount() == 0) {
         $errors[] = "Invalid product";
     }
     if (count($errors) == 0) {
         $productExistsSql = 'SELECT product_id FROM product_user WHERE product_id = "' . $productId . '"
                                 AND user_id = "' . $userId . '"';
         if ($db->query($productExistsSql)->rowCount() == 0) {
             $addProductSql = 'INSERT INTO product_user(product_id, user_id, quantity)
                           VALUES("' . $productId . '", "' . $userId . '", "' . $quantity . '")';
         } else {
             $addProductSql = 'UPDATE product_user SET quantity = quantity + "' . $quantity . '"
                                   WHERE  product_id = "' . $productId . '" AND user_id = "' . $userId . '"';
         }
         $db->query($addProductSql);
         View::$viewBag['successMessage'] = "Product added";
     } else {
         View::$viewBag['errors'] = $errors;
     }
 }
Example #6
0
 public function setUp()
 {
     $this->db = DB::getInstance();
     $this->dao = new OwnerDAO();
 }
 public function setUp()
 {
     $this->db = DB::getInstance();
     $this->service = new AccountService();
     $this->db->query("INSERT INTO owner VALUES (1, 'temp','temp');");
 }
Example #8
0
 public function editUser($username, $email, $role, $cash, $userId)
 {
     $errors = [];
     if (strlen($username) == 0) {
         $errors[] = "Invalid username";
     }
     if (strlen($email) == 0) {
         $errors[] = "Invalid email";
     }
     if ($cash < 0) {
         $errors[] = "Invalid cash";
     }
     if (count($errors) == 0) {
         $db = DB::connect();
         $editUserSql = 'UPDATE users SET
                           username = "******",
                           email = "' . $email . '",
                           role = "' . $role . '",
                           cash = "' . $cash . '"
                           WHERE id = "' . $userId . '"';
         $db->query($editUserSql);
         View::$viewBag['successMessage'] = "User edited";
     } else {
         View::$viewBag['errors'] = $errors;
     }
 }
 public function setUp()
 {
     $this->db = DB::getInstance();
     $this->service = new OwnerService();
 }