From 5b32f1eb39f14457a11701095218524fc50f5a73 Mon Sep 17 00:00:00 2001 From: Santo Cariotti Date: Mon, 22 Aug 2022 17:13:58 +0200 Subject: Move models --- server/src/main.rs | 22 +--------------------- server/src/models/mod.rs | 2 +- server/src/models/user.rs | 39 +++++++++++++++++++++++++++++++++++++++ server/src/models/users.rs | 39 --------------------------------------- 4 files changed, 41 insertions(+), 61 deletions(-) create mode 100644 server/src/models/user.rs delete mode 100644 server/src/models/users.rs (limited to 'server/src') diff --git a/server/src/main.rs b/server/src/main.rs index 8319fb2..a61e44b 100644 --- a/server/src/main.rs +++ b/server/src/main.rs @@ -5,10 +5,8 @@ mod models; use axum::{ http::{header, Request}, - routing::get, - Json, Router, + Router, }; -use serde::Serialize; use std::time::Duration; use tower_http::sensitive_headers::SetSensitiveHeadersLayer; use tower_http::{classify::ServerErrorsFailureClass, trace::TraceLayer}; @@ -33,7 +31,6 @@ async fn create_app() -> Router { let _ = db::setup().await; Router::new() - .route("/", get(hej)) // Mark the `Authorization` request header as sensitive so it doesn't // show in logs. .layer(SetSensitiveHeadersLayer::new(std::iter::once( @@ -51,20 +48,3 @@ async fn create_app() -> Router { ), ) } - -// Example root which says hi -async fn hej() -> Result>, errors::AppError> { - let users = models::users::User::list().await?; - Ok(Json(users)) -} - -#[derive(Debug, Serialize)] -struct Hej { - hello: String, -} - -impl Hej { - fn new(hello: String) -> Self { - Self { hello } - } -} diff --git a/server/src/models/mod.rs b/server/src/models/mod.rs index 913bd46..22d12a3 100644 --- a/server/src/models/mod.rs +++ b/server/src/models/mod.rs @@ -1 +1 @@ -pub mod users; +pub mod user; diff --git a/server/src/models/user.rs b/server/src/models/user.rs new file mode 100644 index 0000000..d7a836f --- /dev/null +++ b/server/src/models/user.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) + } +} diff --git a/server/src/models/users.rs b/server/src/models/users.rs deleted file mode 100644 index d7a836f..0000000 --- a/server/src/models/users.rs +++ /dev/null @@ -1,39 +0,0 @@ -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