Genericize Feed::write_to_db
This commit is contained in:
parent
c74ebbe5fa
commit
6e815a94a0
12
src/feeds.rs
12
src/feeds.rs
@ -4,6 +4,7 @@ use rocket_db_pools::Connection;
|
||||
use sqlx::types::JsonValue;
|
||||
use url::Url;
|
||||
use uuid::Uuid;
|
||||
use sqlx::Executor;
|
||||
|
||||
use crate::feed_utils::fetch_feed;
|
||||
use crate::user::AuthenticatedUser;
|
||||
@ -35,7 +36,10 @@ impl Feed {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn write_to_database(&self, mut db: Connection<Db>) -> Result<(), sqlx::Error> {
|
||||
pub async fn write_to_database<'a, E>(&self, executor: E) -> sqlx::Result<()>
|
||||
where
|
||||
E: Executor<'a, Database = sqlx::Sqlite>
|
||||
{
|
||||
// Convert categorization to JSON value
|
||||
let categorization_json = serde::json::to_value(&self.categorization).map_err(|e| {
|
||||
eprintln!("Failed to serialize categorization: {}", e);
|
||||
@ -53,7 +57,7 @@ impl Feed {
|
||||
.bind(self.added_time.to_rfc3339())
|
||||
.bind(self.last_checked_time.to_rfc3339())
|
||||
.bind(categorization_json.to_string())
|
||||
.execute(&mut **db)
|
||||
.execute(executor)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
@ -70,7 +74,7 @@ pub struct NewFeed {
|
||||
|
||||
#[post("/feeds", data = "<new_feed>")]
|
||||
pub async fn create_feed(
|
||||
db: Connection<Db>,
|
||||
mut db: Connection<Db>,
|
||||
new_feed: Json<NewFeed>,
|
||||
user: AuthenticatedUser,
|
||||
) -> Result<Json<Feed>, Status> {
|
||||
@ -97,7 +101,7 @@ pub async fn create_feed(
|
||||
let mut feed = Feed::new(name, new_feed.url, user.user_id);
|
||||
feed.categorization = new_feed.categorization;
|
||||
|
||||
match feed.write_to_database(db).await {
|
||||
match feed.write_to_database(&mut **db).await {
|
||||
Ok(_) => Ok(Json(feed)),
|
||||
Err(e) => {
|
||||
eprintln!("Database error: {}", e);
|
||||
|
Loading…
Reference in New Issue
Block a user