2025-06-05 23:56:15 +02:00
|
|
|
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 {
|
2025-08-09 12:19:06 +02:00
|
|
|
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)
|
2025-06-05 23:56:15 +02:00
|
|
|
.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
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|