Fix Movie list
This commit is contained in:
+25
-44
@@ -1,66 +1,47 @@
|
||||
<?php
|
||||
|
||||
// models/Movie.php
|
||||
|
||||
class Movie {
|
||||
private $conn;
|
||||
private $db;
|
||||
|
||||
public function __construct($db) {
|
||||
$this->conn = $db;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
// Ajouter un film
|
||||
public function add($userId, $tmdbId, $title, $posterPath, $mediaType, $isCommon) {
|
||||
$query = "INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, is_common)
|
||||
VALUES (:user_id, :tmdb_id, :title, :poster_path, :media_type, :is_common)";
|
||||
|
||||
$stmt = $this->conn->prepare($query);
|
||||
try {
|
||||
$stmt->execute([
|
||||
':user_id' => $userId,
|
||||
':tmdb_id' => $tmdbId,
|
||||
':title' => $title,
|
||||
':poster_path' => $posterPath,
|
||||
':media_type' => $mediaType,
|
||||
':is_common' => $isCommon ? 1 : 0
|
||||
]);
|
||||
return ["success" => true, "message" => "Film ajouté avec succès."];
|
||||
} catch (PDOException $e) {
|
||||
return ["success" => false, "message" => "Erreur lors de l'ajout : " . $e->getMessage()];
|
||||
}
|
||||
// Ajoute un film (toujours en perso par défaut)
|
||||
public function add($userId, $tmdbId, $title, $posterPath, $type) {
|
||||
$stmt = $this->db->prepare("INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, is_common) VALUES (?, ?, ?, ?, ?, 0)");
|
||||
return $stmt->execute([$userId, $tmdbId, $title, $posterPath, $type]);
|
||||
}
|
||||
|
||||
// Récupérer les 3 listes d'un coup
|
||||
public function getLists($userId, $partnerId) {
|
||||
// 1. Liste perso
|
||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0 ORDER BY added_at DESC");
|
||||
$stmt->execute([$userId]);
|
||||
// Bascule un film en "Commune"
|
||||
public function share($movieId, $userId) {
|
||||
// On vérifie que le film appartient bien à l'utilisateur avant de le partager
|
||||
$stmt = $this->db->prepare("UPDATE movies SET is_common = 1 WHERE id = ? AND user_id = ?");
|
||||
return $stmt->execute([$movieId, $userId]);
|
||||
}
|
||||
|
||||
// Récupère les 3 listes
|
||||
public function getAllLists($myId, $partnerId) {
|
||||
// 1. Perso (mes films non partagés)
|
||||
$stmt = $this->db->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0");
|
||||
$stmt->execute([$myId]);
|
||||
$perso = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
// 2. Liste du partenaire (vide si pas de partenaire)
|
||||
// 2. Partenaire (ses films non partagés)
|
||||
$partner = [];
|
||||
if ($partnerId) {
|
||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0 ORDER BY added_at DESC");
|
||||
$stmt = $this->db->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0");
|
||||
$stmt->execute([$partnerId]);
|
||||
$partner = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
}
|
||||
|
||||
// 3. Liste commune (toi + partenaire si lié, sinon juste toi)
|
||||
if ($partnerId) {
|
||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE (user_id = ? OR user_id = ?) AND is_common = 1 ORDER BY added_at DESC");
|
||||
$stmt->execute([$userId, $partnerId]);
|
||||
} else {
|
||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 1 ORDER BY added_at DESC");
|
||||
$stmt->execute([$userId]);
|
||||
}
|
||||
// 3. Commune (tous les films avec is_common = 1)
|
||||
$stmt = $this->db->prepare("SELECT * FROM movies WHERE is_common = 1");
|
||||
$stmt->execute();
|
||||
$common = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||
|
||||
return [
|
||||
"success" => true,
|
||||
"lists" => [
|
||||
"perso" => $perso,
|
||||
"partner" => $partner,
|
||||
"common" => $common
|
||||
]
|
||||
];
|
||||
return ["perso" => $perso, "partner" => $partner, "common" => $common];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user