Improved search

This commit is contained in:
Evert Prants 2018-10-05 17:10:05 +03:00
parent a07de062cd
commit 266f8d9a3b
Signed by: evert
GPG Key ID: 1688DA83D222D0B5

View File

@ -43,11 +43,38 @@ router.get('/tracks', async (req, res) => {
})
})
let srchcategories = ['title', 'artist', 'album']
router.get('/tracks/search', async (req, res) => {
let query = req.query.string
let qr = ''
let exact = false
if (query.indexOf(':') !== -1) {
let ctr = query.split(':')
if (srchcategories.indexOf(ctr[0]) !== -1) {
qr = `ifnull(${ctr[0]}, '')`
query = query.substring(ctr[0].length + 1)
}
}
if (qr === '') {
for (let c in srchcategories) {
let cat = srchcategories[c]
if (parseInt(c) !== 0) qr += ' || '
qr += `ifnull(${cat}, '')`
}
}
if (query.indexOf('=') !== -1 && query.indexOf('\\=') === -1) {
query = query.replace('=', '')
exact = true
}
if (!exact) query = `%${query}%`
let db = await dbPromise
let tracks = await db.all('SELECT * FROM Track WHERE title || ifnull(artist,\'\') || ifnull(album,\'\') LIKE ? LIMIT 100', `%${query}%`)
let tracks = await db.all(`SELECT * FROM Track WHERE ${qr} LIKE ? LIMIT 100`, query)
for (let i in tracks) {
delete tracks[i].file