More refactoring
Some checks failed
Rust Build / Check (pull_request) Successful in 44s
Rust Build / Test Suite (pull_request) Successful in 56s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Failing after 53s
Rust Build / build (pull_request) Successful in 1m25s
Some checks failed
Rust Build / Check (pull_request) Successful in 44s
Rust Build / Test Suite (pull_request) Successful in 56s
Rust Build / Rustfmt (pull_request) Successful in 31s
Rust Build / Clippy (pull_request) Failing after 53s
Rust Build / build (pull_request) Successful in 1m25s
This commit is contained in:
@@ -50,16 +50,37 @@ pub async fn register_user(
|
|||||||
last_login: String::from("nil"),
|
last_login: String::from("nil"),
|
||||||
};
|
};
|
||||||
|
|
||||||
match repo::user::insert(&pool, &user).await {
|
match repo::user::exists(&pool, &user.username).await {
|
||||||
Ok(id) => {
|
Ok(res) => {
|
||||||
user.id = id;
|
if res {
|
||||||
(
|
return (
|
||||||
StatusCode::CREATED,
|
StatusCode::NOT_FOUND,
|
||||||
Json(response::Response {
|
Json(response::Response {
|
||||||
message: String::from("User inserted"),
|
message: String::from("Error"),
|
||||||
data: vec![user],
|
data: vec![user],
|
||||||
}),
|
}),
|
||||||
)
|
);
|
||||||
|
} else {
|
||||||
|
match repo::user::insert(&pool, &user).await {
|
||||||
|
Ok(id) => {
|
||||||
|
user.id = id;
|
||||||
|
(
|
||||||
|
StatusCode::CREATED,
|
||||||
|
Json(response::Response {
|
||||||
|
message: String::from("User inserted"),
|
||||||
|
data: vec![user],
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Err(err) => (
|
||||||
|
StatusCode::BAD_REQUEST,
|
||||||
|
Json(response::Response {
|
||||||
|
message: err.to_string(),
|
||||||
|
data: vec![user],
|
||||||
|
}),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Err(err) => (
|
Err(err) => (
|
||||||
StatusCode::BAD_REQUEST,
|
StatusCode::BAD_REQUEST,
|
||||||
|
@@ -7,6 +7,26 @@ pub mod user {
|
|||||||
pub date_created: Option<time::OffsetDateTime>,
|
pub date_created: Option<time::OffsetDateTime>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn exists(pool: &sqlx::PgPool, username: &String) -> Result<bool, sqlx::Error> {
|
||||||
|
let result = sqlx::query(
|
||||||
|
r#"
|
||||||
|
SELECT 1 FROM "user" WHERE username = $1
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.bind(&username)
|
||||||
|
.fetch_optional(pool)
|
||||||
|
.await;
|
||||||
|
|
||||||
|
match result {
|
||||||
|
Ok(r) => Ok(r.is_some()),
|
||||||
|
Err(e) => {
|
||||||
|
println!("Error: {:?}", e.to_string());
|
||||||
|
// Err(e)
|
||||||
|
Err(e)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub async fn insert(
|
pub async fn insert(
|
||||||
pool: &sqlx::PgPool,
|
pool: &sqlx::PgPool,
|
||||||
user: &icarus_models::user::User,
|
user: &icarus_models::user::User,
|
||||||
|
Reference in New Issue
Block a user