Improved search
This commit is contained in:
parent
a07de062cd
commit
266f8d9a3b
29
server.js
29
server.js
@ -43,11 +43,38 @@ router.get('/tracks', async (req, res) => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
let srchcategories = ['title', 'artist', 'album']
|
||||||
router.get('/tracks/search', async (req, res) => {
|
router.get('/tracks/search', async (req, res) => {
|
||||||
let query = req.query.string
|
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 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) {
|
for (let i in tracks) {
|
||||||
delete tracks[i].file
|
delete tracks[i].file
|
||||||
|
Loading…
Reference in New Issue
Block a user