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 { 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 {
|
export default class EmployeesController {
|
||||||
public async index ({}: HttpContextContract) {
|
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 create ({}: HttpContextContract) {
|
public async show ({params}: HttpContextContract) {
|
||||||
|
return await Employee.find(params.id)
|
||||||
}
|
}
|
||||||
|
|
||||||
public async store ({}: HttpContextContract) {
|
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 show ({}: HttpContextContract) {
|
public async destroy ({params}: HttpContextContract) {
|
||||||
}
|
return await Database.from('employees').where('id', params.id).delete()
|
||||||
|
|
||||||
public async edit ({}: HttpContextContract) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public async update ({}: HttpContextContract) {
|
|
||||||
}
|
|
||||||
|
|
||||||
public async destroy ({}: HttpContextContract) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ export default class Employee extends BaseModel {
|
|||||||
public firstName : string
|
public firstName : string
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public lastName : string
|
public lastName : string | undefined
|
||||||
|
|
||||||
@column()
|
@column()
|
||||||
public shorthand: string
|
public shorthand: string
|
||||||
|
|||||||
83
app/Validators/CreateEmployeeValidator.ts
Normal file
83
app/Validators/CreateEmployeeValidator.ts
Normal file
@@ -0,0 +1,83 @@
|
|||||||
|
import { schema, rules } from '@ioc:Adonis/Core/Validator'
|
||||||
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
|
||||||
|
export default class CreateEmployeeValidator {
|
||||||
|
id : number
|
||||||
|
|
||||||
|
constructor (protected ctx: HttpContextContract) {
|
||||||
|
this.id = ctx.params.id
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define schema to validate the "shape", "type", "formatting" and "integrity" of data.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
* 1. The username must be of data type string. But then also, it should
|
||||||
|
* not contain special characters or numbers.
|
||||||
|
* ```
|
||||||
|
* schema.string({}, [ rules.alpha() ])
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* 2. The email must be of data type string, formatted as a valid
|
||||||
|
* email. But also, not used by any other user.
|
||||||
|
* ```
|
||||||
|
* schema.string({}, [
|
||||||
|
* rules.email(),
|
||||||
|
* rules.unique({ table: 'users', column: 'email' }),
|
||||||
|
* ])
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
public schema = schema.create({
|
||||||
|
|
||||||
|
firstName: schema.string({
|
||||||
|
trim: true
|
||||||
|
}),
|
||||||
|
|
||||||
|
lastName: schema.string.optional({
|
||||||
|
trim: true
|
||||||
|
}),
|
||||||
|
|
||||||
|
shorthand: schema.string({
|
||||||
|
trim: true
|
||||||
|
},
|
||||||
|
[
|
||||||
|
rules.unique({
|
||||||
|
table: 'employees',
|
||||||
|
column: 'shorthand',
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
|
||||||
|
email: schema.string.optional({
|
||||||
|
trim: true
|
||||||
|
},
|
||||||
|
[
|
||||||
|
rules.email({
|
||||||
|
sanitize: true
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
|
||||||
|
phone: schema.string.optional(),
|
||||||
|
|
||||||
|
mobile: schema.string.optional(),
|
||||||
|
|
||||||
|
contractHours: schema.number.optional()
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom messages for validation failures. You can make use of dot notation `(.)`
|
||||||
|
* for targeting nested fields and array expressions `(*)` for targeting all
|
||||||
|
* children of an array. For example:
|
||||||
|
*
|
||||||
|
* {
|
||||||
|
* 'profile.username.required': 'Username is required',
|
||||||
|
* 'scores.*.number': 'Define scores as valid numbers'
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public messages = {
|
||||||
|
'shorthand.unique': 'Shorthand has to be unique.',
|
||||||
|
required: '{{ field }} is required.'
|
||||||
|
}
|
||||||
|
}
|
||||||
86
app/Validators/UpdateEmployeeValidator.ts
Normal file
86
app/Validators/UpdateEmployeeValidator.ts
Normal file
@@ -0,0 +1,86 @@
|
|||||||
|
import { schema, rules } from '@ioc:Adonis/Core/Validator'
|
||||||
|
import { HttpContextContract } from '@ioc:Adonis/Core/HttpContext'
|
||||||
|
|
||||||
|
export default class UpdateEmployeeValidator {
|
||||||
|
id : number
|
||||||
|
|
||||||
|
constructor (protected ctx: HttpContextContract) {
|
||||||
|
this.id = ctx.params.id
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Define schema to validate the "shape", "type", "formatting" and "integrity" of data.
|
||||||
|
*
|
||||||
|
* For example:
|
||||||
|
* 1. The username must be of data type string. But then also, it should
|
||||||
|
* not contain special characters or numbers.
|
||||||
|
* ```
|
||||||
|
* schema.string({}, [ rules.alpha() ])
|
||||||
|
* ```
|
||||||
|
*
|
||||||
|
* 2. The email must be of data type string, formatted as a valid
|
||||||
|
* email. But also, not used by any other user.
|
||||||
|
* ```
|
||||||
|
* schema.string({}, [
|
||||||
|
* rules.email(),
|
||||||
|
* rules.unique({ table: 'users', column: 'email' }),
|
||||||
|
* ])
|
||||||
|
* ```
|
||||||
|
*/
|
||||||
|
public schema = schema.create({
|
||||||
|
|
||||||
|
firstName: schema.string({
|
||||||
|
trim: true
|
||||||
|
}),
|
||||||
|
|
||||||
|
lastName: schema.string.optional({
|
||||||
|
trim: true
|
||||||
|
}),
|
||||||
|
|
||||||
|
shorthand: schema.string({
|
||||||
|
trim: true
|
||||||
|
},
|
||||||
|
[
|
||||||
|
rules.unique({
|
||||||
|
table: 'employees',
|
||||||
|
column: 'shorthand',
|
||||||
|
whereNot: {
|
||||||
|
id: this.ctx.params.id
|
||||||
|
}
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
|
||||||
|
email: schema.string.optional({
|
||||||
|
trim: true
|
||||||
|
},
|
||||||
|
[
|
||||||
|
rules.email({
|
||||||
|
sanitize: true
|
||||||
|
})
|
||||||
|
]),
|
||||||
|
|
||||||
|
phone: schema.string.optional(),
|
||||||
|
|
||||||
|
mobile: schema.string.optional(),
|
||||||
|
|
||||||
|
contractHours: schema.number.optional()
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Custom messages for validation failures. You can make use of dot notation `(.)`
|
||||||
|
* for targeting nested fields and array expressions `(*)` for targeting all
|
||||||
|
* children of an array. For example:
|
||||||
|
*
|
||||||
|
* {
|
||||||
|
* 'profile.username.required': 'Username is required',
|
||||||
|
* 'scores.*.number': 'Define scores as valid numbers'
|
||||||
|
* }
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public messages = {
|
||||||
|
'shorthand.unique': 'Shorthand has to be unique.',
|
||||||
|
required: '{{ field }} is required.'
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,9 +12,9 @@ export default class Employees extends BaseSchema {
|
|||||||
*/
|
*/
|
||||||
table.timestamp('created_at', { useTz: true })
|
table.timestamp('created_at', { useTz: true })
|
||||||
table.timestamp('updated_at', { useTz: true })
|
table.timestamp('updated_at', { useTz: true })
|
||||||
table.string('first_name ')
|
table.string('first_name').notNullable()
|
||||||
table.string('last_name')
|
table.string('last_name')
|
||||||
table.string('shorthand').unique()
|
table.string('shorthand').unique().notNullable()
|
||||||
table.string('email')
|
table.string('email')
|
||||||
table.string('phone')
|
table.string('phone')
|
||||||
table.string('mobile')
|
table.string('mobile')
|
||||||
|
|||||||
Reference in New Issue
Block a user