Add global search, add reset all audiobooks

This commit is contained in:
advplyr 2021-08-21 16:23:35 -05:00
parent fb0a6f4ec2
commit f70e1beca1
18 changed files with 323 additions and 33 deletions

View file

@ -17,8 +17,9 @@ class ApiController {
this.router.get('/find/covers', this.findCovers.bind(this))
this.router.get('/find/:method', this.find.bind(this))
this.router.get('/audiobooks', this.getAudiobooks.bind(this))
this.router.delete('/audiobooks', this.deleteAllAudiobooks.bind(this))
this.router.get('/audiobook/:id', this.getAudiobook.bind(this))
this.router.delete('/audiobook/:id', this.deleteAudiobook.bind(this))
this.router.patch('/audiobook/:id/tracks', this.updateAudiobookTracks.bind(this))
@ -57,9 +58,22 @@ class ApiController {
}
getAudiobooks(req, res) {
Logger.info('Get Audiobooks')
var audiobooksMinified = this.db.audiobooks.map(ab => ab.toJSONMinified())
res.json(audiobooksMinified)
var audiobooks = []
if (req.query.q) {
audiobooks = this.db.audiobooks.filter(ab => {
return ab.isSearchMatch(req.query.q)
}).map(ab => ab.toJSONMinified())
} else {
audiobooks = this.db.audiobooks.map(ab => ab.toJSONMinified())
}
res.json(audiobooks)
}
async deleteAllAudiobooks(req, res) {
Logger.info('Removing all Audiobooks')
var success = await this.db.recreateAudiobookDb()
if (success) res.sendStatus(200)
else res.sendStatus(500)
}
getAudiobook(req, res) {

View file

@ -207,5 +207,9 @@ class Audiobook {
this.addTrack(file)
})
}
isSearchMatch(search) {
return this.book.isSearchMatch(search.toLowerCase().trim())
}
}
module.exports = Audiobook

View file

@ -16,6 +16,10 @@ class Book {
}
}
get _title() { return this.title || '' }
get _author() { return this.author || '' }
get _series() { return this.series || '' }
construct(book) {
this.olid = book.olid
this.title = book.title
@ -81,5 +85,9 @@ class Book {
}
return true
}
isSearchMatch(search) {
return this._title.toLowerCase().includes(search) || this._author.toLowerCase().includes(search) || this._series.toLowerCase().includes(search)
}
}
module.exports = Book

View file

@ -28,6 +28,12 @@ class Db {
return this.settingsDb
}
getEntityDbKey(entityName) {
if (entityName === 'user') return 'usersDb'
else if (entityName === 'audiobook') return 'audiobooksDb'
return 'settingsDb'
}
getEntityArrayKey(entityName) {
if (entityName === 'user') return 'users'
else if (entityName === 'audiobook') return 'audiobooks'
@ -155,6 +161,18 @@ class Db {
})
}
recreateAudiobookDb() {
return this.audiobooksDb.drop().then((results) => {
Logger.info(`[DB] Dropped audiobook db`, results)
this.audiobooksDb = new njodb.Database(this.AudiobooksPath)
this.audiobooks = []
return true
}).catch((error) => {
Logger.error(`[DB] Failed to drop audiobook db`, error)
return false
})
}
getGenres() {
var allGenres = []
this.db.audiobooks.forEach((audiobook) => {

View file

@ -108,8 +108,11 @@ class Server {
if (process.env.NODE_ENV === 'production') {
const distPath = Path.join(global.appRoot, '/client/dist')
app.use(express.static(distPath))
app.use('/local', express.static(this.AudiobookPath))
} else {
app.use(express.static(this.AudiobookPath))
}
app.use(express.static(this.AudiobookPath))
app.use(express.static(this.MetadataPath))
app.use(express.urlencoded({ extended: true }));
app.use(express.json())