Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 12 additions & 22 deletions server/api/degreesOfSeparation.js
Original file line number Diff line number Diff line change
@@ -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);
Expand Down
83 changes: 30 additions & 53 deletions server/api/getCommonMovie.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
24 changes: 22 additions & 2 deletions src/Components/Home.js
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand All @@ -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);
Expand All @@ -50,7 +51,7 @@ const Home = () => {
<div className="flex">
<Star />
<div className="ml-3 mr-3 mb-4 text-3xl">
Welcome {auth.username} to Reel Relations!!{" "}
Welcome {auth.username} to Reel Relations!!
</div>
<Star />
</div>
Expand Down Expand Up @@ -93,6 +94,25 @@ const Home = () => {
{degreesOfSeparation !== null && (
<div>Degrees of Separation: {degreesOfSeparation}</div>
)}

{/* Display the actors and movies */}
{path.length > 0 && (
<div>
{path.map((actor, index) => (
<div key={index}>
{actor.name}
{moviesPath[index].length > 0 && (
<ul>
{moviesPath[index].map((movie) => (
<li key={movie.id}>{movie.title}</li>
))}
</ul>
)}
</div>
))}
</div>
)}

{loading && <Spinner />}
</div>
);
Expand Down