From 3237235fbf7f34f9b885e9eee96940eb908d4dc1 Mon Sep 17 00:00:00 2001 From: DataHearth Date: Wed, 24 May 2023 16:53:15 +0200 Subject: [PATCH] add tags to techs --- src/commands.rs | 27 ++++++++++++++++++++++----- src/database.rs | 29 ++++++++++++++--------------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/commands.rs b/src/commands.rs index 6767d96..8edbf89 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -52,16 +52,25 @@ pub async fn add( ctx: Context<'_>, #[description = "Technology name"] technology: String, #[description = "Git repository link"] link: String, + #[description = "Technology tags (comma separated)"] tags: Option, ) -> Result<(), Error> { if !Url::parse(&link).is_ok() { ctx.say(format!("Link {link} is not a valid URL")).await?; return Ok(()); } - add_tech(link.clone(), technology.clone())?; + let tags = tags.unwrap_or(String::new()); - ctx.say(format!("Added {technology} with link {link}",)) - .await?; + add_tech( + &link, + &technology, + &(&tags).split(",").collect::>(), + )?; + + ctx.say(format!( + "Added {technology} with link {link} and tags [{tags}]" + )) + .await?; Ok(()) } @@ -79,7 +88,7 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> { "Saved technologies: {}", techs .iter() - .map(|tech| format!("[{}]({})", tech.name, tech.link)) + .map(|tech| format!("[{}]({}) **{}**", tech.name, tech.link, tech.tags.join(","))) .collect::>() .join(", ") )) @@ -94,8 +103,16 @@ pub async fn search( ctx: Context<'_>, #[description = "Technology name (can be a regex string)"] technology: String, #[description = "Regex options"] options: Option, + #[description = "Technology tags (comma separated)"] tags: Option, ) -> Result<(), Error> { - let found_techs = search_tech(technology, options.map_or(String::new(), |opts| opts))?; + let found_techs = search_tech( + technology, + options.map_or(String::new(), |opts| opts), + &tags + .unwrap_or(String::new()) + .split(",") + .collect::>(), + )?; if found_techs.len() == 0 { ctx.say("No technologies found").await?; return Ok(()); diff --git a/src/database.rs b/src/database.rs index 45d0d78..8ab5806 100644 --- a/src/database.rs +++ b/src/database.rs @@ -12,6 +12,7 @@ pub static DB: OnceCell = OnceCell::new(); pub struct Technology { pub link: String, pub name: String, + pub tags: Vec, } #[derive(Debug, Serialize, Deserialize)] @@ -20,11 +21,15 @@ struct AuthorizedUser { } /// Add a new technology to the database. -pub fn add_tech(link: String, name: String) -> Result<(), Error> { +pub fn add_tech(name: &str, link: &str, tags: &[&str]) -> Result<(), Error> { DB.get() .unwrap() .collection::("technologies") - .insert_one(Technology { link, name }) + .insert_one(Technology { + link: link.into(), + name: name.into(), + tags: tags.iter().map(|s| s.to_string()).collect(), + }) .map_err(|err| Error::new(ErrorKind::InvalidInput, err))?; Ok(()) @@ -52,23 +57,17 @@ pub fn list_tech() -> Result, Error> { .collect()) } -pub fn search_tech(name: String, options: String) -> Result, Error> { - let doc = if regex::Regex::new(&name).is_ok() { - doc! { "name": {"$regex": Regex { - pattern: name, - options: options, - }} } - } else { - doc! { "name": { - "$eq": name - } } - }; - +pub fn search_tech(name: String, options: String, tags: &[&str]) -> Result, Error> { Ok(DB .get() .unwrap() .collection::("technologies") - .find(doc) + .find(doc! { "name": {"$regex": Regex { + pattern: name, + options: options, + }}, "tags": { + "$in": tags + } }) .map_err(|err| Error::new(ErrorKind::InvalidInput, err))? .map( |doc| doc.unwrap(), // todo: find a way to handle error