Added simpleSearch functionality to EmployeesController, solves #2
This commit is contained in:
@@ -4,101 +4,136 @@ import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator'
|
||||
import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator'
|
||||
|
||||
import Database from '@ioc:Adonis/Lucid/Database'
|
||||
import Logger from '@ioc:Adonis/Core/Logger'
|
||||
|
||||
// TODO: #1 Implement paginator for Employee-Index
|
||||
export default class EmployeesController {
|
||||
public async index ({bouncer, request}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.index')
|
||||
public async index ({bouncer, request}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.index')
|
||||
|
||||
const limit: number = request.qs().limit ?? 10
|
||||
const page: number = request.qs().page ?? 1
|
||||
const sort_by = await this.sort_by(request.qs().sort_by)
|
||||
const limit: number = request.qs().limit ?? 10
|
||||
const page: number = request.qs().page ?? 1
|
||||
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')
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
try {
|
||||
const payload = await request.validate(CreateEmployeeValidator)
|
||||
if(simpleSearch) {
|
||||
simpleSearch.columns.forEach(column => {
|
||||
employees.orWhere(column, 'like', `%${simpleSearch.query}%`)
|
||||
})
|
||||
}
|
||||
|
||||
return await Employee.create({
|
||||
firstName: payload.firstName,
|
||||
lastName: payload.lastName,
|
||||
shorthand: payload.shorthand,
|
||||
email: payload.email,
|
||||
phone: payload.phone,
|
||||
mobile: payload.mobile,
|
||||
contractHours: payload.contractHours,
|
||||
})
|
||||
employees.orderBy(sortBy)
|
||||
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
return employees.paginate(page, limit)
|
||||
}
|
||||
|
||||
}
|
||||
public async store ({request}: HttpContextContract) {
|
||||
try {
|
||||
const payload = await request.validate(CreateEmployeeValidator)
|
||||
|
||||
public async show ({params, bouncer}: HttpContextContract) {
|
||||
const emp = await Employee.findOrFail(params.id)
|
||||
return await Employee.create({
|
||||
firstName: payload.firstName,
|
||||
lastName: payload.lastName,
|
||||
shorthand: payload.shorthand,
|
||||
email: payload.email,
|
||||
phone: payload.phone,
|
||||
mobile: payload.mobile,
|
||||
contractHours: payload.contractHours,
|
||||
})
|
||||
|
||||
await bouncer.authorize('employees.show', emp)
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
|
||||
return emp
|
||||
}
|
||||
}
|
||||
|
||||
public async update ({params, bouncer, response, request}: HttpContextContract) {
|
||||
public async show ({params, bouncer}: HttpContextContract) {
|
||||
const emp = await Employee.findOrFail(params.id)
|
||||
|
||||
const employee : Employee = await Employee.findOrFail(params.id)
|
||||
const editContractHours : boolean = employee.contractHours !== request.input('contractHours')
|
||||
await bouncer.authorize('employees.show', emp)
|
||||
|
||||
await bouncer.authorize('employees.update', editContractHours, employee)
|
||||
return emp
|
||||
}
|
||||
|
||||
const payload = await request.validate(UpdateEmployeeValidator)
|
||||
public async update ({params, bouncer, response, request}: HttpContextContract) {
|
||||
|
||||
if (editContractHours){
|
||||
employee.contractHours = payload.contractHours ?? 0
|
||||
}
|
||||
const employee : Employee = await Employee.findOrFail(params.id)
|
||||
const editContractHours : boolean = employee.contractHours !== request.input('contractHours')
|
||||
|
||||
employee.firstName = payload.firstName
|
||||
employee.lastName = payload.lastName ?? ''
|
||||
employee.shorthand = payload.shorthand
|
||||
employee.email = payload.email ?? ''
|
||||
employee.phone = payload.phone ?? ''
|
||||
employee.mobile = payload.mobile ?? ''
|
||||
await bouncer.authorize('employees.update', editContractHours, employee)
|
||||
|
||||
await employee.save()
|
||||
const payload = await request.validate(UpdateEmployeeValidator)
|
||||
|
||||
return response.ok({
|
||||
status: 200,
|
||||
message: "Employee updated successfully"
|
||||
})
|
||||
}
|
||||
if (editContractHours){
|
||||
employee.contractHours = payload.contractHours ?? 0
|
||||
}
|
||||
|
||||
public async destroy ({params, bouncer}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.destroy')
|
||||
employee.firstName = payload.firstName
|
||||
employee.lastName = payload.lastName ?? ''
|
||||
employee.shorthand = payload.shorthand
|
||||
employee.email = payload.email ?? ''
|
||||
employee.phone = payload.phone ?? ''
|
||||
employee.mobile = payload.mobile ?? ''
|
||||
|
||||
return await Database.from('employees').where('id', params.id).delete()
|
||||
}
|
||||
await employee.save()
|
||||
|
||||
private async sort_by(qs: string): Promise<{column : string, order?: 'asc' | 'desc' | undefined}[]> {
|
||||
const regex : RegExp = /(asc|desc)\((\w+)\)/gi
|
||||
const client = Database.connection()
|
||||
let result: {
|
||||
column: string,
|
||||
order?: 'asc' | 'desc' | undefined
|
||||
}[] = []
|
||||
return response.ok({
|
||||
status: 200,
|
||||
message: "Employee updated successfully"
|
||||
})
|
||||
}
|
||||
|
||||
const columns = await client.columnsInfo('employees')
|
||||
const match = qs?.matchAll(regex) ?? []
|
||||
public async destroy ({params, bouncer}: HttpContextContract) {
|
||||
await bouncer.authorize('employees.destroy')
|
||||
|
||||
for (const item of match) {
|
||||
if( columns.hasOwnProperty(item[2]) && (item[1] === 'asc' || item[1] === 'desc')) result.push({column: item[2], order: item[1]})
|
||||
}
|
||||
return await Database.from('employees').where('id', params.id).delete()
|
||||
}
|
||||
|
||||
if(result.length === 0) result.push({column: 'last_name'})
|
||||
private async sortBy(qs: string): Promise<{column : string, order?: 'asc' | 'desc' | undefined}[]> {
|
||||
const regex : RegExp = /(asc|desc)\((\w+)\)/gi
|
||||
const client = Database.connection()
|
||||
let result: {
|
||||
column: string,
|
||||
order?: 'asc' | 'desc' | undefined
|
||||
}[] = []
|
||||
|
||||
return result
|
||||
}
|
||||
const columns = await client.columnsInfo('employees')
|
||||
const match = qs?.matchAll(regex) ?? []
|
||||
|
||||
for (const item of match) {
|
||||
if( columns.hasOwnProperty(item[2]) && (item[1] === 'asc' || item[1] === 'desc')) result.push({column: item[2], order: item[1]})
|
||||
}
|
||||
|
||||
if(result.length === 0) result.push({column: 'last_name'})
|
||||
|
||||
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