100 lines
2.3 KiB
JavaScript
100 lines
2.3 KiB
JavaScript
const Entry = require('../models/Entry.js')
|
|
const Collection = require('../models/Franchise.js')
|
|
const EntryId = require('../models/EntryId.js')
|
|
const EntryRating = require('../models/EntryRating.js');
|
|
const Season = require('../models/Season.js');
|
|
|
|
async function saveEntry(data) {
|
|
// creating the object
|
|
var title = data.title || data.name;
|
|
var original_title = data.original_title || data.original_name;
|
|
|
|
var runtime = data.runtime || null;
|
|
|
|
var is_tvseries = data.release_date == null ? true : false;
|
|
var release_date = data.release_date || data.first_air_date;
|
|
|
|
let movie = Entry.build({
|
|
title: title,
|
|
original_title: original_title,
|
|
release_date: release_date,
|
|
runtime: runtime,
|
|
|
|
storyline: data.overview,
|
|
country: data.production_countries.length > 0 ? data.production_countries[0].name : null,
|
|
|
|
is_tvseries: is_tvseries,
|
|
status: data.status,
|
|
|
|
poster_path: data.poster_path,
|
|
});
|
|
|
|
const savedMovie = await movie.save();
|
|
|
|
// Assuming EntryRating and EntryId are correctly defined and imported
|
|
const ratings = await EntryRating.create({
|
|
entry_id: savedMovie.id,
|
|
user_rating: 0, // Assuming a default value for user rating
|
|
tmdb_rating: data.vote_average,
|
|
});
|
|
|
|
const ids = await EntryId.create({
|
|
entry_id: savedMovie.id,
|
|
tmdb_id: data.id,
|
|
imdb_id: data.imdb_id,
|
|
});
|
|
|
|
let seasons = [];
|
|
if (is_tvseries) {
|
|
data.seasons.forEach(async (season) => {
|
|
const s = await Season.create({
|
|
tmdb_id: season.id,
|
|
|
|
entry_id: savedMovie.id,
|
|
|
|
name: season.name,
|
|
overview: season.overview,
|
|
season_number: season.season_number,
|
|
air_date: season.air_date,
|
|
|
|
user_rating: 0,
|
|
tmdb_rating: season.vote_average,
|
|
|
|
poster_path: season.poster_path,
|
|
banner_path: null,
|
|
})
|
|
|
|
seasons.push(s)
|
|
});
|
|
|
|
await savedMovie.setDataValue('seasons', seasons);
|
|
}
|
|
|
|
savedMovie.setDataValue('entryRating', ratings);
|
|
savedMovie.setDataValue('entryId', ids);
|
|
|
|
return savedMovie;
|
|
}
|
|
|
|
async function updateEntry() {
|
|
|
|
}
|
|
|
|
async function getEntry(id) {
|
|
return Entry.findByPk(id);
|
|
}
|
|
|
|
async function deleteEntry(id) {
|
|
await Entry.destroy({
|
|
where: {
|
|
id: id
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
saveEntry,
|
|
updateEntry,
|
|
getEntry,
|
|
deleteEntry,
|
|
} |