/
Database.php
129 lines (116 loc) · 4.12 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php declare(strict_types=1);
namespace Components;
use Models\User;
use PDO;
use PDOStatement;
class Database
{
public $pdo;
private function __construct()
{
$dsn = "mysql:host=mysql-host;port=3306;dbname=app;charset=utf8mb4";
$options = [
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
];
$this->pdo = new PDO($dsn, "php-user", "php-pass", $options);
}
public static function instance()
{
static $instance;
if (is_null($instance)) {
$instance = new static();
}
return $instance;
}
public function addUser(string $username, string $password): PDOStatement
{
$stmt = $this->pdo->prepare("INSERT INTO users (`username`, `password`) values (:user, :pass)");
$stmt->execute([
':user' => $username,
':pass' => password_hash($password, PASSWORD_BCRYPT),
]);
return $stmt;
}
public function getUserByUsername(string $formUsername): ?User
{
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE username = :username");
if ($stmt->execute([':username' => $formUsername]) && ($data = $stmt->fetch(PDO::FETCH_ASSOC))) {
return new User($data);
}
return null;
}
public function getUserById(int $id): ?User
{
$stmt = $this->pdo->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
if ($stmt->execute() && ($data = $stmt->fetch(PDO::FETCH_ASSOC))) {
return new User($data);
}
return null;
}
public function getOwnContacts(int $uid): PDOStatement
{
$stmt = $this->pdo->prepare("SELECT * FROM contacts WHERE user_id = :uid");
$stmt->bindParam(':uid', $uid, PDO::PARAM_INT);
$stmt->execute();
return $stmt;
}
public function getOwnContactById(int $ownerId, int $contactId): ?array
{
$stmt = $this->pdo->prepare("SELECT * FROM contacts WHERE id = :cid and user_id = :uid");
$stmt->bindParam(':cid', $contactId, PDO::PARAM_INT);
$stmt->bindParam(':uid', $ownerId, PDO::PARAM_INT);
if ($stmt->execute() && ($data = $stmt->fetch(PDO::FETCH_ASSOC))) {
return $data;
}
return null;
}
public function addContact(
int $ownerId,
string $name,
string $email,
string $phone,
string $address
): PDOStatement
{
$stmt = $this->pdo->prepare("INSERT INTO contacts (user_id, `name`, phone, email, address) " .
"VALUES (:uid, :name, :phone, :email, :address)");
$stmt->bindParam(':uid', $ownerId, PDO::PARAM_INT);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':address', $address);
$stmt->execute();
return $stmt;
}
public function updateContact(
int $contactId,
int $ownerId,
string $name,
string $email,
string $phone,
string $address
): PDOStatement
{
$stmt = $this->pdo->prepare(
"UPDATE contacts SET `name` = :name, phone = :phone, email = :email, address = :address "
. "WHERE id = :cid and user_id = :uid"
);
$stmt->bindParam(':cid', $contactId, PDO::PARAM_INT);
$stmt->bindParam(':uid', $ownerId, PDO::PARAM_INT);
$stmt->bindParam(':name', $name);
$stmt->bindParam(':phone', $phone);
$stmt->bindParam(':email', $email);
$stmt->bindParam(':address', $address);
$stmt->execute();
return $stmt;
}
public function deleteOwnContactById(int $ownerId, int $contactId): PDOStatement
{
$stmt = $this->pdo->prepare("DELETE FROM contacts WHERE id = :cid and user_id = :uid");
$stmt->bindParam(':cid', $contactId, PDO::PARAM_INT);
$stmt->bindParam(':uid', $ownerId, PDO::PARAM_INT);
$stmt->execute();
return $stmt;
}
}