Update routes/controllers/db structure

This commit is contained in:
advplyr 2023-03-20 15:52:33 -05:00
parent 258b9ec54e
commit b62e88c4ed
7 changed files with 306 additions and 282 deletions

View File

@ -21,8 +21,9 @@ const Db = require('./Db')
const Database = require('./Database')
const SocketAuthority = require('./SocketAuthority')
const routes = require('./routes/index')
const ApiRouter = require('./routers/ApiRouter')
const ApiRouter2 = require('./routers/ApiRouter2')
const HlsRouter = require('./routers/HlsRouter')
const StaticRouter = require('./routers/StaticRouter')
@ -171,7 +172,7 @@ class Server {
// Static folder
router.use(express.static(Path.join(global.appRoot, 'static')))
router.use('/api/v1', new ApiRouter2(this).router)
router.use('/api/v1', routes)
router.use('/api', this.authMiddleware.bind(this), this.apiRouter.router)
router.use('/hls', this.authMiddleware.bind(this), this.hlsRouter.router)
router.use('/s', this.authMiddleware.bind(this), this.staticRouter.router)

View File

@ -1,265 +0,0 @@
const { Sequelize } = require('sequelize')
const Database = require('../Database')
class LibraryItemController {
constructor() { }
// Example get library item full, expanded, minified
async get(req, res) {
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: [
{
model: Database.models.audioTrack
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'authors',
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'narrators',
through: {
attributes: []
}
},
{
model: Database.models.series,
through: {
attributes: ['sequence']
}
},
{
model: Database.models.bookChapter
},
{
model: Database.models.eBookFile,
include: 'fileMetadata'
}
]
},
{
model: Database.models.podcast,
include: [
{
model: Database.models.podcastEpisode,
include: {
model: Database.models.audioTrack
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
]
}
],
expanded: [
{
model: Database.models.book,
include: [
{
model: Database.models.fileMetadata,
as: 'imageFile'
},
{
model: Database.models.audioTrack,
include: {
model: Database.models.mediaFile,
include: [
'fileMetadata',
'mediaStreams'
]
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'authors',
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'narrators',
through: {
attributes: []
}
},
{
model: Database.models.series,
through: {
attributes: ['sequence']
}
},
{
model: Database.models.bookChapter
},
{
model: Database.models.eBookFile,
include: 'fileMetadata'
}
]
},
{
model: Database.models.podcast,
include: [
{
model: Database.models.fileMetadata,
as: 'imageFile'
},
{
model: Database.models.podcastEpisode,
include: {
model: Database.models.audioTrack,
include: {
model: Database.models.mediaFile,
include: [
'fileMetadata',
'mediaStreams'
]
}
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
]
},
{
model: Database.models.libraryFile,
include: 'fileMetadata'
},
{
model: Database.models.libraryFolder,
include: 'library'
}
]
}
const LibraryItem = await Database.models.libraryItem.findByPk(req.params.id, {
include: include[key]
})
res.json(LibraryItem)
}
}
module.exports = new LibraryItemController()

View File

@ -0,0 +1,18 @@
const itemDb = require('../db/item.db')
const getLibraryItem = async (req, res) => {
let libraryItem = null
if (req.query.minified == 1) {
libraryItem = await itemDb.getLibraryItemMinified(req.params.id)
} else if (req.query.expanded == 1) {
libraryItem = await itemDb.getLibraryItemExpanded(req.params.id)
} else {
libraryItem = await itemDb.getLibraryItemFull(req.params.id)
}
res.json(libraryItem)
}
module.exports = {
getLibraryItem
}

269
server/db/item.db.js Normal file
View File

@ -0,0 +1,269 @@
const { Sequelize } = require('sequelize')
const Database = require('../Database')
const getLibraryItemMinified = (libraryItemId) => {
return Database.models.libraryItem.findByPk(libraryItemId, {
include: [
{
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: []
}
},
]
}
]
})
}
const getLibraryItemFull = (libraryItemId) => {
return Database.models.libraryItem.findByPk(libraryItemId, {
include: [
{
model: Database.models.book,
include: [
{
model: Database.models.audioTrack
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'authors',
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'narrators',
through: {
attributes: []
}
},
{
model: Database.models.series,
through: {
attributes: ['sequence']
}
},
{
model: Database.models.bookChapter
},
{
model: Database.models.eBookFile,
include: 'fileMetadata'
}
]
},
{
model: Database.models.podcast,
include: [
{
model: Database.models.podcastEpisode,
include: {
model: Database.models.audioTrack
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
]
}
]
})
}
const getLibraryItemExpanded = (libraryItemId) => {
return Database.models.libraryItem.findByPk(libraryItemId, {
include: [
{
model: Database.models.book,
include: [
{
model: Database.models.fileMetadata,
as: 'imageFile'
},
{
model: Database.models.audioTrack,
include: {
model: Database.models.mediaFile,
include: [
'fileMetadata',
'mediaStreams'
]
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'authors',
through: {
attributes: []
}
},
{
model: Database.models.person,
as: 'narrators',
through: {
attributes: []
}
},
{
model: Database.models.series,
through: {
attributes: ['sequence']
}
},
{
model: Database.models.bookChapter
},
{
model: Database.models.eBookFile,
include: 'fileMetadata'
}
]
},
{
model: Database.models.podcast,
include: [
{
model: Database.models.fileMetadata,
as: 'imageFile'
},
{
model: Database.models.podcastEpisode,
include: {
model: Database.models.audioTrack,
include: {
model: Database.models.mediaFile,
include: [
'fileMetadata',
'mediaStreams'
]
}
}
},
{
model: Database.models.genre,
through: {
attributes: []
}
},
{
model: Database.models.tag,
through: {
attributes: []
}
},
]
},
{
model: Database.models.libraryFile,
include: 'fileMetadata'
},
{
model: Database.models.libraryFolder,
include: 'library'
}
]
})
}
module.exports = {
getLibraryItemMinified,
getLibraryItemFull,
getLibraryItemExpanded
}

View File

@ -1,15 +0,0 @@
const express = require('express')
const LibraryItemController = require('../controllers2/LibraryItemController')
class ApiRouter2 {
constructor(Server) {
this.router = express()
this.router.disable('x-powered-by')
this.init()
}
init() {
this.router.get('/items/:id', LibraryItemController.get.bind(this))
}
}
module.exports = ApiRouter2

8
server/routes/index.js Normal file
View File

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

8
server/routes/items.js Normal file
View File

@ -0,0 +1,8 @@
const express = require('express')
const LibraryItemController = require('../controllers2/item.controller')
const router = express.Router()
router.get('/:id', LibraryItemController.getLibraryItem)
module.exports = router