Back: reworked the media/entry relation

This commit is contained in:
X4Lo committed 2024-09-10 21:32:46 +01:00
1 parent d06a21097f
commit b42e055691
5 files changed
+41 -30

No files matched your search

+15 -7
View File
@@ -4,7 +4,7 @@ const Entry = require('../models/Entry')
const EntryId = require('../models/EntryId') const EntryId = require('../models/EntryId')
const EntryRating = require('../models/EntryRating') const EntryRating = require('../models/EntryRating')
const EntrySeen = require('../models/EntrySeen'); const EntrySeen = require('../models/EntrySeen');
const UserCollection = require('../models/UserCollection'); const Collection = require('../models/Collection');
const UserCollectionEntry = require('../models/UserCollectionEntry'); const UserCollectionEntry = require('../models/UserCollectionEntry');
const Season = require('../models/Season'); const Season = require('../models/Season');
const Episode = require('../models/Episode'); const Episode = require('../models/Episode');
@@ -41,7 +41,7 @@ Entry.hasMany(EntrySeen, {
EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); EntrySeen.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
// * ENTRY & LIST - MANY TO MANY // * ENTRY & LIST - MANY TO MANY
Entry.belongsToMany(UserCollection, { Entry.belongsToMany(Collection, {
through: UserCollectionEntry, through: UserCollectionEntry,
foreignKey: 'entry_id', foreignKey: 'entry_id',
otherKey: 'user_collection_id', otherKey: 'user_collection_id',
@@ -49,7 +49,7 @@ Entry.belongsToMany(UserCollection, {
as: 'collections', as: 'collections',
}); });
UserCollection.belongsToMany(Entry, { Collection.belongsToMany(Entry, {
through: UserCollectionEntry, through: UserCollectionEntry,
foreignKey: 'user_collection_id', foreignKey: 'user_collection_id',
otherKey: 'entry_id', otherKey: 'entry_id',
@@ -59,8 +59,8 @@ UserCollection.belongsToMany(Entry, {
Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries' }); Entry.hasMany(UserCollectionEntry, { foreignKey: 'entry_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' }); UserCollectionEntry.belongsTo(Entry, { foreignKey: 'entry_id', as: 'entry' });
UserCollection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' }); Collection.hasMany(UserCollectionEntry, { foreignKey: 'user_collection_id', as: 'userCollectionEntries' });
UserCollectionEntry.belongsTo(UserCollection, { foreignKey: 'user_collection_id', as: 'userCollection' }); UserCollectionEntry.belongsTo(Collection, { foreignKey: 'user_collection_id', as: 'userCollection' });
// * ENTRY & GENRE - MANY TO MANY // * ENTRY & GENRE - MANY TO MANY
Entry.belongsToMany(Genre, { Entry.belongsToMany(Genre, {
@@ -86,8 +86,16 @@ EntryGenre.belongsTo(Genre, { foreignKey: 'genre_id', as: 'genre' });
// * ENTRY & MEDIA - ONE TO MANY // * ENTRY & MEDIA - ONE TO MANY
Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', }); // Entry.hasMany(Media, { foreignKey: 'entryId', as: 'medias', onDelete: 'CASCADE', });
Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' }); // Media.belongsTo(Entry, { foreignKey: 'entryId', as: 'entry' });
// * ENTRY & MEDIA - ONE TO MANY
Media.belongsTo(Entry, { foreignKey: 'mediaableId', as: 'entry', constraints: false, scope: { mediaable: 'entry' } });
Media.belongsTo(Collection, { foreignKey: 'mediaableId', as: 'collection', constraints: false, scope: { mediaable: 'collection' } });
// * ENTRY & COLLECTION - ONE TO MANY
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 & SEASON - ONE TO MANY
Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', }); Entry.hasMany(Season, { foreignKey: 'entry_id', as: 'seasons', onDelete: 'CASCADE', });
@@ -1,7 +1,7 @@
const { DataTypes } = require('sequelize'); const { DataTypes } = require('sequelize');
const sequelize = require('../db/database'); const sequelize = require('../db/database');
const UserCollection = sequelize.define("userCollection", const Collection = sequelize.define("userCollection",
{ {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
tmdbId: { type: DataTypes.STRING(30), field: 'tmdb_id' }, tmdbId: { type: DataTypes.STRING(30), field: 'tmdb_id' },
@@ -19,4 +19,4 @@ const UserCollection = sequelize.define("userCollection",
} }
); );
module.exports = UserCollection; module.exports = Collection;
+12 -9
View File
@@ -1,18 +1,21 @@
const { DataTypes } = require('sequelize'); const { DataTypes, Sequelize } = require('sequelize');
const sequelize = require('../db/database'); const sequelize = require('../db/database');
const Media = sequelize.define("media", const Media = sequelize.define("media",
{ {
id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true }, id: { type: DataTypes.INTEGER, primaryKey: true, autoIncrement: true },
entryId: { // entryId: {
type: DataTypes.INTEGER, // type: DataTypes.INTEGER,
references: { // references: {
model: 'Entries', // model: 'Entries',
key: 'id' // key: 'id'
}, // },
field: 'entry_id' // field: 'entry_id'
}, // },
mediaable: { type: Sequelize.STRING },
mediaableId: { type: Sequelize.INTEGER, field: 'mediaable_id' },
type: { type: DataTypes.STRING(50) }, type: { type: DataTypes.STRING(50) },
path: { type: DataTypes.STRING(200) }, path: { type: DataTypes.STRING(200) },
+7 -9
View File
@@ -1,13 +1,14 @@
const express = require('express'); const express = require('express');
const entryRouter = express.Router(); const entryRouter = express.Router();
const sharp = require('sharp');
const path = require('path'); const path = require('path');
const fs = require('fs');
const { fetchEntryData, fetchCollectionData } = require('../services/tmdbService'); const { fetchEntryData, fetchCollectionData } = require('../services/tmdbService');
const { saveEntry, refrechEntry, SyncSeriesData } = require('../services/models-services/entryService'); const { saveEntry, refrechEntry, SyncSeriesData } = require('../services/models-services/entryService');
const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService'); const { bulkImportMovies, bulkImportTmdb, bulkImportEpisodes } = require('../services/importerService');
const { entryPicturesUpload } = require('../services/mutlerStorageLocations');
const { generatePreview } = require('../services/mediaService');
const { Sequelize } = require('sequelize');
const Entry = require('../models/Entry'); const Entry = require('../models/Entry');
const EntryId = require('../models/EntryId'); const EntryId = require('../models/EntryId');
const EntryRating = require('../models/EntryRating'); const EntryRating = require('../models/EntryRating');
@@ -16,13 +17,9 @@ const EntrySeen = require('../models/EntrySeen');
const Season = require('../models/Season'); const Season = require('../models/Season');
const Media = require('../models/Media'); const Media = require('../models/Media');
const Episode = require('../models/Episode'); const Episode = require('../models/Episode');
const { entryPicturesUpload } = require('../services/mutlerStorageLocations'); const UserCollection = require('../models/Collection');
const { generatePreview } = require('../services/mediaService');
const UserCollection = require('../models/UserCollection');
const { Sequelize } = require('sequelize');
// ! /entries // ! /entries
// * GET - ALL ENTRIES // * GET - ALL ENTRIES
entryRouter.get('/', async (req, res) => { entryRouter.get('/', async (req, res) => {
const { page, pageSize } = req.body; const { page, pageSize } = req.body;
@@ -64,7 +61,6 @@ entryRouter.get('/sync-series', async (req, res, next) => {
} }
}); });
// * GET ENTRY BY ID // * GET ENTRY BY ID
entryRouter.get('/:id', async (req, res) => { entryRouter.get('/:id', async (req, res) => {
try { try {
@@ -81,6 +77,7 @@ entryRouter.get('/:id', async (req, res) => {
{ model: EntrySeen, as: 'entrySeen' }, { model: EntrySeen, as: 'entrySeen' },
{ model: Season, as: 'seasons' }, { model: Season, as: 'seasons' },
{ model: Media, as: 'medias' }, { model: Media, as: 'medias' },
{ model: UserCollection, as: 'collections' },
{ {
model: Genre, model: Genre,
as: 'genres', as: 'genres',
@@ -375,7 +372,8 @@ entryRouter.post('/:id/media', upload, async (req, res) => {
if (fileType) { if (fileType) {
const file = req.files[fileType][0]; const file = req.files[fileType][0];
const createdMedia = await Media.create({ const createdMedia = await Media.create({
entryId: id, mediaable: 'entry',
mediaableId: id,
type: fileType, type: fileType,
path: file.filename, path: file.filename,
isLocal: true, isLocal: true,
@@ -130,12 +130,13 @@ async function saveEntry(data) {
// ? cast // ? cast
// ? images // ? media
const downloadedImages = await getEntryMedia(data.id, is_tvseries); const downloadedImages = await getEntryMedia(data.id, is_tvseries);
console.log(`entry service, downloaded images: `, downloadedImages) console.log(`entry service, downloaded images: `, downloadedImages)
for (const image of downloadedImages) { for (const image of downloadedImages) {
await Media.create({ await Media.create({
entryId: savedEntry.id, mediaable: 'entry',
mediaableId: savedEntry.id,
type: image.type, type: image.type,
path: image.path, path: image.path,
isLocal: true, isLocal: true,
@@ -528,7 +529,8 @@ async function refrechEntry(entryId) {
if (downloadedImages) { if (downloadedImages) {
for (const image of downloadedImages) { for (const image of downloadedImages) {
await Media.create({ await Media.create({
entryId: oldEntry.id, mediaable: 'entry',
mediaableId: oldEntry.id,
type: image.type, type: image.type,
path: image.path, path: image.path,
isLocal: true, isLocal: true,