Fix Movie list
This commit is contained in:
@@ -1,49 +1,42 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// controllers/MovieController.php
|
// controllers/MovieController.php
|
||||||
|
|
||||||
require_once __DIR__ . '/../models/Movie.php';
|
require_once __DIR__ . '/../models/Movie.php';
|
||||||
|
|
||||||
class MovieController {
|
class MovieController {
|
||||||
private $db;
|
|
||||||
private $movieModel;
|
private $movieModel;
|
||||||
|
|
||||||
public function __construct($db) {
|
public function __construct($db) {
|
||||||
$this->db = $db;
|
$this->movieModel = new Movie($db);
|
||||||
$this->movieModel = new Movie($this->db);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// INTERCEPTE L'AJOUT
|
public function add($data) {
|
||||||
public function addMovie($data) {
|
// Validation basique
|
||||||
if (empty($data['user_id']) || empty($data['tmdb_id']) || empty($data['title'])) {
|
if (!isset($data['tmdb_id'], $data['title'])) {
|
||||||
http_response_code(400);
|
http_response_code(400);
|
||||||
echo json_encode(["success" => false, "message" => "Données incomplètes (user_id, tmdb_id, title requis)."]);
|
echo json_encode(["error" => "Données manquantes"]);
|
||||||
return;
|
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);
|
$res = $this->movieModel->add($data['user_id'], $data['tmdb_id'], $data['title'], $data['poster_path'] ?? null, $data['media_type'] ?? 'film');
|
||||||
echo json_encode($result);
|
echo json_encode(["success" => $res]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function share($data) {
|
||||||
|
if (!isset($data['movie_id'])) {
|
||||||
|
http_response_code(400);
|
||||||
|
echo json_encode(["error" => "ID du film manquant"]);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$res = $this->movieModel->share($data['movie_id'], $data['user_id']);
|
||||||
|
echo json_encode(["success" => $res]);
|
||||||
}
|
}
|
||||||
|
|
||||||
// INTERCEPTE LA RECUPERATION DES LISTES
|
|
||||||
public function getLists($data) {
|
public function getLists($data) {
|
||||||
if (empty($data['user_id'])) {
|
// On récupère les listes. Si tu veux le partner_id, tu peux soit l'extraire
|
||||||
http_response_code(400);
|
// via le user_id dans le modèle, soit l'envoyer dans le $data si besoin.
|
||||||
echo json_encode(["success" => false, "message" => "ID utilisateur requis."]);
|
$lists = $this->movieModel->getLists($data['user_id']);
|
||||||
return;
|
echo json_encode(["success" => true, "lists" => $lists]);
|
||||||
}
|
|
||||||
|
|
||||||
// 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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -17,6 +17,7 @@ if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
|
|||||||
require_once __DIR__ . '/config/database.php';
|
require_once __DIR__ . '/config/database.php';
|
||||||
require_once __DIR__ . '/controllers/UserController.php';
|
require_once __DIR__ . '/controllers/UserController.php';
|
||||||
require_once __DIR__ . '/utils/JWT.php';
|
require_once __DIR__ . '/utils/JWT.php';
|
||||||
|
require_once __DIR__ . '/controllers/MovieController.php';
|
||||||
|
|
||||||
// Initialisation de la BDD et du contrôleur
|
// Initialisation de la BDD et du contrôleur
|
||||||
$database = new Database();
|
$database = new Database();
|
||||||
@@ -24,6 +25,7 @@ $db = $database->getConnection();
|
|||||||
$jwt_secret = $database->jwt_secret;
|
$jwt_secret = $database->jwt_secret;
|
||||||
|
|
||||||
$userController = new UserController($db, $jwt_secret);
|
$userController = new UserController($db, $jwt_secret);
|
||||||
|
$movieController = new MovieController($db);
|
||||||
|
|
||||||
// On récupère la route épurée (ex: si on tape /register, $route vaudra 'register')
|
// On récupère la route épurée (ex: si on tape /register, $route vaudra 'register')
|
||||||
$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2)[0];
|
$request_uri = explode('?', $_SERVER['REQUEST_URI'], 2)[0];
|
||||||
@@ -91,6 +93,27 @@ switch ($route) {
|
|||||||
$userController->unlink($data);
|
$userController->unlink($data);
|
||||||
} else { http_response_code(405); }
|
} else { http_response_code(405); }
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'add-movie':
|
||||||
|
if ($method === 'POST') {
|
||||||
|
$data['user_id'] = getAuthenticatedUserId($jwt_secret);
|
||||||
|
$movieController->add($data);
|
||||||
|
} else { http_response_code(405); }
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'share-movie':
|
||||||
|
if ($method === 'POST') {
|
||||||
|
$data['user_id'] = getAuthenticatedUserId($jwt_secret);
|
||||||
|
$movieController->share($data);
|
||||||
|
} else { http_response_code(405); }
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'get-lists':
|
||||||
|
if ($method === 'POST') {
|
||||||
|
$data['user_id'] = getAuthenticatedUserId($jwt_secret);
|
||||||
|
$movieController->getLists($data);
|
||||||
|
} else { http_response_code(405); }
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
http_response_code(404);
|
http_response_code(404);
|
||||||
|
|||||||
+25
-44
@@ -1,66 +1,47 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
// models/Movie.php
|
// models/Movie.php
|
||||||
|
|
||||||
class Movie {
|
class Movie {
|
||||||
private $conn;
|
private $db;
|
||||||
|
|
||||||
public function __construct($db) {
|
public function __construct($db) {
|
||||||
$this->conn = $db;
|
$this->db = $db;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ajouter un film
|
// Ajoute un film (toujours en perso par défaut)
|
||||||
public function add($userId, $tmdbId, $title, $posterPath, $mediaType, $isCommon) {
|
public function add($userId, $tmdbId, $title, $posterPath, $type) {
|
||||||
$query = "INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, is_common)
|
$stmt = $this->db->prepare("INSERT INTO movies (user_id, tmdb_id, title, poster_path, media_type, is_common) VALUES (?, ?, ?, ?, ?, 0)");
|
||||||
VALUES (:user_id, :tmdb_id, :title, :poster_path, :media_type, :is_common)";
|
return $stmt->execute([$userId, $tmdbId, $title, $posterPath, $type]);
|
||||||
|
|
||||||
$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
|
// Bascule un film en "Commune"
|
||||||
public function getLists($userId, $partnerId) {
|
public function share($movieId, $userId) {
|
||||||
// 1. Liste perso
|
// On vérifie que le film appartient bien à l'utilisateur avant de le partager
|
||||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE user_id = ? AND is_common = 0 ORDER BY added_at DESC");
|
$stmt = $this->db->prepare("UPDATE movies SET is_common = 1 WHERE id = ? AND user_id = ?");
|
||||||
$stmt->execute([$userId]);
|
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);
|
$perso = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
// 2. Liste du partenaire (vide si pas de partenaire)
|
// 2. Partenaire (ses films non partagés)
|
||||||
$partner = [];
|
$partner = [];
|
||||||
if ($partnerId) {
|
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]);
|
$stmt->execute([$partnerId]);
|
||||||
$partner = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
$partner = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Liste commune (toi + partenaire si lié, sinon juste toi)
|
// 3. Commune (tous les films avec is_common = 1)
|
||||||
if ($partnerId) {
|
$stmt = $this->db->prepare("SELECT * FROM movies WHERE is_common = 1");
|
||||||
$stmt = $this->conn->prepare("SELECT * FROM movies WHERE (user_id = ? OR user_id = ?) AND is_common = 1 ORDER BY added_at DESC");
|
$stmt->execute();
|
||||||
$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);
|
$common = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
|
|
||||||
return [
|
return ["perso" => $perso, "partner" => $partner, "common" => $common];
|
||||||
"success" => true,
|
|
||||||
"lists" => [
|
|
||||||
"perso" => $perso,
|
|
||||||
"partner" => $partner,
|
|
||||||
"common" => $common
|
|
||||||
]
|
|
||||||
];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user