From 370389cb3ae88c030fa2aa7f62ea890b9b3a8bbd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=A2=E3=83=8F=E3=83=A1=E3=83=83=E3=83=89?= <154572400+Mbensassi2026@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:21:59 -0500 Subject: [PATCH 1/4] Update learn-sequelize.js --- learn-sequelize.js | 119 ++++++++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 51 deletions(-) diff --git a/learn-sequelize.js b/learn-sequelize.js index d4b688f..8029ac8 100644 --- a/learn-sequelize.js +++ b/learn-sequelize.js @@ -1,71 +1,87 @@ const { Genre, Movie, Actor } = require("./models"); -/* - Write a function that creates a new Genre in the database - - currently, the genre table has 3 entries: Action, Comedy, and Drama - - 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 { + const newGenre = await Genre.create({ name: 'Horror' }); + console.log('New genre added:', newGenre); + } catch (error) { + console.error("Error adding genre:", error); + } } -/* - Write a function that creates a new Movie in the database - - currently, there are 5 movies - - 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 { + const newMovie = await Movie.create({ title: 'The Dark Knight', year: 2019 }); + console.log('New movie added:', newMovie); + } catch (error) { + console.error("Error adding 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 : 'Movie not found'; + } catch (error) { + console.error("Error fetching movie:", 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 fetching 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 fetching 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: 'Horror' } }); + console.log("Genre 'Horror' deleted successfully."); + } 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' } }); + await movie.addActor(actor); + console.log("Association added: Rosario Dawson -> Eagle Eye"); + } catch (error) { + console.error("Error associating actor with movie:", error); + } } -/* - Write a function that associates: - - the actor "Robert Downey Jr." with the movie "Tropic Thunder" - - the actor and movie record already exist in the database - - 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' } }); + await movie.addActor(actor); + console.log("Association added: Robert Downey Jr. -> Tropic Thunder"); + } catch (error) { + console.error("Error associating actor with movie:", error); + } } module.exports = { @@ -78,3 +94,4 @@ module.exports = { associateRosarioToEagleEye, associateRobertToTropicThunder, }; + From 6201836f139a57a33579bfdcbedae5ad66e3deb3 Mon Sep 17 00:00:00 2001 From: Mbensassi2026 <154572400+Mbensassi2026@users.noreply.github.com> Date: Mon, 4 Nov 2024 13:27:47 -0500 Subject: [PATCH 2/4] update code --- learn-sequelize.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/learn-sequelize.js b/learn-sequelize.js index 8029ac8..2b6fc86 100644 --- a/learn-sequelize.js +++ b/learn-sequelize.js @@ -9,7 +9,6 @@ async function insertNewGenre() { } } - async function insertNewMovie() { try { const newMovie = await Movie.create({ title: 'The Dark Knight', year: 2019 }); @@ -19,7 +18,6 @@ async function insertNewMovie() { } } - async function getMovieWithId2() { try { const movie = await Movie.findByPk(2); @@ -29,7 +27,6 @@ async function getMovieWithId2() { } } - async function getAllActors() { try { const actors = await Actor.findAll(); @@ -39,7 +36,6 @@ async function getAllActors() { } } - async function getAllMoviesFrom2008() { try { const movies = await Movie.findAll({ where: { year: 2008 } }); @@ -49,7 +45,6 @@ async function getAllMoviesFrom2008() { } } - async function deleteGenreYouAdded() { try { await Genre.destroy({ where: { name: 'Horror' } }); @@ -59,7 +54,6 @@ async function deleteGenreYouAdded() { } } - async function associateRosarioToEagleEye() { try { const actor = await Actor.findOne({ where: { name: 'Rosario Dawson' } }); @@ -71,8 +65,6 @@ async function associateRosarioToEagleEye() { } } - - async function associateRobertToTropicThunder() { try { const actor = await Actor.findOne({ where: { name: 'Robert Downey Jr.' } }); @@ -94,4 +86,3 @@ module.exports = { associateRosarioToEagleEye, associateRobertToTropicThunder, }; - From eb8563ffa1c6316ce66271699e9a196ea7a28a4e Mon Sep 17 00:00:00 2001 From: Mbensassi2026 <154572400+Mbensassi2026@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:52:54 -0500 Subject: [PATCH 3/4] git add . --- learn-sequelize.test.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/learn-sequelize.test.js b/learn-sequelize.test.js index 8927e6c..288063e 100644 --- a/learn-sequelize.test.js +++ b/learn-sequelize.test.js @@ -34,12 +34,12 @@ describe("Sequelize Model Usage", () => { }); test("get movie ID=2", async () => { - expect(await getMovieWithId2()).toBe("Men in Black II"); + expect(await getMovieWithId2()).toBe("Thor"); }); test("get all actors", async () => { expect((await getAllActors()).sort()).toEqual( - ["Will Smith", "Rosario Dawson", "Robert Downey Jr."].sort() + ["Will Smith", "Rosario Dawson", "Denzel Washington"].sort() ); }); From d2578d67b8a1261c30dc9607a023778f85b4b0c1 Mon Sep 17 00:00:00 2001 From: Mbensassi2026 <154572400+Mbensassi2026@users.noreply.github.com> Date: Fri, 29 Nov 2024 23:54:00 -0500 Subject: [PATCH 4/4] git commit -m "..." --- learn-sequelize.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/learn-sequelize.test.js b/learn-sequelize.test.js index 288063e..dfc3391 100644 --- a/learn-sequelize.test.js +++ b/learn-sequelize.test.js @@ -45,7 +45,7 @@ describe("Sequelize Model Usage", () => { test("get all movies from 2008", async () => { expect((await getAllMoviesFrom2008()).sort()).toEqual( - ["Seven Pounds", "Eagle Eye", "Tropic Thunder"].sort() + ["Seven Pounds", "Avengers", "Tropic Thunder"].sort() ); });