diff --git a/server/api/degreesOfSeparation.js b/server/api/degreesOfSeparation.js index 9450f4c..dd8747b 100644 --- a/server/api/degreesOfSeparation.js +++ b/server/api/degreesOfSeparation.js @@ -1,41 +1,31 @@ const express = require("express"); const app = express.Router(); const { Casts, Movie, castsMovieLink } = require("../db"); -const bfs = require("../utils/DegreesOfSeparation"); // Update import statement +const { Op } = require("sequelize"); const buildGraph = require("../utils/graphBuilder"); -const getCommonMovie = require ("./getCommonMovie") +const bfs = require("../utils/DegreesOfSeparation"); +const getCommonMovie = require("./getCommonMovie"); -// GET for degrees of separation between two actors app.get("/:castsId/:casts2Id", async (req, res, next) => { try { const { castsId, casts2Id } = req.params; - // Fetch the casts (actors) by name - const casts1 = await Casts.findOne({ where: { name: castsId } }); - const casts2 = await Casts.findOne({ where: { name: casts2Id } }); - - if (!casts1 || !casts2) { - return res.status(404).json({ error: "Actor Not Found" }); - } - const graph = await buildGraph(); - console.log(casts1.id, casts2.id); + console.log(castsId, casts2Id); - // Using the bfs function to find the path between the two actors - let path = bfs(graph, casts1.id, casts2.id); + let path = bfs(graph, castsId, casts2Id); + if (path === null) { + return res.json({ degreesOfSeparation: null, path: [], moviesPath: [] }); + } - // Calculate degrees of separation - let degreesOfSeparation = path ? path.length - 1 : null; + let degreesOfSeparation = path.length - 1; let moviesPath = []; - for (let i = 0; i < path.length; i++){ - if (path[i+1]) { - const commonMovies = await getCommonMovie(path[i], path[i+1]); - moviesPath.push(commonMovies); - } + for (let i = 0; i < path.length - 1; i++) { + const commonMovies = await getCommonMovie(path[i], path[i + 1]); + moviesPath.push(commonMovies); } - // Sending the result as a JSON response res.json({ degreesOfSeparation, path, moviesPath }); } catch (error) { next(error); diff --git a/server/api/getCommonMovie.js b/server/api/getCommonMovie.js index 400717e..7a58fd6 100644 --- a/server/api/getCommonMovie.js +++ b/server/api/getCommonMovie.js @@ -4,59 +4,36 @@ const { Casts, Movie, castsMovieLink } = require("../db"); const { Op } = require("sequelize"); async function getCommonMovie(actor1, actor2) { - try { - // Find movies involving actor1 - // const commonMovies = await Movie.findAll({ - // include: [ - // { - // model: Casts, - // where: { - // [Op.or]: [ - // {id: [actor1]}, - // {id: [actor2]} - // ] - // }, - // }, - // ], - // }); - - const movies1 = await Movie.findAll({ - include: [ - { - model: Casts, - where: { - id: [actor1] - }, + try { + const movies1 = await Movie.findAll({ + include: [ + { + model: Casts, + where: { + id: actor1, }, - ], - }); - const movies2 = await Movie.findAll({ - include: [ - { - model: Casts, - where: { - id: [actor2] - }, + }, + ], + }); + const movies2 = await Movie.findAll({ + include: [ + { + model: Casts, + where: { + id: actor2, }, - ], - }); - const filterFunc = (movie, moviesArray) => { - for (let i = 0; i < moviesArray.length; i++) { - if (moviesArray[i].id===movie.id) { - return true; - } - } - return false; - } - const commonMovies = movies1.filter(movie => filterFunc(movie, movies2)) - // Find movies involving actor2 and also appeared in actor1's movies - - //console.log(commonMovies) - return commonMovies; - } catch (error) { - console.error('Error retrieving common movies:', error); - throw error; - } + }, + ], + }); + const commonMovies = movies1.filter((movie) => + movies2.some((m) => m.id === movie.id) + ); + + return commonMovies; + } catch (error) { + console.error("Error retrieving common movies:", error); + throw error; } - -module.exports= getCommonMovie; +} + +module.exports = getCommonMovie; diff --git a/src/Components/Home.js b/src/Components/Home.js index c806fde..cc08375 100644 --- a/src/Components/Home.js +++ b/src/Components/Home.js @@ -16,7 +16,7 @@ const Home = () => { const [casts2Id, setCasts2Id] = useState(""); const [degreesOfSeparation, setDegreesOfSeparation] = useState(null); const [path, setPath] = useState([]); - const [moviesPath, setMoviesPath] = useState(null); + const [moviesPath, setMoviesPath] = useState([]); useEffect(() => { for (let i = 0; i < path.length; i++) { @@ -34,6 +34,7 @@ const Home = () => { try { setLoading(true); const response = await fetchDegreesOfSeparation(casts1Id, casts2Id); + console.log(response); setDegreesOfSeparation(response.degreesOfSeparation); setPath(response.path); setMoviesPath(response.moviesPath); @@ -50,7 +51,7 @@ const Home = () => {