diff --git a/learn-sequelize.js b/learn-sequelize.js index d4b688f..34e5d1f 100644 --- a/learn-sequelize.js +++ b/learn-sequelize.js @@ -1,4 +1,4 @@ -const { Genre, Movie, Actor } = require("./models"); +const { Genre, Movie, Actor } = require('./models'); /* Write a function that creates a new Genre in the database @@ -6,8 +6,10 @@ const { Genre, Movie, Actor } = require("./models"); - add one more Genre of your choice - duplicate entries are not allowed (try it to learn about errors) */ -function insertNewGenre() { + +async function insertNewGenre() { // Add code here + const newGenre = await Genre.create({ name: 'Horror' }); } /* @@ -16,36 +18,52 @@ function insertNewGenre() { - add one more Movie of your choice. - the movie CANNOT be from year 2008 (try it to learn about errors) */ -function insertNewMovie() { +async function insertNewMovie() { // Add code here + const newMovieData = await Movie.create({ title: 'Planet of the Apes', year: 2001 }); } /* Write a function that returns the title of the movie with ID=2 */ -function getMovieWithId2() { +async function getMovieWithId2() { // Add code here + + const moveWithId2 = await Movie.findByPk(2); + return moveWithId2.title; } /* Write a function that returns an array of all the actor names */ -function getAllActors() { +async function getAllActors() { // Add code here + const actors = await Actor.findAll(); + + const allActorNames = actors.map((actors) => actors.name); + return allActorNames; } /* Write a function that returns an array of all the movie titles from 2008 */ -function getAllMoviesFrom2008() { +async function getAllMoviesFrom2008() { // Add code here + const moviesFrom2008 = await Movie.findAll({ where: { year: 2008 } }); + + const movieTitles = moviesFrom2008.map((movies) => movies.title); + + return movieTitles; } /* Write a function that deletes the genre you added in the first function: insertNewGenre() */ -function deleteGenreYouAdded() { +async function deleteGenreYouAdded() { // Add code here + const genreToBeDestroyed = await Genre.findOne({ where: { name: 'Horror' } }); + + await genreToBeDestroyed.destroy(); } /* @@ -54,8 +72,12 @@ function deleteGenreYouAdded() { - the actor and movie record already exist in the database - add the association record to the database */ -function associateRosarioToEagleEye() { +async function associateRosarioToEagleEye() { // Add code here + const actor = await Actor.findOne({ where: { name: 'Rosario Dawson' } }); + const movie = await Movie.findOne({ where: { title: 'Eagle Eye' } }); + + actor.addMovie(movie); } /* @@ -66,6 +88,10 @@ function associateRosarioToEagleEye() { */ async function associateRobertToTropicThunder() { // Add code here + const actor = await Actor.findOne({ where: { name: 'Robert Downey Jr.' } }); + const movie = await Movie.findOne({ where: { title: 'Tropic Thunder' } }); + + await actor.addMovie(movie); } module.exports = {