public static function getRoles() { $db = Database::getInstance(DatabaseConfig::DB_INSTANCE); $result = $db->prepare("\n SELECT * FROM roles\n "); $result->execute(); $roles = []; if ($result->rowCount() > 0) { $roles = $result->fetchAll(\PDO::FETCH_KEY_PAIR); } return array_flip($roles); }
public function checkoutCart($userId, $cartId) { $products = $this->getProductsInCart($cartId); $promoRepo = new PromotionsRepository(); $this->db->beginTransaction(); $price = 0; foreach ($products as $product) { if ($product['quantity'] > $product['product_max_quantity']) { $this->db->rollBack(); throw new \Exception("Product quantity"); } $discount = $promoRepo->getTheBiggestPromotion($userId, $product['id'], $product['category_id']); $price += $product['price'] * $product['quantity'] - $product['price'] * $product['quantity'] * $discount / 100; $statement = $this->db->prepare("\n UPDATE products\n SET quantity = quantity - ?\n WHERE id = ?\n "); $statement->execute([$product['quantity'], $product['id']]); } $statement = $this->db->prepare("\n SELECT cash FROM users WHERE id = ?\n "); $statement->execute([$userId]); $userCash = $statement->fetch(); if ($price > $userCash) { $this->db->rollBack(); return false; } $statement = $this->db->prepare("\n UPDATE users\n SET cash = cash - ?\n WHERE id = ?\n "); $statement->execute([$price, $userId]); if ($statement->rowCount() <= 0) { $this->db->rollBack(); return false; } $statement = $this->db->prepare("\n DELETE FROM cart_products\n WHERE cart_id = ?\n "); $statement->execute([$cartId]); if ($statement->rowCount() < 0) { $this->db->rollBack(); return false; } foreach ($products as $product) { $statement = $this->db->prepare("\n INSERT INTO user_products (user_id, name, quantity, details, price)\n VALUES (?, ?, ?, ?, ?)\n "); $statement->execute([$userId, $product['name'], $product['quantity'], $product['details'], $product['price']]); if ($statement->rowCount() < 0) { $this->db->rollBack(); return false; } } $this->db->commit(); return true; }
public function __construct() { $this->db = Database::getInstance(DatabaseConfig::DB_INSTANCE); }
private function registerDatabaseConfiguration() { \DF\Core\Database::setInstance(\DF\Config\DatabaseConfig::DB_INSTANCE, \DF\Config\DatabaseConfig::DB_DRIVER, \DF\Config\DatabaseConfig::DB_USER, \DF\Config\DatabaseConfig::DB_PASSWORD, \DF\Config\DatabaseConfig::DB_NAME, \DF\Config\DatabaseConfig::DB_HOST); }
private function isBannedByIP() { $db = Database::getInstance(DatabaseConfig::DB_INSTANCE); $statement = $db->prepare("\n SELECT ip_address FROM blacklist\n "); $statement->execute(); $blacklist = $statement->fetchAll(); if (in_array($_SERVER['REMOTE_ADDR'], $blacklist)) { header("location: http://www.google.com/"); exit; } }