Ejemplo n.º 1
0
 public static function create($username, $total)
 {
     $sql = 'INSERT INTO orders ' . '(username, total) ' . 'VALUES (:username, :total)';
     $params = array('username' => $username, 'total' => $total);
     $id = Database::update($sql, $params);
     return new Order($username, $total, $id, Database::now());
 }
Ejemplo n.º 2
0
 public static function create($name, $price, $quantity, $image_url)
 {
     $sql = 'INSERT INTO products ' . '(name, price, quantity, image_url) ' . 'VALUES (:name, :price, :quantity, :image_url)';
     $params = array('name' => $name, 'price' => $price, 'quantity' => $quantity, 'image_url' => $image_url);
     $id = Database::update($sql, $params);
     return new Product($name, $price, $quantity, $id, $image_url, Database::now());
 }
Ejemplo n.º 3
0
 public static function retrieve($username = null, $token = null)
 {
     $params = array();
     $sql = 'SELECT username, address, password, salt, is_admin, ' . 'token, token_expiration ' . 'FROM users';
     if (!is_null($username)) {
         $sql .= ' WHERE username=:username';
         $params['username'] = $username;
     } else {
         if (!is_null($token)) {
             $sql .= ' WHERE token=:token AND token_expiration>=:now';
             $params['token'] = $token;
             $params['now'] = Database::now();
         }
     }
     $result = Database::select($sql, $params);
     $users = array();
     foreach ($result as $r) {
         $is_admin = $r['is_admin'] === '1' ? true : false;
         $users[] = new User($r['username'], $r['address'], $r['password'], $r['salt'], $is_admin, $r['token'], $r['token_expiration']);
     }
     if (!is_null($username)) {
         if (empty($users)) {
             throw new \Lib\Exceptions\NotFoundException();
         }
         return $users[0];
     }
     if (!is_null($token)) {
         if (empty($users)) {
             throw new \Lib\Exceptions\UnauthorizedException();
         }
         return $users[0];
     }
     return $users;
 }