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