77 lines
2.1 KiB
JavaScript
77 lines
2.1 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 getCollections() {
|
|
const collections = await Collection.findAll({
|
|
include: [
|
|
{
|
|
model: Entry,
|
|
as: "entries",
|
|
include: { model: Media, as: "medias" },
|
|
},
|
|
{ model: Media, as: "medias" },
|
|
],
|
|
order: [["name", "ASC"]],
|
|
});
|
|
|
|
return collections;
|
|
}
|
|
|
|
async getCollectionById(id) {
|
|
const collection = 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
|
|
});
|
|
|
|
return collection;
|
|
}
|
|
|
|
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();
|