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 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 {
|
||||||
public async index ({bouncer, request}: HttpContextContract) {
|
public async index ({bouncer, request}: HttpContextContract) {
|
||||||
await bouncer.authorize('employees.index')
|
await bouncer.authorize('employees.index')
|
||||||
|
|
||||||
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')
|
||||||
|
|
||||||
public async store ({request}: HttpContextContract) {
|
if(simpleSearch) {
|
||||||
try {
|
simpleSearch.columns.forEach(column => {
|
||||||
const payload = await request.validate(CreateEmployeeValidator)
|
employees.orWhere(column, 'like', `%${simpleSearch.query}%`)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
return await Employee.create({
|
employees.orderBy(sortBy)
|
||||||
firstName: payload.firstName,
|
|
||||||
lastName: payload.lastName,
|
|
||||||
shorthand: payload.shorthand,
|
|
||||||
email: payload.email,
|
|
||||||
phone: payload.phone,
|
|
||||||
mobile: payload.mobile,
|
|
||||||
contractHours: payload.contractHours,
|
|
||||||
})
|
|
||||||
|
|
||||||
} catch (error) {
|
return employees.paginate(page, limit)
|
||||||
return error
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
public async store ({request}: HttpContextContract) {
|
||||||
|
try {
|
||||||
|
const payload = await request.validate(CreateEmployeeValidator)
|
||||||
|
|
||||||
public async show ({params, bouncer}: HttpContextContract) {
|
return await Employee.create({
|
||||||
const emp = await Employee.findOrFail(params.id)
|
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)
|
await bouncer.authorize('employees.show', emp)
|
||||||
const editContractHours : boolean = employee.contractHours !== request.input('contractHours')
|
|
||||||
|
|
||||||
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){
|
const employee : Employee = await Employee.findOrFail(params.id)
|
||||||
employee.contractHours = payload.contractHours ?? 0
|
const editContractHours : boolean = employee.contractHours !== request.input('contractHours')
|
||||||
}
|
|
||||||
|
|
||||||
employee.firstName = payload.firstName
|
await bouncer.authorize('employees.update', editContractHours, employee)
|
||||||
employee.lastName = payload.lastName ?? ''
|
|
||||||
employee.shorthand = payload.shorthand
|
|
||||||
employee.email = payload.email ?? ''
|
|
||||||
employee.phone = payload.phone ?? ''
|
|
||||||
employee.mobile = payload.mobile ?? ''
|
|
||||||
|
|
||||||
await employee.save()
|
const payload = await request.validate(UpdateEmployeeValidator)
|
||||||
|
|
||||||
return response.ok({
|
if (editContractHours){
|
||||||
status: 200,
|
employee.contractHours = payload.contractHours ?? 0
|
||||||
message: "Employee updated successfully"
|
}
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
public async destroy ({params, bouncer}: HttpContextContract) {
|
employee.firstName = payload.firstName
|
||||||
await bouncer.authorize('employees.destroy')
|
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}[]> {
|
return response.ok({
|
||||||
const regex : RegExp = /(asc|desc)\((\w+)\)/gi
|
status: 200,
|
||||||
const client = Database.connection()
|
message: "Employee updated successfully"
|
||||||
let result: {
|
})
|
||||||
column: string,
|
}
|
||||||
order?: 'asc' | 'desc' | undefined
|
|
||||||
}[] = []
|
|
||||||
|
|
||||||
const columns = await client.columnsInfo('employees')
|
public async destroy ({params, bouncer}: HttpContextContract) {
|
||||||
const match = qs?.matchAll(regex) ?? []
|
await bouncer.authorize('employees.destroy')
|
||||||
|
|
||||||
for (const item of match) {
|
return await Database.from('employees').where('id', params.id).delete()
|
||||||
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'})
|
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