Major Backend Single Responsibility Update part 1
This commit is contained in:
1 parent
65177c7553
commit
cbdffb21e3
41 files changed
+1723
-888
No files matched your search
@@ -0,0 +1,57 @@
|
||||
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();
|
||||
Reference in new issue
Block a user