Skip to content

Commit

Permalink
fix error
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn committed Jan 21, 2025
1 parent f1c7d44 commit 55537b1
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 21 deletions.
18 changes: 3 additions & 15 deletions templates/classic/_base/src/main.rs.liquid
Original file line number Diff line number Diff line change
@@ -1,15 +1,3 @@
{%- if db_lib == "diesel" %}
{%- if db_type == "sqlite" %}
{%- else %}
{%- endif %}
{%- endif %}
{%- if db_lib == "sqlx" %}
{%- if db_type == "sqlite" %}
use sqlx::SqlitePool;
{%- elsif db_type == "postgres" %}
use sqlx::PgPool;
{%- endif %}
{%- endif %}
use dotenvy::dotenv;
use salvo::catcher::Catcher;
use salvo::conn::rustls::{Keycert, RustlsConfig};
Expand Down Expand Up @@ -55,10 +43,10 @@ async fn main() {

crate::config::init();
let config = crate::config::get();
{%- if db_lib == "rbatis" or db_lib == "seaorm" or db_lib == "mongodb" %}
crate::db::init(&config.db).await;
{%- else %}
{%- if db_lib == "diesel" %}
crate::db::init(&config.db);
{%- else %}
crate::db::init(&config.db).await;
{%- endif %}

let _guard = config.log.guard();
Expand Down
4 changes: 2 additions & 2 deletions templates/classic/seaorm/src/routers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub struct CreateInData {
pub async fn create_user(idata: JsonBody<CreateInData>) -> JsonResult<SafeUser> {
let CreateInData { username, password } = idata.into_inner();
let id = Ulid::new().to_string();
let password = utils::hash_password(&password).await?;
let password = utils::hash_password(&password)?;
let conn = db::pool();
let model = users::ActiveModel {
id: Set(id.clone()),
Expand Down Expand Up @@ -78,7 +78,7 @@ pub async fn update_user(
};
let mut user: users::ActiveModel = user.into();
user.username = Set(username.to_owned());
user.password = Set(utils::hash_password(&password).await?);
user.password = Set(utils::hash_password(&password)?);

let user: users::Model = user.update(conn).await?;
json_ok(SafeUser {
Expand Down
12 changes: 9 additions & 3 deletions templates/classic/sqlx/src/db/mod.rs.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@ use std::sync::OnceLock;
{%- if db_type == "sqlite" %}
use sqlx::SqlitePool;

use crate::config::DbConfig;

pub static SQLX_POOL: OnceLock<SqlitePool> = OnceLock::new();

pub fn init(config: &DbConfig) {
pub async fn init(config: &DbConfig) {
let sqlx_pool = SqlitePool::connect(&config.url).await
.expect("Database connection failed.");
crate::db::SQLX_POOL
Expand All @@ -19,9 +21,11 @@ pub fn pool() -> &'static SqlitePool {
{%- elsif db_type == "postgres" %}
use sqlx::postgres::PgPool;

use crate::config::DbConfig;

pub static SQLX_POOL: OnceLock<PgPool> = OnceLock::new();

pub fn init(config: &DbConfig) {
pub async fn init(config: &DbConfig) {
let sqlx_pool = PgPool::connect(&config.url).await
.expect("Database connection failed.");
crate::db::SQLX_POOL
Expand All @@ -35,9 +39,11 @@ pub fn pool() -> &'static PgPool {
{%- elsif db_type == "mysql" %}
use sqlx::mysql::MySqlPool;

use crate::config::DbConfig;

pub static SQLX_POOL: OnceLock<MySqlPool> = OnceLock::new();

pub fn init(config: &DbConfig) {
pub async fn init(config: &DbConfig) {
let sqlx_pool = SqlitePool::connect(&config.url).await
.expect("Database connection failed.");
crate::db::SQLX_POOL
Expand Down
2 changes: 1 addition & 1 deletion templates/classic/sqlx/src/routers/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct CreateInData {
pub async fn create_user(idata: JsonBody<CreateInData>) -> JsonResult<SafeUser> {
let CreateInData { username, password } = idata.into_inner();
let id = Ulid::new().to_string();
let password = utils::hash_password(&password).await?;
let password = utils::hash_password(&password)?;
let conn = db::pool();
let _ = sqlx::query!(
r#"
Expand Down

0 comments on commit 55537b1

Please sign in to comment.