add tags to techs

This commit is contained in:
DataHearth 2023-05-24 16:53:15 +02:00
parent f397b8bdf9
commit 3237235fbf
No known key found for this signature in database
GPG Key ID: E88FD356ACC5F3C4
2 changed files with 36 additions and 20 deletions

View File

@ -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<String>,
) -> 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::<Vec<&str>>(),
)?;
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::<Vec<String>>()
.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<String>,
#[description = "Technology tags (comma separated)"] tags: Option<String>,
) -> 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::<Vec<&str>>(),
)?;
if found_techs.len() == 0 {
ctx.say("No technologies found").await?;
return Ok(());

View File

@ -12,6 +12,7 @@ pub static DB: OnceCell<Database> = OnceCell::new();
pub struct Technology {
pub link: String,
pub name: String,
pub tags: Vec<String>,
}
#[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::<Technology>("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<Vec<Technology>, Error> {
.collect())
}
pub fn search_tech(name: String, options: String) -> Result<Vec<Technology>, 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<Vec<Technology>, Error> {
Ok(DB
.get()
.unwrap()
.collection::<Technology>("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