Refactor schema migrations, optimize queries, and improve data fetching logic

- Simplified `run_dev_queries` by delegating schema updates to `run_migrations` for consistency.
- Added universal `fetch_all_*_by_book` methods for streamlined data retrieval across repositories.
- Replaced nested query logic with batch fetching to improve performance and maintainability.
- Optimized HTML-to-text conversion by consolidating regex patterns with `LazyLock`.
This commit is contained in:
natreex
2026-03-24 23:14:33 -04:00
parent 25c7f25a0e
commit b9606e899a
8 changed files with 175 additions and 283 deletions

View File

@@ -442,6 +442,24 @@ pub fn fetch_book_characters_attributes(conn: &Connection, user_id: &str, charac
Ok(attributes)
}
pub fn fetch_all_character_attributes_by_book(conn: &Connection, user_id: &str, book_id: &str, lang: Lang) -> AppResult<Vec<BookCharactersAttributesTable>> {
let mut statement = conn
.prepare("SELECT bca.attr_id, bca.character_id, bca.user_id, bca.attribute_name, bca.attribute_value, bca.last_update FROM book_characters_attributes bca INNER JOIN book_characters bc ON bca.character_id = bc.character_id WHERE bca.user_id=?1 AND bc.book_id=?2")
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?;
let attributes = statement
.query_map(params![user_id, book_id], |query_row| {
Ok(BookCharactersAttributesTable {
attr_id: query_row.get(0)?, character_id: query_row.get(1)?,
user_id: query_row.get(2)?, attribute_name: query_row.get(3)?,
attribute_value: query_row.get(4)?, last_update: query_row.get(5)?,
})
})
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?
.collect::<Result<Vec<_>, _>>()
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les attributs des personnages.".to_string() } else { "Unable to retrieve character attributes.".to_string() }))?;
Ok(attributes)
}
/// Fetches all synced characters for a user.
/// * `conn` - Database connection
/// * `user_id` - The unique identifier of the user