summaryrefslogtreecommitdiff
path: root/server
diff options
context:
space:
mode:
authorSanto Cariotti <santo@dcariotti.me>2022-08-22 17:04:48 +0200
committerSanto Cariotti <santo@dcariotti.me>2022-08-22 17:04:48 +0200
commitbb186c0d67139f076ca8ec1aa99ac0841b3036f3 (patch)
tree16d7b46a53527fed2ab6fbbd1ab6f421311c514e /server
parent78c3d9f61e2087cd09fdd36f002f910a4394c7f3 (diff)
Get all users
Diffstat (limited to 'server')
-rw-r--r--server/src/main.rs6
-rw-r--r--server/src/models/mod.rs1
-rw-r--r--server/src/models/users.rs39
3 files changed, 44 insertions, 2 deletions
diff --git a/server/src/main.rs b/server/src/main.rs
index 3b080eb..8319fb2 100644
--- a/server/src/main.rs
+++ b/server/src/main.rs
@@ -1,6 +1,7 @@
mod db;
mod errors;
mod logger;
+mod models;
use axum::{
http::{header, Request},
@@ -52,8 +53,9 @@ async fn create_app() -> Router {
}
// Example root which says hi
-async fn hej() -> Result<Json<Hej>, errors::AppError> {
- Ok(Json(Hej::new("hej verden".to_string())))
+async fn hej() -> Result<Json<Vec<models::users::User>>, errors::AppError> {
+ let users = models::users::User::list().await?;
+ Ok(Json(users))
}
#[derive(Debug, Serialize)]
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)
+ }
+}