57 lines
1.7 KiB
JavaScript
57 lines
1.7 KiB
JavaScript
const { Sequelize } = require("sequelize");
|
|
const Collection = require("../models/Collection");
|
|
const Entry = require("../models/Entry");
|
|
const EntryId = require("../models/EntryId");
|
|
const EntryRating = require("../models/EntryRating");
|
|
const Genre = require("../models/Genre");
|
|
const Media = require("../models/Media");
|
|
const Season = require("../models/Season");
|
|
const UserCollectionEntry = require("../models/UserCollectionEntry");
|
|
|
|
|
|
class CollectionService {
|
|
|
|
constructor() {
|
|
if (!CollectionService.instance) {
|
|
CollectionService.instance = this;
|
|
}
|
|
return CollectionService.instance;
|
|
}
|
|
|
|
async getCollectionById(id) {
|
|
return await Collection.findByPk(id, {
|
|
include: [{
|
|
model: Entry,
|
|
as: 'entries',
|
|
include: [
|
|
{ model: EntryId, as: 'entryId' },
|
|
{ model: EntryRating, as: 'entryRating' },
|
|
{ model: Season, as: 'seasons' },
|
|
{ model: Media, as: 'medias' },
|
|
{ model: Genre, as: 'genres' },
|
|
],
|
|
through: {
|
|
model: UserCollectionEntry,
|
|
attributes: ['order'], // attributes (to include) from the join model
|
|
},
|
|
}],
|
|
order: [[Sequelize.literal("`entries->UserCollectionEntry`.`order`"), 'ASC']], // sort by 'order' in ascending order
|
|
});
|
|
}
|
|
|
|
async createOrUpdateCollection(collectionData) {
|
|
return await Collection.findOrCreate({
|
|
where: { tmdbId: collectionData.id },
|
|
defaults: {
|
|
name: collectionData.name,
|
|
posterPath: collectionData.poster_path,
|
|
backdropPath: collectionData.backdrop_path,
|
|
description: collectionData.overview
|
|
},
|
|
include: [{ model: Media, as: 'medias' }]
|
|
});
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = new CollectionService(); |