Adding Our Create Handler

Background

The create handler uses the same route as index but has a different verb. For this handler, we're going to validate two pieces of information - email and password - then pass that data off to a createuser() method in models.userService which will use QB to insert the data into the DB.

Making It Work

Our handler looks like this in handlers.Users

function create( event, rc, prc ){
	var valid = validateOrFail(
		target      = rc,
		constraints = {
			"email"    : { "required" : true, "type" : "email", "size" : "1..256" },
			"password" : { "required" : true, "type" : "string", "size" : "1..256" }
		}
	);

	var created = userService.createUser( valid.email, valid.password );

	prc.response.setData( created ).addMessage( "user created" );
}

The createuser method in models.UserService looks like this:\

function createUser( required string email, required string password ){
	return qb
		.from( "users" )
		.insert( {
			email        : arguments.email,
			password     : arguments.password,
			created_date : now()
		} )
}

To add some Update Logic we added this function to the users entity \

function preUpdate(){
	setModified_date(now());
}

Last updated