From bb186c0d67139f076ca8ec1aa99ac0841b3036f3 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 22 Aug 2022 17:04:48 +0200 Subject: Get all users --- server/src/models/users.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 server/src/models/users.rs (limited to 'server/src/models/users.rs') 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 { + 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, 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) + } +} -- cgit v1.2.3-18-g5258