Add movies lists

This commit is contained in:
2026-07-11 10:55:59 +02:00
parent 45e5f8d6c6
commit d8aa88a8fb
2 changed files with 115 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
<?php
// controllers/MovieController.php
require_once __DIR__ . '/../models/Movie.php';
class MovieController {
private $db;
private $movieModel;
public function __construct($db) {
$this->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);
}
}
+66
View File
@@ -0,0 +1,66 @@
<?php
// models/Movie.php
class Movie {
private $conn;
public function __construct($db) {
$this->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
]
];
}
}