191 lines
4.7 KiB
JavaScript
191 lines
4.7 KiB
JavaScript
const Entry = require('../models/Entry.js')
|
|
const EntryId = require('../models/EntryId.js')
|
|
const EntryRating = require('../models/EntryRating.js');
|
|
const Season = require('../models/Season.js');
|
|
const EntryGenre = require('../models/EntryGenre.js');
|
|
const EntrySeen = require('../models/EntrySeen.js');
|
|
|
|
async function saveEntry(data) {
|
|
// creating the object
|
|
var is_tvseries = data.release_date == null ? true : false;
|
|
|
|
let movie = Entry.build({
|
|
title: data.title || data.name,
|
|
original_title: data.original_title || data.original_name,
|
|
release_date: data.release_date || data.first_air_date,
|
|
runtime: data.runtime || null,
|
|
|
|
tagline: data.tagline || null,
|
|
storyline: data.overview,
|
|
country: data.production_countries.length > 0 ? data.production_countries[0].name : null,
|
|
|
|
is_tvseries: is_tvseries,
|
|
status: data.status,
|
|
|
|
genres: data.genre_ids,
|
|
|
|
poster_path: data.poster_path,
|
|
banner_path: data.backdrop_path,
|
|
});
|
|
|
|
const savedMovie = await movie.save();
|
|
|
|
// genres
|
|
data.genres.forEach(async (genre) => {
|
|
await EntryGenre.create({
|
|
genre_id: genre.id,
|
|
entry_id: savedMovie.id
|
|
})
|
|
});
|
|
|
|
const ratings = await EntryRating.create({
|
|
entry_id: savedMovie.id,
|
|
user_rating: 0,
|
|
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);
|
|
}
|
|
|
|
const currentGenresNames = await data.genres.map(genre => genre.name);
|
|
|
|
|
|
savedMovie.setDataValue('entryId', ids);
|
|
savedMovie.setDataValue('entryRating', ratings);
|
|
savedMovie.setDataValue('genres', currentGenresNames);
|
|
|
|
return savedMovie;
|
|
}
|
|
|
|
async function updateEntry(oldEntry, data) {
|
|
let newData = {
|
|
title: data.title || data.name,
|
|
original_title: data.original_title || data.original_name,
|
|
release_date: data.release_date || data.first_air_date,
|
|
runtime: data.runtime || null,
|
|
|
|
tagline: data.tagline || null,
|
|
storyline: data.overview,
|
|
country: data.production_countries.length > 0 ? data.production_countries[0].name : null,
|
|
|
|
status: data.status,
|
|
|
|
poster_path: data.poster_path,
|
|
banner_path: data.backdrop_path,
|
|
};
|
|
|
|
const savedEntry = await oldEntry.update(newData);
|
|
|
|
// update ids
|
|
// TODO: get more ids
|
|
// [affectedCount, affectedRows]
|
|
const ids = await oldEntry.entryId.update({
|
|
imdb_id: data.imdb_id,
|
|
});
|
|
|
|
// update ratings
|
|
const ratings = await oldEntry.entryRating.update({
|
|
tmdb_rating: data.vote_average
|
|
});
|
|
|
|
// update genres
|
|
const currentGenresIds = oldEntry.genres.map(genre => genre.id);
|
|
let currentGenresNames = [];
|
|
data.genres.forEach(async (genre) => {
|
|
currentGenresNames.push(genre.name);
|
|
if (currentGenresIds.includes(genre.id)) return;
|
|
|
|
await EntryGenre.create({
|
|
genre_id: genre.id,
|
|
entry_id: savedEntry.id
|
|
})
|
|
});
|
|
|
|
// entry seens
|
|
const entrySeens = await EntrySeen.findAll({ where: { entry_id: oldEntry.id } });
|
|
|
|
// getting updated genres name
|
|
// const currentGenresNames = await data.genres.map((genre) => genre.name);
|
|
|
|
// TODO: refrech seasons
|
|
// let seasons = [];
|
|
// if (oldEntry.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);
|
|
// }
|
|
|
|
savedEntry.setDataValue('entryId', ids);
|
|
savedEntry.setDataValue('entryRating', ratings);
|
|
savedEntry.setDataValue('genres', currentGenresNames);
|
|
savedEntry.setDataValue('entrySeen', entrySeens);
|
|
|
|
return savedEntry;
|
|
}
|
|
|
|
async function getEntry(id) {
|
|
return Entry.findByPk(id);
|
|
}
|
|
|
|
async function deleteEntry(id) {
|
|
await Entry.destroy({
|
|
where: {
|
|
id: id
|
|
}
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
saveEntry,
|
|
updateEntry,
|
|
getEntry,
|
|
deleteEntry,
|
|
} |