use sqlx query macro everywhere
This commit is contained in:
parent
b597d179f0
commit
df46571240
12
.sqlx/query-8b041918410c3c12dc9faff2a36e908455ae0e5bbadebc606944908ee49c6c63.json
generated
Normal file
12
.sqlx/query-8b041918410c3c12dc9faff2a36e908455ae0e5bbadebc606944908ee49c6c63.json
generated
Normal 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"
|
||||||
|
}
|
12
.sqlx/query-a1da67f80f4bcaebc761aba3c6043f9ebe7fecd2351be8b0bd3e4bd34a70b107.json
generated
Normal file
12
.sqlx/query-a1da67f80f4bcaebc761aba3c6043f9ebe7fecd2351be8b0bd3e4bd34a70b107.json
generated
Normal 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"
|
||||||
|
}
|
12
.sqlx/query-c23d98cd26e2aa0637978f71455bcf0f8384572858e9649704596ba63f2a8b96.json
generated
Normal file
12
.sqlx/query-c23d98cd26e2aa0637978f71455bcf0f8384572858e9649704596ba63f2a8b96.json
generated
Normal 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"
|
||||||
|
}
|
42
src/feeds.rs
42
src/feeds.rs
@ -47,17 +47,26 @@ impl Feed {
|
|||||||
sqlx::Error::Decode(Box::new(e))
|
sqlx::Error::Decode(Box::new(e))
|
||||||
})?;
|
})?;
|
||||||
|
|
||||||
sqlx::query(
|
let feed_id_str = self.feed_id.to_string();
|
||||||
"INSERT INTO feeds (feed_id, name, url, user_id, added_time, last_checked_time, categorization)
|
let user_id_str = self.user_id.to_string();
|
||||||
VALUES (?1, ?2, ?3, ?4, ?5, ?6, json(?7))",
|
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)
|
.execute(executor)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
@ -193,13 +202,18 @@ pub async fn delete_feed(mut db: Connection<Db>, feed_id: &str, user: Authentica
|
|||||||
Err(_) => return Status::BadRequest,
|
Err(_) => return Status::BadRequest,
|
||||||
};
|
};
|
||||||
|
|
||||||
let query = sqlx::query("DELETE FROM feeds WHERE feed_id = ? AND user_id = ?")
|
let feed_id_str = feed_uuid.to_string();
|
||||||
.bind(feed_uuid.to_string())
|
let user_id_str = user.user_id.to_string();
|
||||||
.bind(user.user_id.to_string())
|
|
||||||
|
let result = sqlx::query!(
|
||||||
|
"DELETE FROM feeds WHERE feed_id = ? AND user_id = ?",
|
||||||
|
feed_id_str,
|
||||||
|
user_id_str
|
||||||
|
)
|
||||||
.execute(&mut **db)
|
.execute(&mut **db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
match query {
|
match result {
|
||||||
Ok(result) => {
|
Ok(result) => {
|
||||||
if result.rows_affected() > 0 {
|
if result.rows_affected() > 0 {
|
||||||
Status::NoContent
|
Status::NoContent
|
||||||
|
11
src/poll.rs
11
src/poll.rs
@ -57,9 +57,11 @@ async fn update_entry_db(
|
|||||||
let now = Utc::now().to_rfc3339();
|
let now = Utc::now().to_rfc3339();
|
||||||
|
|
||||||
// Update the feed's last_checked_time
|
// Update the feed's last_checked_time
|
||||||
sqlx::query("UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?")
|
sqlx::query!(
|
||||||
.bind(&now)
|
"UPDATE feeds SET last_checked_time = ? WHERE feed_id = ?",
|
||||||
.bind(feed_id)
|
now,
|
||||||
|
feed_id
|
||||||
|
)
|
||||||
.execute(&mut *tx)
|
.execute(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
@ -163,7 +165,8 @@ async fn read_entries(feed_id: &str, db: &mut SqliteConnection) -> Result<Vec<En
|
|||||||
|
|
||||||
Ok(Entry {
|
Ok(Entry {
|
||||||
id: row.id.clone(),
|
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(),
|
title: row.title.clone(),
|
||||||
published: row.published.flatten(),
|
published: row.published.flatten(),
|
||||||
updated: row.updated.flatten(),
|
updated: row.updated.flatten(),
|
||||||
|
16
src/user.rs
16
src/user.rs
@ -177,10 +177,7 @@ pub async fn delete_user(mut db: Connection<Db>, user_id: &str) -> Status {
|
|||||||
};
|
};
|
||||||
|
|
||||||
let id_str = uuid.to_string();
|
let id_str = uuid.to_string();
|
||||||
let result = sqlx::query!(
|
let result = sqlx::query!("DELETE FROM users WHERE id = ?", id_str)
|
||||||
"DELETE FROM users WHERE id = ?",
|
|
||||||
id_str
|
|
||||||
)
|
|
||||||
.execute(&mut **db)
|
.execute(&mut **db)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
@ -353,7 +350,8 @@ pub async fn setup(
|
|||||||
let new_user = new_user.into_inner();
|
let new_user = new_user.into_inner();
|
||||||
|
|
||||||
// Hash the password - we'll use bcrypt
|
// Hash the password - we'll use bcrypt
|
||||||
let password_hash = bcrypt::hash(new_user.password.as_bytes(), bcrypt::DEFAULT_COST).map_err(|_| {
|
let password_hash =
|
||||||
|
bcrypt::hash(new_user.password.as_bytes(), bcrypt::DEFAULT_COST).map_err(|_| {
|
||||||
Json(SetupError {
|
Json(SetupError {
|
||||||
error: "Failed to hash password".to_string(),
|
error: "Failed to hash password".to_string(),
|
||||||
})
|
})
|
||||||
@ -367,11 +365,11 @@ pub async fn setup(
|
|||||||
);
|
);
|
||||||
user.admin = true;
|
user.admin = true;
|
||||||
|
|
||||||
user.write_to_database(&mut **db)
|
user.write_to_database(&mut **db).await.map_err(|_| {
|
||||||
.await
|
Json(SetupError {
|
||||||
.map_err(|_| Json(SetupError {
|
|
||||||
error: "Failed to create user".to_string(),
|
error: "Failed to create user".to_string(),
|
||||||
}))?;
|
})
|
||||||
|
})?;
|
||||||
|
|
||||||
Ok(Status::Created)
|
Ok(Status::Created)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user