Add "Add Movie" function
This commit is contained in:
@@ -1,12 +1,118 @@
|
||||
<?php
|
||||
session_start();
|
||||
header('Content-Type: application/json');
|
||||
|
||||
// --- CONFIGURATION BDD ---
|
||||
$host = 'localhost';
|
||||
$db = 'watchgether';
|
||||
$user = 'root';
|
||||
$pass = ''; // Vide par défaut sur Wamp
|
||||
|
||||
$options = [
|
||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||
PDO::ATTR_EMULATE_PREPARES => false,
|
||||
];
|
||||
|
||||
$apiKey = '23af653f99d2e7ac884415805e7ca84c';
|
||||
|
||||
function searchMovie($query) {
|
||||
global $apiKey;
|
||||
$url = "https://api.themoviedb.org/3/search/multi?api_key=" . $apiKey . "&language=fr-FR&query=" . urlencode($query);
|
||||
|
||||
// On récupère le JSON
|
||||
$response = file_get_contents($url);
|
||||
return json_decode($response, true);
|
||||
try {
|
||||
$pdo = new PDO("mysql:host=$host;dbname=$db;charset=utf8", $user, $pass, $options);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'error' => 'Connexion BDD échouée']);
|
||||
exit;
|
||||
}
|
||||
?>
|
||||
|
||||
// --- LECTURE DE L'INPUT ---
|
||||
$json = file_get_contents('php://input');
|
||||
$data = json_decode($json, true);
|
||||
|
||||
if (!$data || !isset($data['action'])) {
|
||||
echo json_encode(['success' => false, 'error' => 'Aucune action spécifiée']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$action = $data['action'];
|
||||
$params = $data['params'] ?? [];
|
||||
|
||||
// Simulation d'un utilisateur connecté (à remplacer par ton système de login plus tard)
|
||||
$current_user_id = $_SESSION['user_id'] ?? 1;
|
||||
|
||||
switch ($action) {
|
||||
|
||||
// --- RECHERCHE TMDB ---
|
||||
case 'searchTMDB':
|
||||
$query = urlencode($params['query']);
|
||||
$url = "https://api.themoviedb.org/3/search/multi?api_key=$apiKey&language=fr-FR&query=$query";
|
||||
|
||||
$response = file_get_contents($url);
|
||||
if ($response) {
|
||||
echo $response; // On renvoie directement le JSON de TMDB au front
|
||||
} else {
|
||||
echo json_encode(['success' => false, 'error' => 'Erreur API TMDB']);
|
||||
}
|
||||
exit;
|
||||
|
||||
// --- GESTION DES FILMS ---
|
||||
case 'addMovie':
|
||||
$stmt = $pdo->prepare("INSERT INTO movies (tmdb_id, titre, affiche_path, type, user_id, vu) VALUES (?, ?, ?, ?, ?, 0)");
|
||||
$success = $stmt->execute([
|
||||
$params['tmdb_id'],
|
||||
$params['titre'],
|
||||
$params['affiche_path'],
|
||||
$params['type'], // 'film' ou 'serie'
|
||||
$current_user_id
|
||||
]);
|
||||
echo json_encode(['success' => $success]);
|
||||
exit;
|
||||
|
||||
case 'getMyList':
|
||||
// Récupère les films ajoutés par l'utilisateur
|
||||
$stmt = $pdo->prepare("SELECT * FROM movies WHERE user_id = ? ORDER BY date_ajout DESC");
|
||||
$stmt->execute([$current_user_id]);
|
||||
echo json_encode(['success' => true, 'movies' => $stmt->fetchAll()]);
|
||||
exit;
|
||||
|
||||
case 'getCommonList':
|
||||
// LA MAGIE : On cherche les doublons de tmdb_id entre deux utilisateurs
|
||||
// On part du principe que tu es l'ID 1 et ta copine l'ID 2 (à adapter)
|
||||
$partner_id = ($current_user_id == 1) ? 2 : 1;
|
||||
|
||||
$stmt = $pdo->prepare("
|
||||
SELECT m1.* FROM movies m1
|
||||
INNER JOIN movies m2 ON m1.tmdb_id = m2.tmdb_id
|
||||
WHERE m1.user_id = ? AND m2.user_id = ?
|
||||
");
|
||||
$stmt->execute([$current_user_id, $partner_id]);
|
||||
echo json_encode(['success' => true, 'common_movies' => $stmt->fetchAll()]);
|
||||
exit;
|
||||
|
||||
// --- ACTIONS SUR LE FILM ---
|
||||
case 'toggleViewed':
|
||||
// Alterne entre vu (1) et non vu (0)
|
||||
$stmt = $pdo->prepare("UPDATE movies SET vu = !vu WHERE id = ? AND user_id = ?");
|
||||
$success = $stmt->execute([(int)$params['movie_id'], $current_user_id]);
|
||||
echo json_encode(['success' => $success]);
|
||||
exit;
|
||||
|
||||
case 'deleteMovie':
|
||||
$stmt = $pdo->prepare("DELETE FROM movies WHERE id = ? AND user_id = ?");
|
||||
$success = $stmt->execute([(int)$params['movie_id'], $current_user_id]);
|
||||
echo json_encode(['success' => $success]);
|
||||
exit;
|
||||
|
||||
// --- COMMENTAIRES ---
|
||||
case 'addComment':
|
||||
$stmt = $pdo->prepare("INSERT INTO commentaires (movie_id, user_id, contenu) VALUES (?, ?, ?)");
|
||||
$success = $stmt->execute([
|
||||
(int)$params['movie_id'],
|
||||
$current_user_id,
|
||||
$params['text']
|
||||
]);
|
||||
echo json_encode(['success' => $success]);
|
||||
exit;
|
||||
|
||||
default:
|
||||
echo json_encode(['success' => false, 'error' => 'Action inconnue']);
|
||||
exit;
|
||||
}
|
||||
+7
-32
@@ -1,4 +1,3 @@
|
||||
<?php include 'RequestHandler.php'; ?>
|
||||
<!DOCTYPE html>
|
||||
<html lang="fr">
|
||||
<head>
|
||||
@@ -9,43 +8,19 @@
|
||||
<body class="bg-slate-900 text-white p-8">
|
||||
|
||||
<div class="max-w-4xl mx-auto">
|
||||
<h1 class="text-3xl font-bold mb-8 text-blue-400 text-center">Rechercher un film ou une série</h1>
|
||||
<h1 class="text-3xl font-bold mb-8 text-blue-400 text-center">WatchGether</h1>
|
||||
|
||||
<form method="GET" class="flex gap-2 mb-10">
|
||||
<input type="text" name="q" placeholder="Titre du film..."
|
||||
<div class="flex gap-2 mb-10">
|
||||
<input type="text" id="searchInput" placeholder="Chercher un film ou une série..."
|
||||
class="flex-grow p-4 rounded-lg bg-slate-800 border border-slate-700 focus:outline-none focus:border-blue-500">
|
||||
<button type="submit" class="bg-blue-600 px-8 py-4 rounded-lg font-bold hover:bg-blue-500 transition">
|
||||
<button onclick="performSearch()" class="bg-blue-600 px-8 py-4 rounded-lg font-bold hover:bg-blue-500 transition">
|
||||
Chercher
|
||||
</button>
|
||||
</form>
|
||||
|
||||
<div class="grid grid-cols-2 md:grid-cols-4 gap-6">
|
||||
<?php
|
||||
if (isset($_GET['q']) && !empty($_GET['q'])) {
|
||||
$results = searchMovie($_GET['q']);
|
||||
|
||||
foreach ($results['results'] as $item) {
|
||||
// On ne prend que les films ou séries qui ont une affiche
|
||||
if (isset($item['poster_path'])) {
|
||||
$title = isset($item['title']) ? $item['title'] : $item['name'];
|
||||
$poster = "https://image.tmdb.org/t/p/w500" . $item['poster_path'];
|
||||
$date = isset($item['release_date']) ? substr($item['release_date'], 0, 4) : (isset($item['first_air_date']) ? substr($item['first_air_date'], 0, 4) : '');
|
||||
|
||||
echo "
|
||||
<div class='bg-slate-800 rounded-xl overflow-hidden border border-slate-700 hover:scale-105 transition-transform cursor-pointer'>
|
||||
<img src='$poster' alt='$title' class='w-full h-auto'>
|
||||
<div class='p-3'>
|
||||
<h3 class='font-bold text-sm truncate'>$title</h3>
|
||||
<p class='text-xs text-gray-400'>$date</p>
|
||||
<button class='mt-2 w-full bg-green-600 text-xs py-1 rounded hover:bg-green-500'>+ Ajouter</button>
|
||||
</div>
|
||||
</div>";
|
||||
}
|
||||
}
|
||||
}
|
||||
?>
|
||||
</div>
|
||||
|
||||
<div id="resultsGrid" class="grid grid-cols-2 md:grid-cols-4 gap-6"></div>
|
||||
</div>
|
||||
|
||||
<script src="js/index.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,62 @@
|
||||
// Fonction principale pour parler au RequestHandler
|
||||
async function apiRequest(action, params = {}) {
|
||||
const response = await fetch('RequestHandler.php', {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type:': 'application/json' },
|
||||
body: JSON.stringify({ action, params })
|
||||
});
|
||||
return await response.json();
|
||||
}
|
||||
|
||||
// Action de recherche
|
||||
async function performSearch() {
|
||||
const query = document.getElementById('searchInput').value;
|
||||
if (!query) return;
|
||||
|
||||
const grid = document.getElementById('resultsGrid');
|
||||
grid.innerHTML = '<p class="col-span-full text-center">Recherche en cours...</p>';
|
||||
|
||||
const data = await apiRequest('searchTMDB', { query: query });
|
||||
|
||||
grid.innerHTML = ''; // On vide le message de chargement
|
||||
|
||||
if (data.results) {
|
||||
data.results.forEach(item => {
|
||||
if (!item.poster_path) return;
|
||||
|
||||
const title = item.title || item.name;
|
||||
const poster = `https://image.tmdb.org/t/p/w500${item.poster_path}`;
|
||||
const type = item.media_type === 'tv' ? 'serie' : 'film';
|
||||
|
||||
const card = document.createElement('div');
|
||||
card.className = 'bg-slate-800 rounded-xl overflow-hidden border border-slate-700 hover:scale-105 transition-transform';
|
||||
card.innerHTML = `
|
||||
<img src="${poster}" class="w-full h-auto">
|
||||
<div class="p-3">
|
||||
<h3 class="font-bold text-sm truncate">${title}</h3>
|
||||
<button onclick="addMovie(${item.id}, '${title.replace(/'/g, "\\'")}', '${item.poster_path}', '${type}')"
|
||||
class="mt-2 w-full bg-green-600 text-xs py-2 rounded font-bold hover:bg-green-500">
|
||||
+ Ajouter
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
grid.appendChild(card);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// Action d'ajout
|
||||
async function addMovie(tmdbId, title, posterPath, type) {
|
||||
const result = await apiRequest('addMovie', {
|
||||
tmdb_id: tmdbId,
|
||||
titre: title,
|
||||
affiche_path: posterPath,
|
||||
type: type
|
||||
});
|
||||
|
||||
if (result.success) {
|
||||
alert('Ajouté à la liste !');
|
||||
} else {
|
||||
alert('Erreur lors de l\'ajout.');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user