Various refactoring

This commit is contained in:
Geomitron
2020-03-03 21:48:41 -05:00
parent 442736205e
commit 4ebf2db650
34 changed files with 503 additions and 329 deletions

View File

@@ -3,23 +3,34 @@ import Database from '../shared/Database'
import { SongSearch, SearchType, SongResult } from '../shared/interfaces/search.interface'
import { escape } from 'mysql'
/**
* Handles the 'song-search' event.
*/
export default class SearchHandler implements IPCInvokeHandler<'song-search'> {
event: 'song-search' = 'song-search'
// TODO: add method documentation
/**
* @returns the top 20 songs that match `search`.
*/
async handler(search: SongSearch) {
const db = await Database.getInstance()
return db.sendQuery(this.getSearchQuery(search)) as Promise<SongResult[]>
}
/**
* @returns a database query that returns the type of results expected by `search.type`.
*/
private getSearchQuery(search: SongSearch) {
switch(search.type) {
switch (search.type) {
case SearchType.Any: return this.getGeneralSearchQuery(search.query)
default: return '<<<ERROR>>>' // TODO: add more search types
}
}
/**
* @returns a database query that returns the top 20 songs that match `search`.
*/
private getGeneralSearchQuery(searchString: string) {
return `
SELECT id, name, artist, album, genre, year