From c1126d7bc103baca01eb19e622a67c31d061aebb Mon Sep 17 00:00:00 2001 From: Lev Kokotov Date: Wed, 4 Dec 2024 11:42:23 -0800 Subject: [PATCH] save --- examples/users/src/controllers.rs | 5 +---- examples/users/src/models.rs | 8 ++------ rwf/src/http/request.rs | 11 +++++++++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/users/src/controllers.rs b/examples/users/src/controllers.rs index dfff9293..d864b705 100644 --- a/examples/users/src/controllers.rs +++ b/examples/users/src/controllers.rs @@ -13,10 +13,7 @@ pub struct Signup; #[async_trait] impl PageController for Signup { async fn get(&self, request: &Request) -> Result { - let user = { - let mut conn = Pool::connection().await?; - request.user::(&mut conn).await? - }; + let user = request.user::(Pool::pool()).await?; if let Some(_) = user { return Ok(Response::new().redirect("/profile")); diff --git a/examples/users/src/models.rs b/examples/users/src/models.rs index 72280869..58f58a95 100644 --- a/examples/users/src/models.rs +++ b/examples/users/src/models.rs @@ -27,13 +27,11 @@ impl User { _ => (), } - let mut conn = Pool::connection().await?; - let user = User::create(&[ ("email", email.to_value()), ("password", encrypted_password.to_value()), ]) - .fetch(&mut conn) + .fetch(Pool::pool()) .await?; Ok(UserLogin::Ok(user)) @@ -44,10 +42,8 @@ impl User { /// Return a user if one exists and the passwords match. /// Return `None` otherwise. pub async fn login(email: &str, password: &str) -> Result { - let mut conn = Pool::connection().await?; - if let Some(user) = User::filter("email", email) - .fetch_optional(&mut conn) + .fetch_optional(Pool::pool()) .await? { if hash_validate(password.as_bytes(), &user.password)? { diff --git a/rwf/src/http/request.rs b/rwf/src/http/request.rs index eb2f10f1..271667e7 100644 --- a/rwf/src/http/request.rs +++ b/rwf/src/http/request.rs @@ -11,6 +11,7 @@ use time::OffsetDateTime; use tokio::io::{AsyncRead, AsyncReadExt}; use super::{Cookies, Error, FormData, FromFormData, Head, Params, Response, ToParameter}; +use crate::prelude::ToConnectionRequest; use crate::{ config::get_config, controller::{Session, SessionId}, @@ -265,7 +266,10 @@ impl Request { /// let conn = Pool::connection().await?; /// let user = request.user::(&mut conn).await?; /// ``` - pub async fn user(&self, conn: &mut ConnectionGuard) -> Result, Error> { + pub async fn user( + &self, + conn: impl ToConnectionRequest<'_>, + ) -> Result, Error> { match self.session_id() { SessionId::Authenticated(user_id) => Ok(Some(T::find(user_id).fetch(conn).await?)), @@ -275,7 +279,10 @@ impl Request { /// Same function as [`Request::user`], except if returns a [`Result`] instead of an [`Option`]. /// If used with the `?` operator, returns `403 - Unauthorized` automatically. - pub async fn user_required(&self, conn: &mut ConnectionGuard) -> Result { + pub async fn user_required( + &self, + conn: impl ToConnectionRequest<'_>, + ) -> Result { match self.user(conn).await? { Some(user) => Ok(user), None => Err(Error::Forbidden),