Register user

To register a new user with an email address, add the following method:

pub fn register_user(&self, email: &str) -> Result<User, Error> {
insert_into(users::table)
.values((
users::email.eq(email),
))
.returning((
users::id,
users::email
))
.get_result(&self.conn)
.map_err(Error::from)
}

The register_user function expects a string with the email of a user, adds a record to a database, and returns a User instance, which represents a record in the users table.

We use the insert_into method with a table type from the users scope, which is automatically created by the table! macro in the schema module. This method returns an IncompleteInsertStatement instance that provides a values method to set values with an INSERT statement. We set the email column equal to the email variable. The values method call returns an InsertStatement type instance that has the returning method to set columns that will be returned with this statement. We set the returning values to the id and email columns of the users table. The returning method takes ownership of a statement and returns a new InsertStatement instance with the returning values.

At the end, we call the get_result method of the InsertStatement struct to execute the statement and convert the result to the User model. Because we have a different error type of Result, we have to convert the diesel::result::Error type returned by the get_result, method call to the failure::Error type.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.148.104.242