Added simpleSearch functionality to EmployeesController, solves #2
This commit is contained in:
@@ -4,7 +4,6 @@ import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator'
|
|||||||
import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator'
|
import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator'
|
||||||
|
|
||||||
import Database from '@ioc:Adonis/Lucid/Database'
|
import Database from '@ioc:Adonis/Lucid/Database'
|
||||||
import Logger from '@ioc:Adonis/Core/Logger'
|
|
||||||
|
|
||||||
// TODO: #1 Implement paginator for Employee-Index
|
// TODO: #1 Implement paginator for Employee-Index
|
||||||
export default class EmployeesController {
|
export default class EmployeesController {
|
||||||
@@ -13,11 +12,22 @@ export default class EmployeesController {
|
|||||||
|
|
||||||
const limit: number = request.qs().limit ?? 10
|
const limit: number = request.qs().limit ?? 10
|
||||||
const page: number = request.qs().page ?? 1
|
const page: number = request.qs().page ?? 1
|
||||||
const sort_by = await this.sort_by(request.qs().sort_by)
|
const sortBy = await this.sortBy(request.qs().sort_by)
|
||||||
|
|
||||||
const employees = await Database.from('employees').orderBy(sort_by).paginate(page, limit)
|
const simpleSearch = await this.simpleSearch(request.qs().simple_search)
|
||||||
|
|
||||||
return employees
|
const employees = Database.query()
|
||||||
|
employees.from('employees')
|
||||||
|
|
||||||
|
if(simpleSearch) {
|
||||||
|
simpleSearch.columns.forEach(column => {
|
||||||
|
employees.orWhere(column, 'like', `%${simpleSearch.query}%`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
employees.orderBy(sortBy)
|
||||||
|
|
||||||
|
return employees.paginate(page, limit)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async store ({request}: HttpContextContract) {
|
public async store ({request}: HttpContextContract) {
|
||||||
@@ -82,7 +92,7 @@ export default class EmployeesController {
|
|||||||
return await Database.from('employees').where('id', params.id).delete()
|
return await Database.from('employees').where('id', params.id).delete()
|
||||||
}
|
}
|
||||||
|
|
||||||
private async sort_by(qs: string): Promise<{column : string, order?: 'asc' | 'desc' | undefined}[]> {
|
private async sortBy(qs: string): Promise<{column : string, order?: 'asc' | 'desc' | undefined}[]> {
|
||||||
const regex : RegExp = /(asc|desc)\((\w+)\)/gi
|
const regex : RegExp = /(asc|desc)\((\w+)\)/gi
|
||||||
const client = Database.connection()
|
const client = Database.connection()
|
||||||
let result: {
|
let result: {
|
||||||
@@ -101,4 +111,29 @@ export default class EmployeesController {
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async simpleSearch(qs : string): Promise<{query: string, columns: string[]} | false>{
|
||||||
|
if (!qs) return false
|
||||||
|
|
||||||
|
const regex = /([\w@.]+)(?:\((\w+(?:,\w+)*)\))*/i
|
||||||
|
const columns = await Database.connection().columnsInfo('employees')
|
||||||
|
|
||||||
|
const match = qs.match(regex) ?? []
|
||||||
|
|
||||||
|
let result: {
|
||||||
|
query: string,
|
||||||
|
columns: string[]
|
||||||
|
} = {
|
||||||
|
query: match[1] ?? '',
|
||||||
|
columns: []
|
||||||
|
}
|
||||||
|
|
||||||
|
match[2]?.split(',').filter(elem => columns.hasOwnProperty(elem.trim())).forEach((col) => {
|
||||||
|
result.columns.push(col.trim())
|
||||||
|
})
|
||||||
|
|
||||||
|
if(result.columns.length < 1) result.columns = ['first_name', 'last_name']
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user