diff --git a/backend/db/dataInitializer.js b/backend/db/dataInitializer.js new file mode 100644 index 0000000..de1ba4a --- /dev/null +++ b/backend/db/dataInitializer.js @@ -0,0 +1,63 @@ +const Genre = require("../models/Genre"); + + +async function initializeData() { + initializeGenres(); +} + +async function initializeGenres() { + try { + // * data to insert: + const initialGenres = [ + { id: 10759, name: 'Action & Adventure' }, + { id: 16, name: 'Animation' }, + { id: 35, name: 'Comedy' }, + { id: 80, name: 'Crime' }, + { id: 99, name: 'Documentary' }, + { id: 18, name: 'Drama' }, + { id: 10751, name: 'Family' }, + { id: 10762, name: 'Kids' }, + { id: 9648, name: 'Mystery' }, + { id: 10763, name: 'News' }, + { id: 10764, name: 'Reality' }, + { id: 10765, name: 'Sci-Fi & Fantasy' }, + { id: 10766, name: 'Soap' }, + { id: 10767, name: 'Talk' }, + { id: 10768, name: 'War & Politics' }, + { id: 37, name: 'Western' }, + { id: 28, name: 'Action' }, + { id: 12, name: 'Adventure' }, + { id: 14, name: 'Fantasy' }, + { id: 36, name: 'History' }, + { id: 27, name: 'Horror' }, + { id: 10402, name: 'Music' }, + { id: 10749, name: 'Romance' }, + { id: 878, name: 'Science Fiction' }, + { id: 10770, name: 'TV Movie' }, + { id: 53, name: 'Thriller' }, + { id: 10752, name: 'War' } + ]; + + // * Check if the data already exists + const existingGenres = await Genre.findAll(); + + // ? If data already exists, return + if (existingGenres.length == initialGenres.length) { + console.log('Genres already initialized'); + return; + } + + // ? If data doesn't exist, insert initial data + await Genre.bulkCreate(initialGenres); + + console.log(`${initialGenres.length} Genres initialized successfully`); + } catch (error) { + console.error('Error initializing data:', error); + } +} + +module.exports = { + initializeData, + + initializeGenres, +} \ No newline at end of file diff --git a/backend/models/Entry.js b/backend/models/Entry.js index 558be47..55ed19f 100644 --- a/backend/models/Entry.js +++ b/backend/models/Entry.js @@ -5,7 +5,7 @@ const Entry = sequelize.define("entry", { id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, - title: { type: DataTypes.STRING(50) }, + title: { type: DataTypes.STRING(100) }, original_title: { type: DataTypes.STRING(50) }, release_date: { type: DataTypes.DATE }, runtime: { type: DataTypes.INTEGER }, diff --git a/backend/models/Episode.js b/backend/models/Episode.js index 2510cae..f0536b4 100644 --- a/backend/models/Episode.js +++ b/backend/models/Episode.js @@ -17,7 +17,7 @@ const Episode = sequelize.define("episode", } }, - name: { type: DataTypes.STRING(30) }, + name: { type: DataTypes.STRING(100) }, overview: { type: DataTypes.STRING(1000) }, episode_number: { type: DataTypes.INTEGER }, episode_type: { type: DataTypes.STRING(60) }, diff --git a/backend/server.js b/backend/server.js index a5dd45a..9e1c3c2 100644 --- a/backend/server.js +++ b/backend/server.js @@ -29,13 +29,16 @@ app.use((req, res, next) => { app.use('/api/v1', routes); // Start the server -const relations = require('./db/relations.js') +const relations = require('./db/relations.js'); +const { initializeGenres, initializeData } = require('./db/dataInitializer.js'); sequelize // .sync({force: true}) // will drop all tables and recreate them. // .sync({ alter: true }) // will attempt to automatically alter tables to fit the model definitions. .sync() .then((result) => { + initializeData(); + app.listen(PORT, () => { //const Entry = require("./models/Entry.js") console.log(`Backend server running on http://localhost:${PORT}`); diff --git a/frontend/src/app/features/entry/entry-page/entry-page.component.html b/frontend/src/app/features/entry/entry-page/entry-page.component.html index 4349c5d..c1ebe97 100644 --- a/frontend/src/app/features/entry/entry-page/entry-page.component.html +++ b/frontend/src/app/features/entry/entry-page/entry-page.component.html @@ -40,7 +40,8 @@