changed folder structure and added readme file
This commit is contained in:
1 parent
cbdffb21e3
commit
3bb0935db9
50 files changed
+6600
-3999
No files matched your search
@@ -0,0 +1,86 @@
|
||||
const { Sequelize } = require("sequelize");
|
||||
const Collection = require("../models/Collection");
|
||||
const Genre = require("../models/Genre");
|
||||
const Media = require("../models/Media");
|
||||
|
||||
|
||||
async function initializeData() {
|
||||
initializeGenres();
|
||||
|
||||
|
||||
// const collections = await Collection.findAll({
|
||||
// where: {
|
||||
// posterPath: { [Sequelize.Op.notLike]: '/%' }
|
||||
// }
|
||||
// });
|
||||
|
||||
// collections.forEach(collection => {
|
||||
// Media.create({
|
||||
// mediaable: 'collection',
|
||||
// mediaableId: collection.id,
|
||||
// type: 'poster',
|
||||
// path: collection.posterPath,
|
||||
// isLocal: true,
|
||||
// isSelected: true
|
||||
// })
|
||||
// });
|
||||
|
||||
// console.log(collections);
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
const { Sequelize } = require('sequelize');
|
||||
const fs = require('fs');
|
||||
|
||||
const logStream = fs.createWriteStream('sequelize.log', { flags: 'a' });
|
||||
|
||||
const sequelize = new Sequelize('Showzy', 'root', 'password', {
|
||||
host: 'localhost',
|
||||
dialect: 'mysql',
|
||||
logging: (msg) => {
|
||||
logStream.write(msg + '\n');
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = sequelize;
|
||||
@@ -0,0 +1,28 @@
|
||||
-- 27 genres
|
||||
INSERT INTO genres(id,name) VALUES (10759,'Action & Adventure');
|
||||
INSERT INTO genres(id,name) VALUES (16,'Animation');
|
||||
INSERT INTO genres(id,name) VALUES (35,'Comedy');
|
||||
INSERT INTO genres(id,name) VALUES (80,'Crime');
|
||||
INSERT INTO genres(id,name) VALUES (99,'Documentary');
|
||||
INSERT INTO genres(id,name) VALUES (18,'Drama');
|
||||
INSERT INTO genres(id,name) VALUES (10751,'Family');
|
||||
INSERT INTO genres(id,name) VALUES (10762,'Kids');
|
||||
INSERT INTO genres(id,name) VALUES (9648,'Mystery');
|
||||
INSERT INTO genres(id,name) VALUES (10763,'News');
|
||||
INSERT INTO genres(id,name) VALUES (10764,'Reality');
|
||||
INSERT INTO genres(id,name) VALUES (10765,'Sci-Fi & Fantasy');
|
||||
INSERT INTO genres(id,name) VALUES (10766,'Soap');
|
||||
INSERT INTO genres(id,name) VALUES (10767,'Talk');
|
||||
INSERT INTO genres(id,name) VALUES (10768,'War & Politics');
|
||||
INSERT INTO genres(id,name) VALUES (37,'Western');
|
||||
INSERT INTO genres(id,name) VALUES (28,'Action');
|
||||
INSERT INTO genres(id,name) VALUES (12,'Adventure');
|
||||
INSERT INTO genres(id,name) VALUES (14,'Fantasy');
|
||||
INSERT INTO genres(id,name) VALUES (36,'History');
|
||||
INSERT INTO genres(id,name) VALUES (27,'Horror');
|
||||
INSERT INTO genres(id,name) VALUES (10402,'Music');
|
||||
INSERT INTO genres(id,name) VALUES (10749,'Romance');
|
||||
INSERT INTO genres(id,name) VALUES (878,'Science Fiction');
|
||||
INSERT INTO genres(id,name) VALUES (10770,'TV Movie');
|
||||
INSERT INTO genres(id,name) VALUES (53,'Thriller');
|
||||
INSERT INTO genres(id,name) VALUES (10752,'War');
|
||||
@@ -0,0 +1,142 @@
|
||||
// Import models
|
||||
const Franchise = require('../models/Franchise');
|
||||
const Entry = require('../models/Entry')
|
||||
const EntryId = require('../models/EntryId')
|
||||
const EntryRating = require('../models/EntryRating')
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const Collection = require('../models/Collection');
|
||||
const UserCollectionEntry = require('../models/UserCollectionEntry');
|
||||
const Season = require('../models/Season');
|
||||
const Episode = require('../models/Episode');
|
||||
const EpisodeSeen = require('../models/EpisodeSeen');
|
||||
const Genre = require('../models/Genre');
|
||||
const EntryGenre = require('../models/EntryGenre');
|
||||
const Media = require('../models/Media');
|
||||
|
||||
|
||||
// ? one to one
|
||||
// A hasOne B = A > B
|
||||
// A belongsTo B = A < B
|
||||
// ? one to many
|
||||
// A hasMany B = A > B
|
||||
// A belongsTo B = A < B
|
||||
|
||||
|
||||
// * ENTRY & EntryId - ONE TO ONE
|
||||
Entry.hasOne(EntryId, {
|
||||
foreignKey: 'entry_id', as: 'entryId', onDelete: 'CASCADE',
|
||||
});
|
||||
EntryId.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
|
||||
// * ENTRY & EntryRating - ONE TO ONE
|
||||
Entry.hasOne(EntryRating, {
|
||||
foreignKey: 'entry_id', as: 'entryRating', onDelete: 'CASCADE',
|
||||
});
|
||||
EntryRating.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
|
||||
// * ENTRY & ENTRY_SEEN - ONE TO MANY
|
||||
Entry.hasMany(EntrySeen, {
|
||||
foreignKey: 'entry_id', as: 'entrySeen', onDelete: 'CASCADE',
|
||||
});
|
||||
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
|
||||
// * ENTRY & LIST - MANY TO MANY
|
||||
Entry.belongsToMany(Collection, {
|
||||
through: UserCollectionEntry,
|
||||
foreignKey: 'entry_id',
|
||||
otherKey: 'user_collection_id',
|
||||
onDelete: 'CASCADE',
|
||||
as: 'collections',
|
||||
});
|
||||
|
||||
Collection.belongsToMany(Entry, {
|
||||
through: UserCollectionEntry,
|
||||
foreignKey: 'user_collection_id',
|
||||
otherKey: 'entry_id',
|
||||
onDelete: 'CASCADE',
|
||||
as: 'entries',
|
||||
});
|
||||
|
||||
Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries' });
|
||||
UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
Collection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
|
||||
UserCollectionEntry.belongsTo(Collection, { foreignKey: 'user_collection_id', as: 'userCollection' });
|
||||
|
||||
// * ENTRY & GENRE - MANY TO MANY
|
||||
Entry.belongsToMany(Genre, {
|
||||
through: EntryGenre,
|
||||
foreignKey: 'entry_id',
|
||||
otherKey: 'genre_id',
|
||||
onDelete: 'CASCADE',
|
||||
as: 'genres',
|
||||
});
|
||||
|
||||
Genre.belongsToMany(Entry, {
|
||||
through: EntryGenre,
|
||||
foreignKey: 'genre_id',
|
||||
otherKey: 'entry_id',
|
||||
onDelete: 'CASCADE',
|
||||
as: 'entries',
|
||||
});
|
||||
|
||||
Entry.hasMany(EntryGenre, { foreignKey: 'entry_id', as: 'entryGenres' });
|
||||
EntryGenre.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
Genre.hasMany(EntryGenre, { foreignKey: 'genre_id', as: 'entryGenres' });
|
||||
EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' });
|
||||
|
||||
|
||||
// * ENTRY & MEDIA - ONE TO MANY
|
||||
// Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', });
|
||||
// Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' });
|
||||
|
||||
Media.belongsTo(Entry, {
|
||||
foreignKey: 'mediaableId',
|
||||
as: 'entry',
|
||||
constraints: false,
|
||||
where: {
|
||||
mediaable: 'entry'
|
||||
}
|
||||
});
|
||||
|
||||
Media.belongsTo(Collection, {
|
||||
foreignKey: 'mediaableId',
|
||||
as: 'collection',
|
||||
constraints: false,
|
||||
where: {
|
||||
mediaable: 'collection'
|
||||
}
|
||||
});
|
||||
|
||||
Entry.hasMany(Media, {
|
||||
foreignKey: 'mediaableId',
|
||||
constraints: false,
|
||||
as: 'medias',
|
||||
scope: { mediaable: 'entry' }
|
||||
});
|
||||
|
||||
Collection.hasMany(Media, {
|
||||
foreignKey: 'mediaableId',
|
||||
constraints: false,
|
||||
as: 'medias',
|
||||
scope: { mediaable: 'collection' }
|
||||
});
|
||||
|
||||
// * ENTRY & SEASON - ONE TO MANY
|
||||
Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', });
|
||||
Season.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
|
||||
|
||||
// * SEASON & EPISODE - ONE TO MANY
|
||||
Season.hasMany(Episode, { foreignKey: 'season_id', onDelete: 'CASCADE', });
|
||||
Episode.belongsTo(Season, { foreignKey: 'season_id' });
|
||||
|
||||
// * EPISODE & EPISODE_SEEN - ONE TO MANY
|
||||
Episode.hasMany(EpisodeSeen, { foreignKey: 'episode_id', onDelete: 'CASCADE', });
|
||||
EpisodeSeen.belongsTo(Episode, { foreignKey: 'episode_id' });
|
||||
|
||||
// * ENTRY & EPISODE_SEEN - ONE TO MANY
|
||||
Entry.hasMany(EpisodeSeen, { foreignKey: 'entry_id', onDelete: 'CASCADE', });
|
||||
EpisodeSeen.belongsTo(Entry, { foreignKey: 'entry_id' });
|
||||
|
||||
// * ----
|
||||
Franchise.hasMany(Entry, { foreignKey: 'franchise_id' });
|
||||
Entry.belongsTo(Franchise, { foreignKey: 'franchise_id' });
|
||||
@@ -0,0 +1,389 @@
|
||||
// Import models
|
||||
const Franchise = require('../models/Franchise');
|
||||
const Entry = require('../models/Entry')
|
||||
const EntryId = require('../models/EntryId')
|
||||
const EntryRating = require('../models/EntryRating')
|
||||
const EntrySeen = require('../models/EntrySeen');
|
||||
const UserCollection = require('../models/Collection');
|
||||
const UserCollectionEntry = require('../models/UserCollectionEntry');
|
||||
const Season = require('../models/Season');
|
||||
const Episode = require('../models/Episode');
|
||||
const EpisodeSeen = require('../models/EpisodeSeen');
|
||||
const Genre = require('../models/Genre');
|
||||
const EntryGenre = require('../models/EntryGenre');
|
||||
const sequelize = require('./database');
|
||||
|
||||
// ! Hooks
|
||||
// ? user collections
|
||||
// * entry add
|
||||
UserCollectionEntry.afterCreate(async (instance, options) => {
|
||||
try {
|
||||
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||
await userCollections.update({
|
||||
name: sequelize.literal('name')
|
||||
});
|
||||
const userCollectionEntriesCount = await UserCollectionEntry.count({
|
||||
where: {
|
||||
user_collection_id: instance.user_collection_id
|
||||
}
|
||||
})
|
||||
|
||||
// ? updating newly create UserCollectionEntry order
|
||||
await instance.update({
|
||||
order: userCollectionEntriesCount
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: UserCollectionEntry.afterCreate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * entry update (order)
|
||||
UserCollectionEntry.afterUpdate(async (instance, options) => {
|
||||
try {
|
||||
// ? mimicking a change in UserCollection to trigger updatedAt property update for the UserCollection
|
||||
const userCollections = await UserCollection.findByPk(instance.user_collection_id);
|
||||
await userCollections.update({
|
||||
name: sequelize.literal('name')
|
||||
});
|
||||
|
||||
// ? updating other entries order
|
||||
const collectionEntries = await UserCollectionEntry.findAll({
|
||||
where: {
|
||||
user_collection_id: instance.user_collection_id
|
||||
},
|
||||
order: [['order', 'ASC']]
|
||||
})
|
||||
|
||||
// * changing the order of all entries that are now after the updated entry
|
||||
let startOrder = instance.order + 1;
|
||||
for (let i = 0; i < collectionEntries.length; i++) {
|
||||
if (collectionEntries[i].order >= instance.order && collectionEntries[i].entry_id != instance.entry_id) {
|
||||
await collectionEntries[i].update({ order: startOrder }, { hooks: false })
|
||||
startOrder++;
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: UserCollectionEntry.afterUpdate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * entry remove
|
||||
UserCollectionEntry.afterDestroy(async (instance, options) => {
|
||||
try {
|
||||
const userCollection = await UserCollection.findByPk(instance.user_collection_id);
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
if (!entry || !userCollection) return;
|
||||
|
||||
// ? mimicking a change to trigger updatedAt property update
|
||||
let updates = {
|
||||
name: sequelize.literal('name')
|
||||
}
|
||||
|
||||
const collectionsEntries = await UserCollectionEntry.findAll({
|
||||
where: {
|
||||
user_collection_id: instance.user_collection_id
|
||||
},
|
||||
include: [
|
||||
{ model: Entry, as: 'entry' },
|
||||
]
|
||||
})
|
||||
|
||||
// ? updating collection poster
|
||||
// ? remove the poster if the collection is empty now
|
||||
// if (collectionsEntries.length == 0) {
|
||||
// updates.poster = null;
|
||||
// } else {
|
||||
// // ? replace poster it belonged to the removed entry
|
||||
// // if (userCollection.poster_path == entry.poster_path) {
|
||||
// // updates.poster_path = collectionsEntries[0].entry.poster_path;
|
||||
// // }
|
||||
// }
|
||||
|
||||
// ? updating order, only if the removed entry isn't placed at the end
|
||||
let order = instance.order;
|
||||
if (instance.order != collectionsEntries.length) {
|
||||
for (let i = 0; i < collectionsEntries.length; i++) {
|
||||
if (collectionsEntries[i].order > instance.order) {
|
||||
collectionsEntries[i].update({ order: order }, { hooks: false })
|
||||
order++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ? updating the collection updatedAt and poster maybe
|
||||
await userCollection.update(updates)
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: UserCollectionEntry.afterDestroy: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// ? entry-seen
|
||||
// * entry-seen create
|
||||
EntrySeen.afterCreate(async (instance, options) => {
|
||||
try {
|
||||
// ? getting the entry that we just created the entry-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// this will ensure that the entry is updated anyways to trigger the updatedAt property
|
||||
let update = {
|
||||
watchlist: false
|
||||
};
|
||||
|
||||
// ? comparing the instance.date_seen with the entry date_first_seen &
|
||||
// ? date_last_seen to check if they should be replaced
|
||||
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
|
||||
update.date_first_seen = instance.date_seen;
|
||||
}
|
||||
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
|
||||
update.date_last_seen = instance.date_seen;
|
||||
}
|
||||
|
||||
// * updating the entry
|
||||
await entry.update(update);
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EntrySeen.afterCreate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * entry-seen udpate
|
||||
EntrySeen.afterUpdate(async (instance, options) => {
|
||||
try {
|
||||
// ? getting the entry that we just created the entry-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
||||
// ? then asigning the first one in the array to: date_first_seen
|
||||
// ? and the last one to: date_last_seen
|
||||
const entrySeens = await EntrySeen.findAll({
|
||||
where: {
|
||||
entry_id: instance.entry_id
|
||||
},
|
||||
order: [
|
||||
['date_seen', 'ASC']
|
||||
]
|
||||
});
|
||||
|
||||
let update = {};
|
||||
update.date_first_seen = entrySeens[0].date_seen;
|
||||
update.date_last_seen = entrySeens[entrySeens.length - 1].date_seen;
|
||||
|
||||
// * updating the entry
|
||||
await entry.update(update);
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * entry-seen delete
|
||||
EntrySeen.afterDestroy(async (instance, options) => {
|
||||
try {
|
||||
// ? getting the entry that we just created the entry-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
||||
// ? then asigning the first one in the array to: date_first_seen
|
||||
// ? and the last one to: date_last_seen
|
||||
const entrySeens = await EntrySeen.findAll({
|
||||
where: {
|
||||
entry_id: instance.entry_id
|
||||
},
|
||||
order: [
|
||||
['date_seen', 'ASC']
|
||||
]
|
||||
});
|
||||
|
||||
let update = {};
|
||||
|
||||
// ? if the deleted entry-seen was the only one available for the current entry
|
||||
// ? then set date_first_seen & date_last_seen values to `null`
|
||||
if (entrySeens.length == 0) {
|
||||
update.date_first_seen = null;
|
||||
update.date_last_seen = null;
|
||||
} else {
|
||||
update.date_first_seen = entrySeens[0].date_seen;
|
||||
update.date_last_seen = entrySeens[entrySeens.length - 1].date_seen;
|
||||
}
|
||||
|
||||
// * updating the entry
|
||||
await entry.update(update);
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EntrySeen.afterUpdate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// ? episode seen
|
||||
// * episode-seen create
|
||||
EpisodeSeen.afterCreate(async (instance, options) => {
|
||||
try {
|
||||
// ! updating the entry
|
||||
// ? getting the entry that we just created the episode-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// this will ensure that the entry is updated anyways to trigger the updatedAt property
|
||||
let update = {
|
||||
// title: sequelize.literal('title')
|
||||
watchlist: false
|
||||
};
|
||||
|
||||
// ? comparing the instance.date_seen with the entry date_first_seen &
|
||||
// ? date_last_seen to check if they should be replaced
|
||||
if (!entry.date_first_seen || instance.date_seen < new Date(entry.date_first_seen)) {
|
||||
update.date_first_seen = instance.date_seen;
|
||||
}
|
||||
if (!entry.date_last_seen || instance.date_seen > new Date(entry.date_last_seen)) {
|
||||
update.date_last_seen = instance.date_seen;
|
||||
}
|
||||
|
||||
// * updating the entry
|
||||
await entry.update(update);
|
||||
|
||||
// ! updating the episode
|
||||
// ? getting the episode that we just created the episode-seen for
|
||||
const episode = await Episode.findByPk(instance.episode_id);
|
||||
|
||||
// this will ensure that the episode is updated anyways to trigger the updatedAt property
|
||||
let episodeUpdate = {
|
||||
name: sequelize.literal('name')
|
||||
};
|
||||
|
||||
// ? comparing the instance.date_seen with the entry date_first_seen &
|
||||
// ? date_last_seen to check if they should be replaced
|
||||
if (!episode.date_first_seen || instance.date_seen < new Date(episode.date_first_seen)) {
|
||||
episodeUpdate.date_first_seen = instance.date_seen;
|
||||
}
|
||||
if (!episode.date_last_seen || instance.date_seen > new Date(episode.date_last_seen)) {
|
||||
episodeUpdate.date_last_seen = instance.date_seen;
|
||||
}
|
||||
|
||||
// * updating the episode
|
||||
await episode.update(episodeUpdate);
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EpisodeSeen.afterCreate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * episode-seen udpate
|
||||
EpisodeSeen.afterUpdate(async (instance, options) => {
|
||||
try {
|
||||
// ! updating the entry
|
||||
// ? getting the entry that we just created the episode-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// ? getting the min & max values of the column date_seen from episode-seen
|
||||
// ? for the current entry
|
||||
EpisodeSeen.findAll({
|
||||
attributes: [
|
||||
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
|
||||
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
|
||||
],
|
||||
where: {
|
||||
entry_id: instance.entry_id
|
||||
}
|
||||
}).then(result => {
|
||||
let update = {};
|
||||
|
||||
update.date_first_seen = result[0].dataValues.minValue;
|
||||
update.date_last_seen = result[0].dataValues.maxValue;
|
||||
|
||||
entry.update(update);
|
||||
}).catch(err => {
|
||||
console.error('Error:', err);
|
||||
});
|
||||
|
||||
// ! updating the episode
|
||||
// ? getting the episode that we just created the episode-seen for
|
||||
const episode = await Episode.findByPk(instance.episode_id);
|
||||
|
||||
// ? getting the min & max values of the column date_seen from episode-seen
|
||||
// ? for the current episode
|
||||
EpisodeSeen.findAll({
|
||||
attributes: [
|
||||
[sequelize.fn('min', sequelize.col('date_seen')), 'minValue'],
|
||||
[sequelize.fn('max', sequelize.col('date_seen')), 'maxValue']
|
||||
],
|
||||
where: {
|
||||
episode_id: instance.episode_id
|
||||
}
|
||||
}).then(result => {
|
||||
let update = {};
|
||||
update.date_first_seen = result[0].dataValues.minValue;
|
||||
update.date_last_seen = result[0].dataValues.maxValue;
|
||||
episode.update(update);
|
||||
}).catch(err => {
|
||||
console.error('Error:', err);
|
||||
});
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
|
||||
}
|
||||
});
|
||||
|
||||
// * episode-seen delete
|
||||
EpisodeSeen.afterDestroy(async (instance, options) => {
|
||||
try {
|
||||
// ! updating the entry
|
||||
// ? getting the entry that we just created the entry-seen for
|
||||
const entry = await Entry.findByPk(instance.entry_id);
|
||||
|
||||
// ? getting the list of entry-seen for the current entry, sorted by date_seen
|
||||
// ? then asigning the first one in the array to: date_first_seen
|
||||
// ? and the last one to: date_last_seen
|
||||
const episodeSeens = await EpisodeSeen.findAll({
|
||||
where: {
|
||||
entry_id: instance.entry_id
|
||||
},
|
||||
order: [
|
||||
['date_seen', 'ASC']
|
||||
]
|
||||
});
|
||||
|
||||
let update = {};
|
||||
|
||||
// ? if the deleted entry-seen was the only one available for the current entry
|
||||
// ? then set date_first_seen & date_last_seen values to `null`
|
||||
if (episodeSeens.length == 0) {
|
||||
update.date_first_seen = null;
|
||||
update.date_last_seen = null;
|
||||
} else {
|
||||
update.date_first_seen = episodeSeens[0].date_seen;
|
||||
update.date_last_seen = episodeSeens[episodeSeens.length - 1].date_seen;
|
||||
}
|
||||
|
||||
// * updating the entry
|
||||
await entry.update(update);
|
||||
|
||||
// ! updating the episode
|
||||
// ? getting the entry that we just created the episode-seen for
|
||||
const episode = await Episode.findByPk(instance.episode_id);
|
||||
|
||||
// ? getting the list of episode-seen for the current episode, sorted by date_seen
|
||||
// ? then asigning the first one in the array to: date_first_seen
|
||||
// ? and the last one to: date_last_seen
|
||||
const episodeSeens2 = await EpisodeSeen.findAll({
|
||||
where: {
|
||||
episode_id: instance.episode_id
|
||||
},
|
||||
order: [
|
||||
['date_seen', 'ASC']
|
||||
]
|
||||
});
|
||||
|
||||
let episodeUpdate = {};
|
||||
|
||||
// ? if the deleted episode-seen was the only one available for the current episode
|
||||
// ? then set date_first_seen & date_last_seen values to `null`
|
||||
if (episodeSeens2.length == 0) {
|
||||
episodeUpdate.date_first_seen = null;
|
||||
episodeUpdate.date_last_seen = null;
|
||||
} else {
|
||||
episodeUpdate.date_first_seen = episodeSeens2[0].date_seen;
|
||||
episodeUpdate.date_last_seen = episodeSeens2[episodeSeens2.length - 1].date_seen;
|
||||
}
|
||||
|
||||
// * updating the episode
|
||||
await episode.update(episodeUpdate);
|
||||
} catch (error) {
|
||||
console.log(`Sequelize Hook Error: EpisodeSeen.afterUpdate: `, error)
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user