use diesel::prelude::*; use crate::models; pub fn find_user_by_uid( uid: i32, conn: &SqliteConnection, ) -> Result, diesel::result::Error> { use crate::schema::users::dsl::*; let maybe_user = users.find(uid).first::(conn).optional(); // Ok(user) maybe_user } pub fn find_user_by_name( user_name: String, conn: &SqliteConnection, ) -> Result, diesel::result::Error> { use crate::schema::users::dsl::*; let maybe_user = users .filter(name.eq(user_name)) .first::(conn) .optional(); maybe_user } pub fn get_all( conn: &SqliteConnection, ) -> Result>, diesel::result::Error> { use crate::schema::users::dsl::*; users.load::(conn).optional() } /// Run query using Diesel to insert a new database row and return the result. pub fn insert_new_user( nu: &models::NewUser, conn: &SqliteConnection, ) -> Result { // It is common when using Diesel with Actix web to import schema-related // modules inside a function's scope (rather than the normal module's scope) // to prevent import collisions and namespace pollution. use crate::schema::users::dsl::*; // let new_user = models::User { // id: Uuid::new_v4().to_string(), // name: nu.name.to_string(), // }; // let x = users.load::(conn).optional(); // let target = users.find("4"); // let test_user = models::User { // id: "5".to_owned(), // name: "who".to_owned(), // }; // let update_result = diesel::update(target).set(&test_user).execute(conn); diesel::insert_into(users).values(nu).execute(conn)?; let user = users .filter(name.eq(nu.name.clone())) .first::(conn); user // Ok(nu.clone()) }