-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFriendshipsTable.php
65 lines (51 loc) · 1.87 KB
/
FriendshipsTable.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
<?php
require_once __DIR__ . '/Table.php';
class FriendshipsTable extends Table
{
public function __construct(PDO $pdo)
{
parent::__construct($pdo, 'friendships');
}
public function addFriendship(int $idUser, int $idFriend): void
{
$stmt = $this->pdo->prepare("INSERT INTO " . $this->name . " (user_id, friend_id) VALUES (:id_user, :id_friend)");
$stmt->bindValue(':id_user', $idUser);
$stmt->bindValue(':id_friend', $idFriend);
$stmt->execute();
}
public function removeFriendship(int $idUser, int $idFriend): void
{
$stmt = $this->pdo->prepare("DELETE FROM " . $this->name . " WHERE user_id = :id_user AND friend_id = :id_friend");
$stmt->bindValue(':id_user', $idUser);
$stmt->bindValue(':id_friend', $idFriend);
$stmt->execute();
}
public function findFriends(int $id): array
{
$stmt = $this->pdo->prepare("
SELECT
CONCAT(u.user_name, ' ', u.user_lastname) AS complete_name,
u.user_picture
FROM Users u
INNER JOIN " . $this->name . " f ON u.id_user = f.friend_id
WHERE f.user_id = :id
");
$stmt->execute(['id' => $id]);
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
public function isFriend(int $currentUserId, int $userId): bool
{
try {
$stmt = $this->pdo->prepare("SELECT COUNT(*) FROM " . $this->name . " WHERE user_id = :currentUserId AND friend_id = :userId");
$stmt->execute([
':currentUserId' => $currentUserId,
':userId' => $userId
]);
$count = $stmt->fetchColumn();
return $count > 0;
} catch (PDOException $e) {
echo "Erreur lors de la vérification de l'ami : " . $e->getMessage();
return false;
}
}
}