diff options
Diffstat (limited to 'server/src/models')
-rw-r--r-- | server/src/models/mod.rs | 1 | ||||
-rw-r--r-- | server/src/models/users.rs | 39 |
2 files changed, 40 insertions, 0 deletions
diff --git a/server/src/models/mod.rs b/server/src/models/mod.rs new file mode 100644 index 0000000..913bd46 --- /dev/null +++ b/server/src/models/mod.rs @@ -0,0 +1 @@ +pub mod users; diff --git a/server/src/models/users.rs b/server/src/models/users.rs new file mode 100644 index 0000000..d7a836f --- /dev/null +++ b/server/src/models/users.rs @@ -0,0 +1,39 @@ +use crate::db::get_client; +use crate::errors::AppError; + +use serde::{Deserialize, Serialize}; + +#[derive(Deserialize, Serialize)] +pub struct User { + id: i32, + email: String, + password: String, +} + +impl User { + pub async fn create(user: User) -> Result<i32, AppError> { + let pool = unsafe { get_client() }; + let rec = sqlx::query!( + r#" +INSERT INTO users (email, password) +VALUES ( $1, $2 ) +RETURNING id + "#, + user.email, + user.password + ) + .fetch_one(pool) + .await?; + + Ok(rec.id) + } + + pub async fn list() -> Result<Vec<User>, AppError> { + let pool = unsafe { get_client() }; + let rows = sqlx::query_as!(User, r#"SELECT id, email, password FROM users"#) + .fetch_all(pool) + .await?; + + Ok(rows) + } +} |