Files
watchgether-api/models/Movie.php
T
2026-07-12 15:36:08 +02:00

81 lines
3.2 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) {
// 1. On insère le film
$stmt = $this->db->prepare("INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, is_common) VALUES (?, ?, ?, ?, ?, 0)");
$stmt->execute([$userId, $tmdbId, $title, $poster, $type]);
// 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) {
// 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
$stmt = $this->db->prepare("
SELECT MAX(id) as id, tmdb_id, title, poster_path, media_type, is_common, MAX(added_at) as added_at
FROM movies
WHERE is_common = 1 AND (user_id = ? OR user_id = ?)
GROUP BY tmdb_id, title, poster_path, media_type, is_common
");
$stmt->execute([$myId, $partnerId]);
$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]);
}
}