Add message

The add_message method adds a message related to a channel and a user to the messages table:

pub fn add_message(&self, channel_id: Id, user_id: Id, text: &str)
-> Result<Message, Error>
{
let timestamp = Utc::now().naive_utc();
insert_into(messages::table)
.values((
messages::timestamp.eq(timestamp),
messages::channel_id.eq(channel_id),
messages::user_id.eq(user_id),
messages::text.eq(text),
))
.returning((
messages::id,
messages::timestamp,
messages::channel_id,
messages::user_id,
messages::text,
))
.get_result(&self.conn)
.map_err(Error::from)
}

The implementation also uses the insert_into function, but we also created the timestamp manually. You can avoid setting this field manually and set a default value to the current timestamp in the timestamp column.

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

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