Refactor and clean up
- Removed unused services (`cover`, `character`, `series_location` modules) and legacy methods to reduce code clutter. - Consolidated `IncidentProps` and `PlotPointProps` into `incident.service` and `plotpoint.service` modules, replacing duplicated definitions. - Updated references across modules (`act`, `incident.service`, `character`) for consistency. - Improved serialization with `serde` for `IncidentProps`. - Enhanced `get_incitents_incidents` to include optional chapters.
This commit is contained in:
@@ -82,30 +82,6 @@ pub struct AttributeResult {
|
||||
pub attribute_value: String,
|
||||
}
|
||||
|
||||
pub struct CompleteCharacterResult {
|
||||
pub character_id: String,
|
||||
pub first_name: String,
|
||||
pub last_name: String,
|
||||
pub nickname: Option<String>,
|
||||
pub age: Option<String>,
|
||||
pub gender: Option<String>,
|
||||
pub species: Option<String>,
|
||||
pub nationality: Option<String>,
|
||||
pub status: Option<String>,
|
||||
pub category: String,
|
||||
pub title: String,
|
||||
pub role: String,
|
||||
pub biography: String,
|
||||
pub history: String,
|
||||
pub speech_pattern: Option<String>,
|
||||
pub catchphrase: Option<String>,
|
||||
pub residence: Option<String>,
|
||||
pub notes: Option<String>,
|
||||
pub color: Option<String>,
|
||||
pub attribute_name: String,
|
||||
pub attribute_value: String,
|
||||
}
|
||||
|
||||
pub struct CharacterData {
|
||||
pub first_name: String,
|
||||
pub last_name: Option<String>,
|
||||
@@ -346,54 +322,6 @@ pub fn fetch_attributes(conn: &Connection, character_id: &str, user_id: &str, la
|
||||
/// * `book_id` - The unique identifier of the book
|
||||
/// * `tags` - An optional array of character IDs to filter by
|
||||
/// * `lang` - The language for error messages ("fr" or "en")
|
||||
/// Returns an array of complete character results with attributes.
|
||||
pub fn fetch_complete_characters(conn: &Connection, user_id: &str, book_id: &str, tags: &[String], lang: Lang) -> AppResult<Vec<CompleteCharacterResult>> {
|
||||
let mut query = "SELECT charac.character_id, first_name, last_name, nickname, age, gender, species, nationality, status, category, title, role, biography, history, speech_pattern, catchphrase, residence, notes, color, attribute_name, attribute_value FROM book_characters AS charac LEFT JOIN book_characters_attributes AS attr ON charac.character_id=attr.character_id WHERE charac.user_id=?1 AND charac.book_id=?2".to_string();
|
||||
let mut param_values: Vec<Box<dyn rusqlite::types::ToSql>> = Vec::new();
|
||||
param_values.push(Box::new(user_id.to_string()));
|
||||
param_values.push(Box::new(book_id.to_string()));
|
||||
|
||||
if !tags.is_empty() {
|
||||
let placeholders: String = tags.iter().enumerate().map(|(index, _)| format!("?{}", index + 3)).collect::<Vec<_>>().join(",");
|
||||
query += &format!(" AND charac.character_id IN ({})", placeholders);
|
||||
for tag in tags {
|
||||
param_values.push(Box::new(tag.clone()));
|
||||
}
|
||||
}
|
||||
|
||||
let param_refs: Vec<&dyn rusqlite::types::ToSql> = param_values.iter().map(|param| param.as_ref()).collect();
|
||||
|
||||
let mut statement = conn
|
||||
.prepare(&query)
|
||||
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les personnages complets.".to_string() } else { "Unable to retrieve complete characters.".to_string() }))?;
|
||||
|
||||
let characters = statement
|
||||
.query_map(param_refs.as_slice(), |query_row| {
|
||||
Ok(CompleteCharacterResult {
|
||||
character_id: query_row.get(0)?, first_name: query_row.get(1)?,
|
||||
last_name: query_row.get(2)?, nickname: query_row.get(3)?,
|
||||
age: query_row.get(4)?, gender: query_row.get(5)?,
|
||||
species: query_row.get(6)?, nationality: query_row.get(7)?,
|
||||
status: query_row.get(8)?, category: query_row.get(9)?,
|
||||
title: query_row.get(10)?, role: query_row.get(11)?,
|
||||
biography: query_row.get(12)?, history: query_row.get(13)?,
|
||||
speech_pattern: query_row.get(14)?, catchphrase: query_row.get(15)?,
|
||||
residence: query_row.get(16)?, notes: query_row.get(17)?,
|
||||
color: query_row.get(18)?, attribute_name: query_row.get(19)?,
|
||||
attribute_value: query_row.get(20)?,
|
||||
})
|
||||
})
|
||||
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les personnages complets.".to_string() } else { "Unable to retrieve complete characters.".to_string() }))?
|
||||
.collect::<Result<Vec<_>, _>>()
|
||||
.map_err(|_| AppError::Internal(if lang == Lang::Fr { "Impossible de récupérer les personnages complets.".to_string() } else { "Unable to retrieve complete characters.".to_string() }))?;
|
||||
|
||||
if characters.is_empty() {
|
||||
return Err(AppError::NotFound(if lang == Lang::Fr { "Aucun personnage complet trouvé.".to_string() } else { "No complete characters found.".to_string() }));
|
||||
}
|
||||
|
||||
Ok(characters)
|
||||
}
|
||||
|
||||
/// Updates an existing character attribute.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_id` - The unique identifier of the user
|
||||
|
||||
@@ -94,47 +94,6 @@ pub struct CharacterListResponse {
|
||||
pub enabled: bool,
|
||||
}
|
||||
|
||||
pub struct CompleteCharacterProps {
|
||||
pub id: Option<String>,
|
||||
pub name: String,
|
||||
pub last_name: String,
|
||||
pub nickname: String,
|
||||
pub age: Option<i64>,
|
||||
pub gender: String,
|
||||
pub species: String,
|
||||
pub nationality: String,
|
||||
pub status: String,
|
||||
pub title: String,
|
||||
pub category: String,
|
||||
pub image: Option<String>,
|
||||
pub role: String,
|
||||
pub biography: String,
|
||||
pub history: String,
|
||||
pub speech_pattern: String,
|
||||
pub catchphrase: String,
|
||||
pub residence: String,
|
||||
pub notes: String,
|
||||
pub color: String,
|
||||
pub physical: Vec<Attribute>,
|
||||
pub psychological: Vec<Attribute>,
|
||||
pub relations: Vec<Attribute>,
|
||||
pub skills: Vec<Attribute>,
|
||||
pub weaknesses: Vec<Attribute>,
|
||||
pub strengths: Vec<Attribute>,
|
||||
pub goals: Vec<Attribute>,
|
||||
pub motivations: Vec<Attribute>,
|
||||
pub arc: Vec<Attribute>,
|
||||
pub secrets: Vec<Attribute>,
|
||||
pub fears: Vec<Attribute>,
|
||||
pub flaws: Vec<Attribute>,
|
||||
pub beliefs: Vec<Attribute>,
|
||||
pub conflicts: Vec<Attribute>,
|
||||
pub quotes: Vec<Attribute>,
|
||||
pub distinguishing_marks: Vec<Attribute>,
|
||||
pub items: Vec<Attribute>,
|
||||
pub affiliations: Vec<Attribute>,
|
||||
}
|
||||
|
||||
#[derive(Serialize)]
|
||||
pub struct Attribute {
|
||||
pub id: String,
|
||||
@@ -410,247 +369,3 @@ pub fn get_attributes(conn: &Connection, character_id: &str, user_id: &str, lang
|
||||
Ok(character_attributes)
|
||||
}
|
||||
|
||||
/// Retrieves complete character data including all attributes for multiple characters.
|
||||
/// Used for exporting or displaying full character profiles.
|
||||
/// * `conn` - Database connection
|
||||
/// * `user_id` - The unique identifier of the user
|
||||
/// * `book_id` - The unique identifier of the book
|
||||
/// * `characters` - An array of character IDs to retrieve
|
||||
/// * `lang` - The language code for localization
|
||||
/// Returns an array of complete character objects with all their attributes.
|
||||
pub fn get_complete_character_list(conn: &Connection, user_id: &str, book_id: &str, characters: &[String], lang: Lang) -> AppResult<Vec<CompleteCharacterProps>> {
|
||||
let encrypted_character_list: Vec<repo::CompleteCharacterResult> = match repo::fetch_complete_characters(conn, user_id, book_id, characters, lang) {
|
||||
Ok(result) => result,
|
||||
Err(_) => return Ok(vec![]),
|
||||
};
|
||||
|
||||
if encrypted_character_list.is_empty() {
|
||||
return Ok(vec![]);
|
||||
}
|
||||
|
||||
let user_key: String = get_user_encryption_key(user_id)?;
|
||||
let mut complete_characters_map: HashMap<String, CompleteCharacterProps> = HashMap::new();
|
||||
|
||||
for encrypted_character in &encrypted_character_list {
|
||||
if encrypted_character.character_id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
if !complete_characters_map.contains_key(&encrypted_character.character_id) {
|
||||
let decrypted_character = CompleteCharacterProps {
|
||||
id: None,
|
||||
name: if encrypted_character.first_name.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.first_name, &user_key)? },
|
||||
last_name: if encrypted_character.last_name.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.last_name, &user_key)? },
|
||||
nickname: if let Some(ref value) = encrypted_character.nickname { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
age: if let Some(ref value) = encrypted_character.age { Some(decrypt_data_with_user_key(value, &user_key)?.parse::<i64>().unwrap_or(0)) } else { None },
|
||||
gender: if let Some(ref value) = encrypted_character.gender { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
species: if let Some(ref value) = encrypted_character.species { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
nationality: if let Some(ref value) = encrypted_character.nationality { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
status: if let Some(ref value) = encrypted_character.status { decrypt_data_with_user_key(value, &user_key)? } else { "alive".to_string() },
|
||||
title: if encrypted_character.title.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.title, &user_key)? },
|
||||
category: if encrypted_character.category.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.category, &user_key)? },
|
||||
image: None,
|
||||
role: if encrypted_character.role.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.role, &user_key)? },
|
||||
biography: if encrypted_character.biography.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.biography, &user_key)? },
|
||||
history: if encrypted_character.history.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.history, &user_key)? },
|
||||
speech_pattern: if let Some(ref value) = encrypted_character.speech_pattern { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
catchphrase: if let Some(ref value) = encrypted_character.catchphrase { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
residence: if let Some(ref value) = encrypted_character.residence { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
notes: if let Some(ref value) = encrypted_character.notes { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
color: if let Some(ref value) = encrypted_character.color { decrypt_data_with_user_key(value, &user_key)? } else { String::new() },
|
||||
physical: vec![],
|
||||
psychological: vec![],
|
||||
relations: vec![],
|
||||
skills: vec![],
|
||||
weaknesses: vec![],
|
||||
strengths: vec![],
|
||||
goals: vec![],
|
||||
motivations: vec![],
|
||||
arc: vec![],
|
||||
secrets: vec![],
|
||||
fears: vec![],
|
||||
flaws: vec![],
|
||||
beliefs: vec![],
|
||||
conflicts: vec![],
|
||||
quotes: vec![],
|
||||
distinguishing_marks: vec![],
|
||||
items: vec![],
|
||||
affiliations: vec![],
|
||||
};
|
||||
complete_characters_map.insert(encrypted_character.character_id.clone(), decrypted_character);
|
||||
}
|
||||
|
||||
let character_entry: &mut CompleteCharacterProps = match complete_characters_map.get_mut(&encrypted_character.character_id) {
|
||||
Some(entry) => entry,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
if encrypted_character.attribute_name.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let decrypted_attribute_name: String = decrypt_data_with_user_key(&encrypted_character.attribute_name, &user_key)?;
|
||||
let decrypted_attribute_value: String = if encrypted_character.attribute_value.is_empty() { String::new() } else { decrypt_data_with_user_key(&encrypted_character.attribute_value, &user_key)? };
|
||||
|
||||
let attribute = Attribute { id: String::new(), name: decrypted_attribute_value };
|
||||
|
||||
match decrypted_attribute_name.as_str() {
|
||||
"physical" => character_entry.physical.push(attribute),
|
||||
"psychological" => character_entry.psychological.push(attribute),
|
||||
"relations" => character_entry.relations.push(attribute),
|
||||
"skills" => character_entry.skills.push(attribute),
|
||||
"weaknesses" => character_entry.weaknesses.push(attribute),
|
||||
"strengths" => character_entry.strengths.push(attribute),
|
||||
"goals" => character_entry.goals.push(attribute),
|
||||
"motivations" => character_entry.motivations.push(attribute),
|
||||
"arc" => character_entry.arc.push(attribute),
|
||||
"secrets" => character_entry.secrets.push(attribute),
|
||||
"fears" => character_entry.fears.push(attribute),
|
||||
"flaws" => character_entry.flaws.push(attribute),
|
||||
"beliefs" => character_entry.beliefs.push(attribute),
|
||||
"conflicts" => character_entry.conflicts.push(attribute),
|
||||
"quotes" => character_entry.quotes.push(attribute),
|
||||
"distinguishingMarks" => character_entry.distinguishing_marks.push(attribute),
|
||||
"items" => character_entry.items.push(attribute),
|
||||
"affiliations" => character_entry.affiliations.push(attribute),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(complete_characters_map.into_values().collect())
|
||||
}
|
||||
|
||||
/// Generates a formatted vCard-style string representation of characters.
|
||||
/// Useful for AI context or text-based exports.
|
||||
/// * `characters` - An array of complete character objects to format
|
||||
/// Returns a formatted string containing all character information.
|
||||
pub fn character_v_card(characters: &[CompleteCharacterProps]) -> String {
|
||||
let mut unique_characters_map: HashMap<String, CompleteCharacterProps> = HashMap::new();
|
||||
|
||||
for character in characters {
|
||||
let character_identifier: String = if !character.name.is_empty() {
|
||||
character.name.clone()
|
||||
} else if let Some(ref id) = character.id {
|
||||
id.clone()
|
||||
} else {
|
||||
"unknown".to_string()
|
||||
};
|
||||
|
||||
if !unique_characters_map.contains_key(&character_identifier) {
|
||||
unique_characters_map.insert(character_identifier.clone(), CompleteCharacterProps {
|
||||
id: None,
|
||||
name: character.name.clone(),
|
||||
last_name: character.last_name.clone(),
|
||||
nickname: character.nickname.clone(),
|
||||
age: character.age,
|
||||
gender: character.gender.clone(),
|
||||
species: character.species.clone(),
|
||||
nationality: character.nationality.clone(),
|
||||
status: character.status.clone(),
|
||||
title: character.title.clone(),
|
||||
category: character.category.clone(),
|
||||
image: None,
|
||||
role: character.role.clone(),
|
||||
biography: character.biography.clone(),
|
||||
history: character.history.clone(),
|
||||
speech_pattern: character.speech_pattern.clone(),
|
||||
catchphrase: character.catchphrase.clone(),
|
||||
residence: character.residence.clone(),
|
||||
notes: character.notes.clone(),
|
||||
color: character.color.clone(),
|
||||
physical: vec![],
|
||||
psychological: vec![],
|
||||
relations: vec![],
|
||||
skills: vec![],
|
||||
weaknesses: vec![],
|
||||
strengths: vec![],
|
||||
goals: vec![],
|
||||
motivations: vec![],
|
||||
arc: vec![],
|
||||
secrets: vec![],
|
||||
fears: vec![],
|
||||
flaws: vec![],
|
||||
beliefs: vec![],
|
||||
conflicts: vec![],
|
||||
quotes: vec![],
|
||||
distinguishing_marks: vec![],
|
||||
items: vec![],
|
||||
affiliations: vec![],
|
||||
});
|
||||
}
|
||||
|
||||
let aggregated_character_data: &mut CompleteCharacterProps = unique_characters_map.get_mut(&character_identifier).unwrap();
|
||||
|
||||
for attribute in &character.physical { aggregated_character_data.physical.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.psychological { aggregated_character_data.psychological.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.relations { aggregated_character_data.relations.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.skills { aggregated_character_data.skills.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.weaknesses { aggregated_character_data.weaknesses.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.strengths { aggregated_character_data.strengths.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.goals { aggregated_character_data.goals.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.motivations { aggregated_character_data.motivations.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.arc { aggregated_character_data.arc.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.secrets { aggregated_character_data.secrets.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.fears { aggregated_character_data.fears.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.flaws { aggregated_character_data.flaws.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.beliefs { aggregated_character_data.beliefs.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.conflicts { aggregated_character_data.conflicts.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.quotes { aggregated_character_data.quotes.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.distinguishing_marks { aggregated_character_data.distinguishing_marks.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.items { aggregated_character_data.items.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
for attribute in &character.affiliations { aggregated_character_data.affiliations.push(Attribute { id: attribute.id.clone(), name: attribute.name.clone() }); }
|
||||
}
|
||||
|
||||
let formatted_characters_description: String = unique_characters_map.values().map(|character| {
|
||||
let mut character_description_lines: Vec<String> = Vec::new();
|
||||
|
||||
let full_name: String = [&character.name, &character.last_name].iter().filter(|name| !name.is_empty()).map(|name| name.as_str()).collect::<Vec<&str>>().join(" ");
|
||||
if !full_name.is_empty() {
|
||||
character_description_lines.push(format!("Nom : {}", full_name));
|
||||
}
|
||||
|
||||
let simple_properties: Vec<(&str, &str)> = vec![
|
||||
("Category", &character.category),
|
||||
("Title", &character.title),
|
||||
("Role", &character.role),
|
||||
("Biography", &character.biography),
|
||||
("History", &character.history),
|
||||
];
|
||||
for (property_label, property_value) in simple_properties {
|
||||
if !property_value.is_empty() {
|
||||
character_description_lines.push(format!("{} : {}", property_label, property_value));
|
||||
}
|
||||
}
|
||||
|
||||
let array_properties: Vec<(&str, &Vec<Attribute>)> = vec![
|
||||
("Physical", &character.physical),
|
||||
("Psychological", &character.psychological),
|
||||
("Relations", &character.relations),
|
||||
("Skills", &character.skills),
|
||||
("Weaknesses", &character.weaknesses),
|
||||
("Strengths", &character.strengths),
|
||||
("Goals", &character.goals),
|
||||
("Motivations", &character.motivations),
|
||||
("Arc", &character.arc),
|
||||
("Secrets", &character.secrets),
|
||||
("Fears", &character.fears),
|
||||
("Flaws", &character.flaws),
|
||||
("Beliefs", &character.beliefs),
|
||||
("Conflicts", &character.conflicts),
|
||||
("Quotes", &character.quotes),
|
||||
("DistinguishingMarks", &character.distinguishing_marks),
|
||||
("Items", &character.items),
|
||||
("Affiliations", &character.affiliations),
|
||||
];
|
||||
for (capitalized_property_key, attribute_values) in array_properties {
|
||||
if !attribute_values.is_empty() {
|
||||
let formatted_attribute_values: String = attribute_values.iter().map(|attribute_item| attribute_item.name.as_str()).collect::<Vec<&str>>().join(", ");
|
||||
character_description_lines.push(format!("{} : {}", capitalized_property_key, formatted_attribute_values));
|
||||
}
|
||||
}
|
||||
|
||||
character_description_lines.join("\n")
|
||||
}).collect::<Vec<String>>().join("\n\n");
|
||||
|
||||
formatted_characters_description
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user