2283c94a22
getLists() returned the common list via MAX(id) grouped across both users' rows, so the id handed back could belong to either partner. remove()/markWatched() scope by (id, user_id), so whenever it picked the partner's id, the query silently matched zero rows while still reporting success. Now each user's own is_common=1 rows are returned directly, so the id is always theirs. Also fixes the frontend never recognizing common movies as "already added" (see watchgether-web). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
92 lines
3.9 KiB
PHP
92 lines
3.9 KiB
PHP
<?php
|
|
|
|
// models/Movie.php
|
|
|
|
class Movie {
|
|
private $db;
|
|
|
|
public function __construct($db) {
|
|
$this->db = $db;
|
|
}
|
|
|
|
public function add($userId, $tmdbId, $title, $poster, $type, $releaseDate = null) {
|
|
// 1. On insère le film
|
|
$stmt = $this->db->prepare("INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, release_date, is_common) VALUES (?, ?, ?, ?, ?, ?, 0)");
|
|
$stmt->execute([$userId, $tmdbId, $title, $poster, $type, $releaseDate ?: null]);
|
|
|
|
// 2. On récupère l'ID du partenaire (supposons que tu as une table 'users' avec un champ 'partner_id')
|
|
$stmt = $this->db->prepare("SELECT partner_id FROM users WHERE id = ?");
|
|
$stmt->execute([$userId]);
|
|
$partnerId = $stmt->fetchColumn();
|
|
|
|
// 3. Si un partenaire existe, on cherche s'il a déjà ce film
|
|
if ($partnerId) {
|
|
$stmt = $this->db->prepare("SELECT id FROM movies WHERE user_id = ? AND tmdb_id = ? AND is_common = 0");
|
|
$stmt->execute([$partnerId, $tmdbId]);
|
|
$partnerMovie = $stmt->fetch();
|
|
|
|
// 4. Si le partenaire a le film, on bascule les DEUX en "common"
|
|
if ($partnerMovie) {
|
|
$stmt = $this->db->prepare("UPDATE movies SET is_common = 1 WHERE tmdb_id = ? AND (user_id = ? OR user_id = ?)");
|
|
$stmt->execute([$tmdbId, $userId, $partnerId]);
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public function share($movieId, $userId) {
|
|
$stmt = $this->db->prepare("UPDATE movies SET is_common = 1 WHERE id = ? AND user_id = ?");
|
|
return $stmt->execute([$movieId, $userId]);
|
|
}
|
|
|
|
// C'est celle-ci qui manquait !
|
|
public function getLists($myId, $partnerId) {
|
|
// --- LA CORRECTION MAGIQUE ---
|
|
// On va vérifier direct en BDD qui est ton vrai partenaire actuel à la seconde près.
|
|
$stmt = $this->db->prepare("SELECT partner_id FROM users WHERE id = ?");
|
|
$stmt->execute([$myId]);
|
|
// Si fetchColumn ne trouve rien, on force à null
|
|
$partnerId = $stmt->fetchColumn() ?: null;
|
|
|
|
// 1. Perso
|
|
$stmt = $this->db->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0 ORDER BY added_at DESC");
|
|
$stmt->execute([$myId]);
|
|
$perso = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
// 2. Partenaire
|
|
$partner = [];
|
|
if ($partnerId) {
|
|
$stmt = $this->db->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0 ORDER BY added_at DESC");
|
|
$stmt->execute([$partnerId]);
|
|
$partner = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
}
|
|
|
|
// 3. Commune
|
|
// On renvoie MA propre ligne (pas une fusion avec celle du/de la partenaire) :
|
|
// chacun a sa propre ligne is_common=1 pour le même film, et remove/mark-watched
|
|
// ont besoin de MON id pour agir sur MA ligne, pas une autre.
|
|
$stmt = $this->db->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 1 ORDER BY added_at DESC");
|
|
$stmt->execute([$myId]);
|
|
$common = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
|
return ["perso" => $perso, "partner" => $partner, "common" => $common];
|
|
}
|
|
|
|
public function remove($movieId, $userId) {
|
|
$stmt = $this->db->prepare("DELETE FROM movies WHERE id = ? AND user_id = ?");
|
|
return $stmt->execute([$movieId, $userId]);
|
|
}
|
|
|
|
public function markWatched($movieId, $userId) {
|
|
// Si tu n'as pas de colonne 'watched', ajoute-la en BDD : ALTER TABLE movies ADD COLUMN watched BOOLEAN DEFAULT 0;
|
|
$stmt = $this->db->prepare("UPDATE movies SET watched = 1 WHERE id = ? AND user_id = ?");
|
|
return $stmt->execute([$movieId, $userId]);
|
|
}
|
|
|
|
public function getRealPartnerId($userId) {
|
|
$stmt = $this->db->prepare("SELECT partner_id FROM users WHERE id = ?");
|
|
$stmt->execute([$userId]);
|
|
return $stmt->fetchColumn() ?: null;
|
|
}
|
|
} |