diff --git a/controllers/MovieController.php b/controllers/MovieController.php new file mode 100644 index 0000000..2044fc6 --- /dev/null +++ b/controllers/MovieController.php @@ -0,0 +1,49 @@ +db = $db; + $this->movieModel = new Movie($this->db); + } + + // INTERCEPTE L'AJOUT + public function addMovie($data) { + if (empty($data['user_id']) || empty($data['tmdb_id']) || empty($data['title'])) { + http_response_code(400); + echo json_encode(["success" => false, "message" => "Données incomplètes (user_id, tmdb_id, title requis)."]); + return; + } + + $mediaType = $data['media_type'] ?? 'film'; + $isCommon = isset($data['is_common']) ? (bool)$data['is_common'] : false; + $posterPath = $data['poster_path'] ?? null; + + $result = $this->movieModel->add($data['user_id'], $data['tmdb_id'], $data['title'], $posterPath, $mediaType, $isCommon); + + http_response_code($result['success'] ? 201 : 400); + echo json_encode($result); + } + + // INTERCEPTE LA RECUPERATION DES LISTES + public function getLists($data) { + if (empty($data['user_id'])) { + http_response_code(400); + echo json_encode(["success" => false, "message" => "ID utilisateur requis."]); + return; + } + + // On a besoin du partner_id pour choper sa liste, s'il n'est pas envoyé on le met à null + $partnerId = $data['partner_id'] ?? null; + + $result = $this->movieModel->getLists($data['user_id'], $partnerId); + + http_response_code(200); + echo json_encode($result); + } +} \ No newline at end of file diff --git a/models/Movie.php b/models/Movie.php new file mode 100644 index 0000000..813e2b7 --- /dev/null +++ b/models/Movie.php @@ -0,0 +1,66 @@ +conn = $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()]; + } + } + + // 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]); + $perso = $stmt->fetchAll(PDO::FETCH_ASSOC); + + // 2. Liste du partenaire (vide si pas de partenaire) + $partner = []; + if ($partnerId) { + $stmt = $this->conn->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. 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]); + } + $common = $stmt->fetchAll(PDO::FETCH_ASSOC); + + return [ + "success" => true, + "lists" => [ + "perso" => $perso, + "partner" => $partner, + "common" => $common + ] + ]; + } +} \ No newline at end of file