Ejemplo n.º 1
0
 public function edit($user, $pass, $id)
 {
     $db = Database::getInstance('app');
     $result = $db->prepare("UPDATE users SET password = ?, username = ? WHERE id = ?");
     $result->execute([$pass, $user, $id]);
     return $result->rowCount() > 0;
 }
Ejemplo n.º 2
0
 public function role($userId)
 {
     $db = Database::getInstance('app');
     $result = $db->prepare("\n            SELECT\n                title\n            FROM\n                roles\n            INNER JOIN users\n            ON roles.id = users.roleId\n            WHERE users.id = ?\n        ");
     $result->execute([$userId]);
     return $result->fetch();
 }
Ejemplo n.º 3
0
 public function editUser(User $user)
 {
     $db = Database::getInstance('app');
     $result = $db->prepare("UPDATE users SET password = ?, username = ? WHERE id = ?");
     $result->execute([$user->getPass(), $user->getUsername(), $user->getId()]);
     return $result->rowCount() > 0;
 }
Ejemplo n.º 4
0
 public function getProductById($id)
 {
     $db = Database::getInstance('app');
     $result = $db->prepare("\n            SELECT\n                id, title, price, quantity, dateAdded, categoryId, promotionsId, promoPrice\n            FROM\n                products\n            WHERE\n                id = ?\n        ");
     $result->execute([$id]);
     return $result->fetch();
     throw new \Exception('Cannot find product');
 }
Ejemplo n.º 5
0
 public function login($username, $password)
 {
     $db = Database::getInstance(DatabaseConfig::DB_INSTANCE);
     $result = $db->prepare("\n            SELECT * FROM users WHERE username = ?\n        ");
     $result->execute([$username]);
     if ($result->rowCount() <= 0) {
         throw new \Exception("Invalid username");
     }
     $userRow = $result->fetch();
     if (password_verify($password, $userRow['password'])) {
         return $userRow['id'];
     } else {
         throw new \Exception("Password not match");
     }
 }
Ejemplo n.º 6
0
 public function evolve($buildingId)
 {
     $db = Database::getInstance(DatabaseConfig::DB_INSTANCE);
     //check building
     $result = $db->prepare("SELECT id FROM buildings WHERE id = ?");
     $result->execute([$buildingId]);
     if ($result->rowCount() < 0) {
         throw new \Exception("Building with such id does not exists");
     }
     //get resources
     $resources = $db->prepare("\n            SELECT\n              (SELECT gold FROM building_levels WHERE building_id = b.id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS gold,\n              (SELECT food FROM building_levels WHERE building_id = b.id AND level = (SELECT level FROM building_levels WHERE id = ub.level_id) + 1) AS food\n            FROM buildings as b\n            INNER JOIN user_buildings AS ub ON ub.building_id = b.id\n            INNER JOIN building_levels AS bl ON bl.id = ub.level_id\n            WHERE ub.user_id = ? AND b.id = ?;\n        ");
     $userModel = new User();
     $userInfo = $userModel->getInfo(Session::get('id'));
     $userInfo = new UserInformation($userInfo['username'], $userInfo['id'], $userInfo['gold'], $userInfo['food']);
     $resources->execute([$userInfo->getId(), $buildingId]);
     $resourcesData = $resources->fetch();
     if ($userInfo->getFood() < $resourcesData['food'] || $userInfo->getGold() < $resourcesData['gold']) {
         throw new \Exception("No resources");
     }
     //max level
     $maxLevel = $db->prepare("\n            SELECT\n              MAX(bl.level) AS level\n            FROM  building_levels bl\n            WHERE bl.building_id = ?\n        ");
     $maxLevel->execute([$buildingId]);
     $maxLevelData = $maxLevel->fetch();
     //current level
     $currentLevel = $db->prepare("\n            SELECT\n                bl.level\n            FROM user_buildings ub\n                JOIN building_levels bl ON bl.id = ub.level_id\n            WHERE ub.building_id = ?\n        ");
     $currentLevel->execute([$buildingId]);
     $currentLevelData = $currentLevel->fetch();
     if ($maxLevelData['level'] < $currentLevelData['level']) {
         throw new \Exception("Max level reached");
     }
     $this->db->beginTransaction();
     $resourceUpdate = $db->prepare("\n            UPDATE\n              users\n            SET\n              gold = gold - ?, food = food - ?\n            WHERE id = ?\n        ");
     $resourceUpdate->execute([$resourcesData['gold'], $resourcesData['food'], $userInfo->getId()]);
     if ($resourceUpdate->rowCount() > 0) {
         $levelUpdate = $db->prepare("\n                UPDATE\n                  user_buildings ub\n                SET\n                  ub.level_id = (SELECT bl.id FROM building_levels bl WHERE level = ? AND bl.building_id = ub.building_id)\n                WHERE ub.user_id = ? AND ub.building_id = ?\n            ");
         $levelUpdate->execute([$currentLevelData['level'] + 1, $userInfo->getId(), $buildingId]);
         if ($levelUpdate->rowCount() > 0) {
             $db->commit();
             return true;
         } else {
             $db->rollBack();
             throw new \Exception("Level up error");
         }
     } else {
         throw new \Exception("Resource update error");
     }
 }
Ejemplo n.º 7
0
 private static function insert(Building $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (name) VALUES (:name);";
     $result = $db->prepare($query);
     $result->execute([':name' => $model->getName()]);
     $model->setId($db->lastId());
 }
 private static function insert(Conference $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (name,date,hall_id,break1,break2) VALUES (:name, :date, :hall_id, :break1, :break2);";
     $result = $db->prepare($query);
     $result->execute([':name' => $model->getName(), ':date' => $model->getDate(), ':hall_id' => $model->getHall_id(), ':break1' => $model->getBreak1(), ':break2' => $model->getBreak2()]);
     $model->setId($db->lastId());
 }
 private static function insert(BuildingLevel $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (buildings_id,level,gold,food) VALUES (:buildings_id, :level, :gold, :food);";
     $result = $db->prepare($query);
     $result->execute([':buildings_id' => $model->getBuildings_id(), ':level' => $model->getLevel(), ':gold' => $model->getGold(), ':food' => $model->getFood()]);
     $model->setId($db->lastId());
 }
 public function __construct(array $requestParams = [])
 {
     $this->requestParams = $requestParams;
     $this->databaseInstance = Database::getInstance(DatabaseConfig::DB_INSTANCE_NAME);
 }
Ejemplo n.º 11
0
 private static function insert(User $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (username,password,gold,food,grass) VALUES (:username, :password, :gold, :food, :grass);";
     $result = $db->prepare($query);
     $result->execute([':username' => $model->getUsername(), ':password' => $model->getPassword(), ':gold' => $model->getGold(), ':food' => $model->getFood(), ':grass' => $model->getGrass()]);
     $model->setId($db->lastId());
 }
 private static function insert(UserBuilding $model)
 {
     $db = Database::getInstance('app');
     $query = "INSERT INTO users (user_id,building_id,level_id) VALUES (:user_id, :building_id, :level_id);";
     $result = $db->prepare($query);
     $result->execute([':user_id' => $model->getUser_id(), ':building_id' => $model->getBuilding_id(), ':level_id' => $model->getLevel_id()]);
     $model->setId($db->lastId());
 }