Lucien Cartier-Tilet 49c7d86102 feat: enable transaction support via sqlx::Executor
This commit abstracts the database operations to use the generic
`sqlx::Executor` trait instead of a concrete `&sqlx::PgPool`.

This change allows all generated methods (find, create, update,
delete, and relationships) to be executed within a
`sqlx::Transaction`, in addition to a connection pool. This is a
crucial feature for ensuring atomic operations and data consistency.

The public-facing traits `Georm` and `Defaultable` have been updated
to require `sqlx::Executor`, and the documentation has been updated to
reflect this new capability.
2025-08-09 15:35:40 +02:00

47 lines
1.3 KiB
Rust

use super::User;
use crate::{Result, errors::UserInputError};
use georm::Georm;
use std::collections::HashMap;
#[derive(Debug, Georm, Clone)]
#[georm(table = "Comments")]
pub struct Comment {
#[georm(id, defaultable)]
pub id: i32,
#[georm(relation = {
entity = User,
table = "Users",
name = "author"
})]
pub author_id: i32,
pub content: String,
}
impl Comment {
pub async fn select_comment<'e, E>(prompt: &str, executor: E) -> Result<Self>
where
E: sqlx::Executor<'e, Database = sqlx::Postgres>,
{
let comments: HashMap<String, Self> = Self::find_all(executor)
.await?
.into_iter()
.map(|comment| (comment.content.clone(), comment))
.collect();
let comment_content = inquire::Select::new(prompt, comments.clone().into_keys().collect())
.prompt()
.map_err(UserInputError::InquireError)?;
let comment: &Self = comments.get(&comment_content).unwrap();
Ok(comment.clone())
}
}
impl std::fmt::Display for Comment {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"Comment:\nID:\t{}\nAuthor:\t{}\nContent:\t{}",
self.id, self.author_id, self.content
)
}
}