workend on update and create employee functions + validator
This commit is contained in:
@@ -1,25 +1,67 @@
|
||||
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||
import Employee from 'App/Models/Employee'
|
||||
import UpdateEmployeeValidator from 'App/Validators/UpdateEmployeeValidator'
|
||||
import CreateEmployeeValidator from 'App/Validators/CreateEmployeeValidator'
|
||||
|
||||
import Database from '@ioc:Adonis/Lucid/Database'
|
||||
|
||||
// TODO: #1 Implement paginator for Employee-Index
|
||||
export default class EmployeesController {
|
||||
public async index ({}: HttpContextContract) {
|
||||
const report = Database.manager.report()
|
||||
|
||||
console.log(report)
|
||||
|
||||
return await Database.from('employees').select('*')
|
||||
}
|
||||
|
||||
public async store ({request}: HttpContextContract) {
|
||||
try {
|
||||
const payload = await request.validate(CreateEmployeeValidator)
|
||||
|
||||
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,
|
||||
})
|
||||
|
||||
} catch (error) {
|
||||
return error
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public async show ({params}: HttpContextContract) {
|
||||
return await Employee.find(params.id)
|
||||
}
|
||||
|
||||
public async update ({params, request}: HttpContextContract) {
|
||||
|
||||
const employee : Employee = await Employee.findOrFail(params.id)
|
||||
|
||||
try {
|
||||
const payload = await request.validate(UpdateEmployeeValidator)
|
||||
|
||||
employee.firstName = payload.firstName
|
||||
employee.lastName = payload.lastName ?? ''
|
||||
employee.shorthand = payload.shorthand
|
||||
employee.email = payload.email ?? ''
|
||||
employee.phone = payload.phone ?? ''
|
||||
employee.mobile = payload.mobile ?? ''
|
||||
employee.contractHours = payload.contractHours ?? 0
|
||||
|
||||
return await employee.save()
|
||||
|
||||
} catch(error) {
|
||||
return error
|
||||
}
|
||||
}
|
||||
|
||||
public async create ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async store ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async show ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async edit ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async update ({}: HttpContextContract) {
|
||||
}
|
||||
|
||||
public async destroy ({}: HttpContextContract) {
|
||||
public async destroy ({params}: HttpContextContract) {
|
||||
return await Database.from('employees').where('id', params.id).delete()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user