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;
 }
 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;
 }
Beispiel #3
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();
 }
Beispiel #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');
 }
 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");
     }
 }
 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");
     }
 }
<?php

$uri = $_SERVER['REQUEST_URI'];
$self = $_SERVER['PHP_SELF'];
$index = basename($self);
$directories = str_replace($index, '', $self);
$requestStrung = str_replace($directories, '', $uri);
$requestParams = explode("/", $requestStrung);
$controller = array_shift($requestParams);
$action = array_shift($requestParams);
/*
require 'Controllers\UsersController.php';
$fullQualifiedController = '\\SoftUni\\Controllers\\' . ucfirst($controller) . 'Controller';
$controllerInstance = new $fullQualifiedController();
var_dump($controllerInstance);


 spl_autoload_register(function($class){
     $classPath = str_replace("\\", "/", $class);
     require_once $classPath . '.php';
 });
*/
session_start();
require_once 'Application.php';
require_once 'Autoloader.php';
$autoloader = new \SoftUni\Autoloader();
$autoloader->init();
\SoftUni\Core\Database::SetInstance(\SoftUni\Config\DatabaseConfig::DB_INSTANCE, \SoftUni\Config\DatabaseConfig::DB_DRIVER, \SoftUni\Config\DatabaseConfig::DB_USER, \SoftUni\Config\DatabaseConfig::DB_PASS, \SoftUni\Config\DatabaseConfig::DB_NAME, \SoftUni\Config\DatabaseConfig::DB_HOST);
$app = new \SoftUni\Application($controller, $action, $requestParams);
$app->start();
 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);
 }
<?php

use Softuni\Core\Database;
use Softuni\Config\DatabaseConfig;
use Softuni\App;
use Softuni\Autoloader;
session_start();
require_once 'Autoloader.php';
Autoloader::init();
if (isset($_GET['uri'])) {
    $uri = explode('/', rtrim($_GET['uri'], '/'));
    if (count($uri)) {
        $controllerName = array_shift($uri);
    }
    if (count($uri)) {
        $actionName = array_shift($uri);
    }
    $params = $uri;
}
Database::setInstance(DatabaseConfig::DB_INSTANCE_NAME, DatabaseConfig::DB_DRIVER, DatabaseConfig::DB_USERNAME, DatabaseConfig::DB_PASSWORD, DatabaseConfig::DB_NAME, DatabaseConfig::DB_HOST);
$app = new App($controllerName, $actionName, $params);
$app->start();
 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());
 }