use sqlx query macro everywhere

This commit is contained in:
Greg Shuflin 2025-02-05 03:01:39 -08:00
parent b597d179f0
commit df46571240
6 changed files with 93 additions and 42 deletions

View File

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "DELETE FROM feeds WHERE feed_id = ? AND user_id = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "8b041918410c3c12dc9faff2a36e908455ae0e5bbadebc606944908ee49c6c63"
}

View File

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?",
"describe": {
"columns": [],
"parameters": {
"Right": 2
},
"nullable": []
},
"hash": "a1da67f80f4bcaebc761aba3c6043f9ebe7fecd2351be8b0bd3e4bd34a70b107"
}

View File

@ -0,0 +1,12 @@
{
"db_name": "SQLite",
"query": "\n INSERT INTO feeds (feed_id, name, url, user_id, added_time, last_checked_time, categorization) \n VALUES (?, ?, ?, ?, ?, ?, json(?))\n ",
"describe": {
"columns": [],
"parameters": {
"Right": 7
},
"nullable": []
},
"hash": "c23d98cd26e2aa0637978f71455bcf0f8384572858e9649704596ba63f2a8b96"
}

View File

@ -47,17 +47,26 @@ impl Feed {
sqlx::Error::Decode(Box::new(e))
})?;
sqlx::query(
"INSERT INTO feeds (feed_id, name, url, user_id, added_time, last_checked_time, categorization)
VALUES (?1, ?2, ?3, ?4, ?5, ?6, json(?7))",
let feed_id_str = self.feed_id.to_string();
let user_id_str = self.user_id.to_string();
let added_time_str = self.added_time.to_rfc3339();
let last_checked_time_str = self.last_checked_time.to_rfc3339();
let url_str = self.url.as_str();
let categorization_str = categorization_json.to_string();
sqlx::query!(
r#"
INSERT INTO feeds (feed_id, name, url, user_id, added_time, last_checked_time, categorization)
VALUES (?, ?, ?, ?, ?, ?, json(?))
"#,
feed_id_str,
self.name,
url_str,
user_id_str,
added_time_str,
last_checked_time_str,
categorization_str
)
.bind(self.feed_id.to_string())
.bind(&self.name)
.bind(self.url.as_str())
.bind(self.user_id.to_string())
.bind(self.added_time.to_rfc3339())
.bind(self.last_checked_time.to_rfc3339())
.bind(categorization_json.to_string())
.execute(executor)
.await?;
@ -193,13 +202,18 @@ pub async fn delete_feed(mut db: Connection<Db>, feed_id: &str, user: Authentica
Err(_) => return Status::BadRequest,
};
let query = sqlx::query("DELETE FROM feeds WHERE feed_id = ? AND user_id = ?")
.bind(feed_uuid.to_string())
.bind(user.user_id.to_string())
.execute(&mut **db)
.await;
let feed_id_str = feed_uuid.to_string();
let user_id_str = user.user_id.to_string();
match query {
let result = sqlx::query!(
"DELETE FROM feeds WHERE feed_id = ? AND user_id = ?",
feed_id_str,
user_id_str
)
.execute(&mut **db)
.await;
match result {
Ok(result) => {
if result.rows_affected() > 0 {
Status::NoContent

View File

@ -57,15 +57,17 @@ async fn update_entry_db(
let now = Utc::now().to_rfc3339();
// Update the feed's last_checked_time
sqlx::query("UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?")
.bind(&now)
.bind(feed_id)
.execute(&mut *tx)
.await
.map_err(|e| {
error!("Failed to update feed last_checked_time: {}", e);
Status::InternalServerError
})?;
sqlx::query!(
"UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?",
now,
feed_id
)
.execute(&mut *tx)
.await
.map_err(|e| {
error!("Failed to update feed last_checked_time: {}", e);
Status::InternalServerError
})?;
for entry in entries {
let content_json = if let Some(content) = &entry.content {
@ -163,7 +165,8 @@ async fn read_entries(feed_id: &str, db: &mut SqliteConnection) -> Result<Vec<En
Ok(Entry {
id: row.id.clone(),
local_id: Uuid::parse_str(&row.local_id).map_err(|_| Status::InternalServerError)?,
local_id: Uuid::parse_str(&row.local_id)
.map_err(|_| Status::InternalServerError)?,
title: row.title.clone(),
published: row.published.flatten(),
updated: row.updated.flatten(),

View File

@ -177,12 +177,9 @@ pub async fn delete_user(mut db: Connection<Db>, user_id: &str) -> Status {
};
let id_str = uuid.to_string();
let result = sqlx::query!(
"DELETE FROM users WHERE id = ?",
id_str
)
.execute(&mut **db)
.await;
let result = sqlx::query!("DELETE FROM users WHERE id = ?", id_str)
.execute(&mut **db)
.await;
match result {
Ok(result) => {
@ -353,11 +350,12 @@ pub async fn setup(
let new_user = new_user.into_inner();
// Hash the password - we'll use bcrypt
let password_hash = bcrypt::hash(new_user.password.as_bytes(), bcrypt::DEFAULT_COST).map_err(|_| {
Json(SetupError {
error: "Failed to hash password".to_string(),
})
})?;
let password_hash =
bcrypt::hash(new_user.password.as_bytes(), bcrypt::DEFAULT_COST).map_err(|_| {
Json(SetupError {
error: "Failed to hash password".to_string(),
})
})?;
let mut user = User::new(
new_user.username,
@ -367,11 +365,11 @@ pub async fn setup(
);
user.admin = true;
user.write_to_database(&mut **db)
.await
.map_err(|_| Json(SetupError {
user.write_to_database(&mut **db).await.map_err(|_| {
Json(SetupError {
error: "Failed to create user".to_string(),
}))?;
})
})?;
Ok(Status::Created)
}