Skip to content

Commit 74daa6c

Browse files
committed
models/token: Extract NewApiToken::insert() fn
1 parent ffe9537 commit 74daa6c

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/models/token.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ pub struct NewApiToken {
2525
pub expired_at: Option<NaiveDateTime>,
2626
}
2727

28+
impl NewApiToken {
29+
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<ApiToken> {
30+
diesel::insert_into(api_tokens::table)
31+
.values(self)
32+
.returning(ApiToken::as_returning())
33+
.get_result(conn)
34+
.await
35+
}
36+
}
37+
2838
/// The model representing a row in the `api_tokens` database table.
2939
#[derive(Debug, Identifiable, Queryable, Selectable, Associations, Serialize)]
3040
#[diesel(belongs_to(User))]
@@ -76,15 +86,9 @@ impl ApiToken {
7686
expired_at,
7787
};
7888

79-
let model: ApiToken = diesel::insert_into(api_tokens::table)
80-
.values(new_token)
81-
.returning(ApiToken::as_returning())
82-
.get_result(conn)
83-
.await?;
84-
8589
Ok(CreatedApiToken {
8690
plaintext: token,
87-
model,
91+
model: new_token.insert(conn).await?,
8892
})
8993
}
9094

0 commit comments

Comments
 (0)