Skip to content
Open
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: 28 additions & 6 deletions learn-sequelize.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: "Thriller" })
return newGenre
}

/*
Expand All @@ -18,34 +20,44 @@ function insertNewGenre() {
*/
function insertNewMovie() {
// Add code here
const newMovie = Movie.create({ name: "Interstellar", year: 2014 })
return newMovie
}

/*
Write a function that returns the title of the movie with ID=2
*/
function getMovieWithId2() {
async function getMovieWithId2() {
// Add code here
const getMovie = await Movie.findByPk(2)
return getMovie.title
}

/*
Write a function that returns an array of all the actor names
*/
function getAllActors() {
async function getAllActors() {
// Add code here
const allActors = await Actor.findAll()
return allActors.map(actor => actor.name)
}

/*
Write a function that returns an array of all the movie titles from 2008
*/
function getAllMoviesFrom2008() {
async function getAllMoviesFrom2008() {
// Add code here
const movies = await Movie.findAll({ where: { year: "2008" } })
return movies.map(movie => movie.title)
}

/*
Write a function that deletes the genre you added in the first function: insertNewGenre()
*/
function deleteGenreYouAdded() {
async function deleteGenreYouAdded() {
// Add code here
const genre = await Genre.destroy ({ where: { name: "Thriller" } })
return genre.name
}

/*
Expand All @@ -54,8 +66,13 @@ 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" } })
if (actor && movie) {
return await actor.addMovie(movie)
}
}

/*
Expand All @@ -66,6 +83,11 @@ 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" } })
if (actor && movie) {
return await movie.addActor(actor)
}
}

module.exports = {
Expand Down