59 lines
1.3 KiB
Rust
59 lines
1.3 KiB
Rust
use crate::error::Error;
|
|
use j_db::database::Database;
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
#[derive(Debug, Deserialize, Serialize, Clone)]
|
|
pub struct SocialCreditPhrase {
|
|
id: Option<u64>,
|
|
pub phrase: String,
|
|
pub lower_bound: i64,
|
|
pub upper_bound: i64,
|
|
}
|
|
|
|
impl j_db::model::JdbModel for SocialCreditPhrase {
|
|
fn id(&self) -> Option<u64> {
|
|
self.id
|
|
}
|
|
|
|
fn set_id(&mut self, id: u64) {
|
|
self.id = Some(id)
|
|
}
|
|
|
|
fn tree() -> String {
|
|
"SocialCreditPhrase".to_string()
|
|
}
|
|
}
|
|
|
|
impl SocialCreditPhrase {
|
|
pub fn check_if_match(db: &Database, msg: &str) -> Result<Option<Self>, Error> {
|
|
let lowercase_msg = msg.to_lowercase();
|
|
let matches = db
|
|
.filter(|_, phrase: &Self| lowercase_msg.contains(&phrase.phrase.to_lowercase()))?
|
|
.next();
|
|
|
|
Ok(matches)
|
|
}
|
|
|
|
pub fn add_new_phrase(
|
|
db: &Database,
|
|
phrase: String,
|
|
lower_bound: i64,
|
|
upper_bound: i64,
|
|
) -> Result<Self, Error> {
|
|
let new_phrase = db.insert(Self {
|
|
id: None,
|
|
phrase,
|
|
lower_bound,
|
|
upper_bound,
|
|
})?;
|
|
|
|
Ok(new_phrase)
|
|
}
|
|
|
|
pub fn get_phrases(db: &Database) -> Result<Vec<Self>, Error> {
|
|
let phrases = db.filter(|_, _phrase: &Self| true)?.collect();
|
|
|
|
Ok(phrases)
|
|
}
|
|
}
|