Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
CaCaoCritics
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Bilel El Yaagoubi
CaCaoCritics
Commits
6b6b870e
Commit
6b6b870e
authored
Jun 9, 2022
by
Bilel El Yaagoubi
Browse files
Options
Downloads
Patches
Plain Diff
define genre and populate the db
parent
203be8f8
No related branches found
No related tags found
1 merge request
!13
Define genre and populate the db with them
Pipeline
#42560
passed
Jun 9, 2022
Stage: lint
Stage: build
Changes
2
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
backend/models/genre.js
+20
-0
20 additions, 0 deletions
backend/models/genre.js
backend/populate.js
+52
-0
52 additions, 0 deletions
backend/populate.js
with
72 additions
and
0 deletions
backend/models/genre.js
0 → 100644
+
20
−
0
View file @
6b6b870e
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
;
This diff is collapsed.
Click to expand it.
backend/populate.js
+
52
−
0
View file @
6b6b870e
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
();
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment