Skip to content
Snippets Groups Projects
Commit 25f32f51 authored by Bilel El Yaagoubi's avatar Bilel El Yaagoubi
Browse files

Merge branch 'fetch_genres' into 'master'

Define genre and populate the db with them

See merge request !13
parents ca07bd62 6b6b870e
Branches tp1-correction
No related tags found
1 merge request!13Define genre and populate the db with them
Pipeline #42576 passed
const mongoose = require("mongoose");
const GenreSchema = new mongoose.Schema(
{
name: { type: String },
id: { type: Number, required: true, unique: true },
},
{
toJSON: { virtuals: true }, // So `res.json()` and other `JSON.stringify()` functions include virtuals
toObject: { virtuals: true }, // So `console.log()` and other functions that use `toObject()` include virtuals
}
);
const GenreModel = mongoose.model(
"GenreModel",
GenreSchema,
"genres_populated"
);
module.exports = GenreModel;
const mongoose = require("mongoose"); const mongoose = require("mongoose");
const MovieModel = require("./models/movie"); const MovieModel = require("./models/movie");
const GenreModel = require("./models/genre");
const axios = require("axios"); const axios = require("axios");
async function fetchMoviesFromTheMovieDatabase(n) { async function fetchMoviesFromTheMovieDatabase(n) {
...@@ -58,10 +59,56 @@ async function populateMovies(movies) { ...@@ -58,10 +59,56 @@ async function populateMovies(movies) {
} }
} }
async function fetchGenresFromTheMovieDatabase(type) {
// TODO: fetch genres from the The Movie Database API
try {
// Do something if call succeeded
const genreFetch = await axios.get(
` https://api.themoviedb.org/3/genre/` +
type +
`/list?api_key=522d421671cf75c2cba341597d86403a&language=en-US`
);
console.log(genreFetch.data.genres);
return genreFetch.data.genres;
} catch (error) {
// Do something if call failed
console.error(error);
}
}
async function populateGenres(genres) {
// TODO: populate genres into the database
try {
for (const genre of genres) {
const newGenre = await new GenreModel({
// Genre attributes
name: genre.name,
id: genre.id,
});
// Create a new genre instance
const is_present = await GenreModel.find({ id: genre.id });
console.log(is_present);
if (!is_present.length) {
const createdGenre = await newGenre.save();
// What to do after genre has been saved !
console.log("Genre Saved !");
console.log(createdGenre.name);
} else {
console.log("Genre already exists within de db");
}
}
} catch (error) {
console.log(error);
}
}
async function dropDataBase() { async function dropDataBase() {
// TODO: Drop the collections // TODO: Drop the collections
try { try {
await MovieModel.deleteMany({}); await MovieModel.deleteMany({});
await GenreModel.deleteMany({});
} catch (error) { } catch (error) {
console.log(error); console.log(error);
} }
...@@ -77,6 +124,11 @@ async function populate(N) { ...@@ -77,6 +124,11 @@ async function populate(N) {
await populateMovies(movies); await populateMovies(movies);
console.log(n); console.log(n);
} }
const tvGenres = await fetchGenresFromTheMovieDatabase("tv");
await populateGenres(tvGenres);
const moviesGenres = await fetchGenresFromTheMovieDatabase("movie");
await populateGenres(moviesGenres);
// disconnect mongoose client // disconnect mongoose client
await client.disconnect(); await client.disconnect();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment