January 2019
Intermediate to advanced
520 pages
14h 32m
English
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 ...
Read now
Unlock full access