From 201c202b0625978409e6824e8501bc83208623b6 Mon Sep 17 00:00:00 2001 From: kjara03 Date: Sun, 27 Oct 2024 17:08:53 -0400 Subject: [PATCH] Implement functions in learn-sequelize.js --- learn-sequelize.js | 76 +++++++++++++++++++++++++++++++++++----------- 1 file changed, 59 insertions(+), 17 deletions(-) diff --git a/learn-sequelize.js b/learn-sequelize.js index d4b688f..bccac44 100644 --- a/learn-sequelize.js +++ b/learn-sequelize.js @@ -6,8 +6,12 @@ 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() { - // Add code here +async function insertNewGenre() { + try { + await Genre.create({ name: "Sci-Fi" }); + } catch (error) { + console.error("Error inserting new genre:", error); + } } /* @@ -16,46 +20,76 @@ function insertNewGenre() { - add one more Movie of your choice. - the movie CANNOT be from year 2008 (try it to learn about errors) */ -function insertNewMovie() { - // Add code here +async function insertNewMovie() { + try { + await Movie.create({ title: "Inception", year: 2010 }); + } catch (error) { + console.error("Error inserting new movie:", error); + } } /* Write a function that returns the title of the movie with ID=2 */ -function getMovieWithId2() { - // Add code here +async function getMovieWithId2() { + try { + const movie = await Movie.findByPk(2); + return movie ? movie.title : null; + } catch (error) { + console.error("Error getting movie with ID 2:", error); + } } /* Write a function that returns an array of all the actor names */ -function getAllActors() { - // Add code here +async function getAllActors() { + try { + const actors = await Actor.findAll(); + return actors.map((actor) => actor.name); + } catch (error) { + console.error("Error getting all actors:", error); + } } + /* Write a function that returns an array of all the movie titles from 2008 */ -function getAllMoviesFrom2008() { - // Add code here +async function getAllMoviesFrom2008() { + try { + const movies = await Movie.findAll({ where: { year: 2008 } }); + return movies.map((movie) => movie.title); + } catch (error) { + console.error("Error getting movies from 2008:", error); + } } - /* Write a function that deletes the genre you added in the first function: insertNewGenre() */ -function deleteGenreYouAdded() { - // Add code here +async function deleteGenreYouAdded() { + try { + await Genre.destroy({ where: { name: "Sci-Fi" } }); + } catch (error) { + console.error("Error deleting genre:", error); + } } - /* Write a function that associates: - the actor "Rosario Dawson" with the movie "Eagle Eye" - the actor and movie record already exist in the database - add the association record to the database */ -function associateRosarioToEagleEye() { - // Add code here +async function associateRosarioToEagleEye() { + try { + const actor = await Actor.findOne({ where: { name: "Rosario Dawson" } }); + const movie = await Movie.findOne({ where: { title: "Eagle Eye" } }); + if (actor && movie) { + await movie.addActor(actor); + } + } catch (error) { + console.error("Error associating Rosario to Eagle Eye:", error); + } } /* @@ -65,7 +99,15 @@ function associateRosarioToEagleEye() { - add the association record to the database */ async function associateRobertToTropicThunder() { - // Add code here + try { + const actor = await Actor.findOne({ where: { name: "Robert Downey Jr." } }); + const movie = await Movie.findOne({ where: { title: "Tropic Thunder" } }); + if (actor && movie) { + await movie.addActor(actor); + } + } catch (error) { + console.error("Error associating Robert to Tropic Thunder:", error); + } } module.exports = {