diff --git a/server/controllers2/library.controller.js b/server/controllers2/library.controller.js new file mode 100644 index 00000000..312cfb29 --- /dev/null +++ b/server/controllers2/library.controller.js @@ -0,0 +1,28 @@ +const libraryDb = require('../db/library.db') +const itemDb = require('../db/item.db') + +const getAllLibraries = async (req, res) => { + const libraries = await libraryDb.getAllLibraries() + res.json({ + libraries + }) +} + +const getLibrary = async (req, res) => { + const library = await libraryDb.getLibrary(req.params.id) + if (!library) return res.sendStatus(404) + res.json(library) +} + +const getLibraryItems = async (req, res) => { + const libraryItems = await itemDb.getLibraryItemsForLibrary(req.params.id) + res.json({ + libraryItems + }) +} + +module.exports = { + getAllLibraries, + getLibrary, + getLibraryItems +} \ No newline at end of file diff --git a/server/db/item.db.js b/server/db/item.db.js index 36aa511b..4512539d 100644 --- a/server/db/item.db.js +++ b/server/db/item.db.js @@ -254,9 +254,92 @@ const getLibraryItemExpanded = (libraryItemId) => { model: Database.models.libraryFile, include: 'fileMetadata' }, + 'libraryFolder', + 'library' + ] + }) +} + +const getLibraryItemsForLibrary = async (libraryId) => { + return Database.models.libraryItem.findAll({ + where: { + libraryId + }, + limit: 50, + order: [ + [Database.models.book, 'title', 'DESC'], + [Database.models.podcast, 'title', 'DESC'] + ], + include: [ { - model: Database.models.libraryFolder, - include: 'library' + 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: [] + } + }, + ] } ] }) @@ -265,5 +348,6 @@ const getLibraryItemExpanded = (libraryItemId) => { module.exports = { getLibraryItemMinified, getLibraryItemFull, - getLibraryItemExpanded + getLibraryItemExpanded, + getLibraryItemsForLibrary } \ No newline at end of file diff --git a/server/db/library.db.js b/server/db/library.db.js new file mode 100644 index 00000000..82265297 --- /dev/null +++ b/server/db/library.db.js @@ -0,0 +1,24 @@ +const Database = require('../Database') + +const getAllLibraries = () => { + return Database.models.library.findAll({ + include: { + model: Database.models.librarySetting, + attributes: ['key', 'value'] + } + }) +} + +const getLibrary = (libraryId) => { + return Database.models.library.findByPk(libraryId, { + include: { + model: Database.models.librarySetting, + attributes: ['key', 'value'] + } + }) +} + +module.exports = { + getAllLibraries, + getLibrary +} \ No newline at end of file diff --git a/server/models/LibraryItem.js b/server/models/LibraryItem.js index 93203aca..f4f4ee65 100644 --- a/server/models/LibraryItem.js +++ b/server/models/LibraryItem.js @@ -33,7 +33,10 @@ module.exports = (sequelize) => { modelName: 'libraryItem' }) - const { libraryFolder, book, podcast } = sequelize.models + const { library, libraryFolder, book, podcast } = sequelize.models + library.hasMany(LibraryItem) + LibraryItem.belongsTo(library) + libraryFolder.hasMany(LibraryItem) LibraryItem.belongsTo(libraryFolder) diff --git a/server/routes/index.js b/server/routes/index.js index b74add6c..7f0be4a0 100644 --- a/server/routes/index.js +++ b/server/routes/index.js @@ -1,8 +1,10 @@ const express = require('express') const items = require('./items') +const libraries = require('./libraries') const router = express.Router() router.use('/items', items) +router.use('/libraries', libraries) module.exports = router \ No newline at end of file diff --git a/server/routes/libraries.js b/server/routes/libraries.js new file mode 100644 index 00000000..6ec5e304 --- /dev/null +++ b/server/routes/libraries.js @@ -0,0 +1,10 @@ +const express = require('express') +const LibraryController = require('../controllers2/library.controller') + +const router = express.Router() + +router.get('/', LibraryController.getAllLibraries) +router.get('/:id', LibraryController.getLibrary) +router.get('/:id/items', LibraryController.getLibraryItems) + +module.exports = router \ No newline at end of file diff --git a/server/utils/migrations/dbMigration.js b/server/utils/migrations/dbMigration.js index bf4324a4..6fead3ae 100644 --- a/server/utils/migrations/dbMigration.js +++ b/server/utils/migrations/dbMigration.js @@ -601,6 +601,11 @@ function migrateLibraryItems(oldLibraryItems) { Logger.error(`[dbMigration] migrateLibraryItems: Old library folder id not found "${oldLibraryItem.folderId}"`) continue } + const libraryId = oldDbIdMap.libraries[oldLibraryItem.libraryId] + if (!libraryId) { + Logger.error(`[dbMigration] migrateLibraryItems: Old library id not found "${oldLibraryItem.libraryId}"`) + continue + } // // Migrate LibraryItem @@ -622,6 +627,7 @@ function migrateLibraryItems(oldLibraryItems) { lastScanVersion: oldLibraryItem.scanVersion, createdAt: oldLibraryItem.addedAt, updatedAt: oldLibraryItem.updatedAt, + libraryId, libraryFolderId } oldDbIdMap.libraryItems[oldLibraryItem.id] = LibraryItem.id