Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Movie-Filter Api Done #29

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
DATABASE_URL="mongodb+srv://Admin:[email protected].mongodb.net/sample_mflix?retryWrites=true&w=majority&appName=LearningCluster"
DATABASE_URL="mongodb+srv://testuser:[email protected].mongodb.net/sample_mflix?retryWrites=true&w=majority&appName=moviewebsite"
JWT_SECRET="ac6043c6817f9de65a2dd031de14f6ee7a89b1f8c18631f6aba66664a075430b"
135 changes: 135 additions & 0 deletions controllers/movie_filter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// import asynchHandler(perhaps i didn't used it)
const asyncHandler = require("express-async-handler");

// import prisma to connect with mongo-db
const prisma = require("../prisma/index");



// global variables to trace the total number of movies and pages to be there
var total_number_of_movies = 0;//by def 0
var total_number_of_pages = 0;//by def 0


// let's write a function which returns us the total number of the movies found

const total_No_Of_Movies = async (lang, genre) => {

// define the prisma query
const moviesQuery = {
// add genre if genre is not undefined , and same for language(lang)
where: {
...(genre && {
genres: {
hasSome: [genre],
}
}),
...(lang && {
languages: {
hasSome: [lang]
}
})
},
};

// search in the database
const movies = await prisma.embedded_movies.findMany(moviesQuery);

// finally return the total number of the movies
console.log("The total_global count of such movies is ", movies.length);
return movies.length;
}

// let's create the getAllMovies function for the router

const getAllMovies = async (req, res) => {
try {
// Extract query parameters from the request
const { page, lang, genre } = req.query;

// declare the page_size_variable
const page_size = 10;


// if page is 1 , call the function and fill the global values such as total_number of movies and pages
if (page == 1) {

// It is the first time, so call it and wait it until its finishes its execution
total_number_of_movies = await total_No_Of_Movies(lang, genre);

// get the toal number of pages
let flag = Math.floor(total_number_of_movies / page_size);

if (flag * page_size == total_number_of_movies) {
total_number_of_pages = flag;
}
else {
total_number_of_pages = flag + 1;
}
}

// console.log("____________\n");
// console.log(`Page Number : ${page}`);
// console.log(`Language : ${lang}`);
// console.log(`Genre : ${genre}`);

// console.log("____________\n");

const skip_val = (page - 1) * page_size;//get the skip_val


// get the take_val and incorporate the case if it is last page or something greater than total number of pages
const take_val = (page <= total_number_of_pages) ? (page == total_number_of_pages ? (total_number_of_movies - (page_size * (total_number_of_pages - 1))) : 10) : 0;

// define the prisma query
const moviesQuery = {
skip: skip_val,//set the skip value
take: take_val,//set the take value

// if genre is not undefined add genre to where,same for lagnuage (lang)
where: {
...(genre && {
genres: {
hasSome: [genre]
}
}),
...(lang && {
languages: {
hasSome: [lang]
}
})
},
};


// Search and filter the database according to the movisQuery
const movies = await prisma.embedded_movies.findMany(moviesQuery);

// console.log("***************\n");
// console.log(movies);
// console.log("***************\n");


// console.log(`Total number of such movies : ${totalMoviesCount}`);

// Response object
const response = {
movies: movies,
currentPage: page,
totalPages: take_val,
totalMovies: total_number_of_movies
};

// Send the response
res.status(200).json(response);
}
catch (error) {
// Handle errors
console.error("Error:", error);
res.status(500).json({ message: "Internal server error" });
}
};


// Enable the export feature to get it imported in the router
module.exports = { getAllMovies }
9 changes: 9 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const morgan = require("morgan");
const app = express();

const otpRoutes = require("./routes/otpRoutes");
const movieRoutes=require("./routes/movie_filter");

require("dotenv").config();

Expand All @@ -15,6 +16,14 @@ app.use(morgan("tiny"));

app.use("/otp", otpRoutes);

// indicate to use the route
app.use("/movie/filter",movieRoutes);
// just defining an new .get api

app.get("/",(req,res)=>{
res.send("Hello, how are you?");
})

app.listen(PORT, () => {
console.log(`App listening on port ${PORT}`);
});
Expand Down
22 changes: 14 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"@prisma/client": "^5.11.0",
"bcryptjs": "^2.4.3",
"dotenv": "^16.4.5",
"express": "^4.18.3",
"express": "^4.19.1",
"express-async-handler": "^1.2.0",
"express-validator": "^7.0.1",
"mongoose": "^8.2.2",
"morgan": "^1.10.0",
Expand Down
Loading