Update libraryItem model to include libraryId

This commit is contained in:
advplyr 2023-03-21 17:06:08 -05:00
parent d745e6b656
commit 633e83a4ab
7 changed files with 161 additions and 4 deletions

View File

@ -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
}

View File

@ -254,9 +254,92 @@ const getLibraryItemExpanded = (libraryItemId) => {
model: Database.models.libraryFile, model: Database.models.libraryFile,
include: 'fileMetadata' 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, model: Database.models.book,
include: 'library' 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 = { module.exports = {
getLibraryItemMinified, getLibraryItemMinified,
getLibraryItemFull, getLibraryItemFull,
getLibraryItemExpanded getLibraryItemExpanded,
getLibraryItemsForLibrary
} }

24
server/db/library.db.js Normal file
View File

@ -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
}

View File

@ -33,7 +33,10 @@ module.exports = (sequelize) => {
modelName: 'libraryItem' modelName: 'libraryItem'
}) })
const { libraryFolder, book, podcast } = sequelize.models const { library, libraryFolder, book, podcast } = sequelize.models
library.hasMany(LibraryItem)
LibraryItem.belongsTo(library)
libraryFolder.hasMany(LibraryItem) libraryFolder.hasMany(LibraryItem)
LibraryItem.belongsTo(libraryFolder) LibraryItem.belongsTo(libraryFolder)

View File

@ -1,8 +1,10 @@
const express = require('express') const express = require('express')
const items = require('./items') const items = require('./items')
const libraries = require('./libraries')
const router = express.Router() const router = express.Router()
router.use('/items', items) router.use('/items', items)
router.use('/libraries', libraries)
module.exports = router module.exports = router

View File

@ -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

View File

@ -601,6 +601,11 @@ function migrateLibraryItems(oldLibraryItems) {
Logger.error(`[dbMigration] migrateLibraryItems: Old library folder id not found "${oldLibraryItem.folderId}"`) Logger.error(`[dbMigration] migrateLibraryItems: Old library folder id not found "${oldLibraryItem.folderId}"`)
continue continue
} }
const libraryId = oldDbIdMap.libraries[oldLibraryItem.libraryId]
if (!libraryId) {
Logger.error(`[dbMigration] migrateLibraryItems: Old library id not found "${oldLibraryItem.libraryId}"`)
continue
}
// //
// Migrate LibraryItem // Migrate LibraryItem
@ -622,6 +627,7 @@ function migrateLibraryItems(oldLibraryItems) {
lastScanVersion: oldLibraryItem.scanVersion, lastScanVersion: oldLibraryItem.scanVersion,
createdAt: oldLibraryItem.addedAt, createdAt: oldLibraryItem.addedAt,
updatedAt: oldLibraryItem.updatedAt, updatedAt: oldLibraryItem.updatedAt,
libraryId,
libraryFolderId libraryFolderId
} }
oldDbIdMap.libraryItems[oldLibraryItem.id] = LibraryItem.id oldDbIdMap.libraryItems[oldLibraryItem.id] = LibraryItem.id