From 258b9ec54e93173c31b88c8d00fe315306c78002 Mon Sep 17 00:00:00 2001 From: advplyr Date: Sun, 19 Mar 2023 17:41:27 -0500 Subject: [PATCH] Update library item example route --- server/Server.js | 2 +- server/controllers2/LibraryItemController.js | 79 +++++++++++++++++++- 2 files changed, 77 insertions(+), 4 deletions(-) diff --git a/server/Server.js b/server/Server.js index 136ca0b7..f094def2 100644 --- a/server/Server.js +++ b/server/Server.js @@ -104,7 +104,7 @@ class Server { await this.playbackSessionManager.removeOrphanStreams() // TODO: Test new db connection - const force = true + const force = false await Database.init(force) if (force) await dbMigration3.migrate() diff --git a/server/controllers2/LibraryItemController.js b/server/controllers2/LibraryItemController.js index 608ac1d9..e6ea6f42 100644 --- a/server/controllers2/LibraryItemController.js +++ b/server/controllers2/LibraryItemController.js @@ -1,13 +1,86 @@ +const { Sequelize } = require('sequelize') const Database = require('../Database') class LibraryItemController { constructor() { } - // Example get library item fully expanded or minified + // Example get library item full, expanded, minified async get(req, res) { - const key = req.query.minified == 1 ? 'minified' : 'full' + const key = req.query.minified == 1 ? 'minified' : req.query.expanded == 1 ? 'expanded' : 'full' const include = { minified: [ + { + model: Database.models.book, + attributes: [ + 'id', 'title', 'subtitle', 'publishedYear', 'publishedDate', 'publisher', 'description', 'isbn', 'asin', 'language', 'explicit', + [Sequelize.literal('(SELECT COUNT(*) FROM "audioTracks" WHERE "audioTracks"."mediaItemId" = book.id)'), 'numAudioTracks'], + [Sequelize.literal('(SELECT COUNT(*) FROM "bookChapters" WHERE "bookChapters"."bookId" = book.id)'), 'numChapters'] + ], + include: [ + { + model: Database.models.genre, + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + { + model: Database.models.tag, + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + { + model: Database.models.person, + as: 'authors', + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + { + model: Database.models.person, + as: 'narrators', + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + { + model: Database.models.series, + attributes: ['id', 'name'], + through: { + attributes: ['sequence'] + } + } + ] + }, + { + model: Database.models.podcast, + attributes: [ + 'id', 'title', 'author', 'releaseDate', 'feedURL', 'imageURL', 'description', 'itunesPageURL', 'itunesId', 'itunesArtistId', 'language', 'podcastType', 'explicit', 'autoDownloadEpisodes', + [Sequelize.literal('(SELECT COUNT(*) FROM "podcastEpisodes" WHERE "podcastEpisodes"."podcastId" = podcast.id)'), 'numPodcastEpisodes'] + ], + include: [ + { + model: Database.models.genre, + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + { + model: Database.models.tag, + attributes: ['id', 'name'], + through: { + attributes: [] + } + }, + ] + } + ], + full: [ { model: Database.models.book, include: [ @@ -79,7 +152,7 @@ class LibraryItemController { ] } ], - full: [ + expanded: [ { model: Database.models.book, include: [