add tags to techs
This commit is contained in:
parent
f397b8bdf9
commit
3237235fbf
|
@ -52,16 +52,25 @@ pub async fn add(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "Technology name"] technology: String,
|
#[description = "Technology name"] technology: String,
|
||||||
#[description = "Git repository link"] link: String,
|
#[description = "Git repository link"] link: String,
|
||||||
|
#[description = "Technology tags (comma separated)"] tags: Option<String>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
if !Url::parse(&link).is_ok() {
|
if !Url::parse(&link).is_ok() {
|
||||||
ctx.say(format!("Link {link} is not a valid URL")).await?;
|
ctx.say(format!("Link {link} is not a valid URL")).await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
add_tech(link.clone(), technology.clone())?;
|
let tags = tags.unwrap_or(String::new());
|
||||||
|
|
||||||
ctx.say(format!("Added {technology} with link {link}",))
|
add_tech(
|
||||||
.await?;
|
&link,
|
||||||
|
&technology,
|
||||||
|
&(&tags).split(",").collect::<Vec<&str>>(),
|
||||||
|
)?;
|
||||||
|
|
||||||
|
ctx.say(format!(
|
||||||
|
"Added {technology} with link {link} and tags [{tags}]"
|
||||||
|
))
|
||||||
|
.await?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -79,7 +88,7 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
|
||||||
"Saved technologies: {}",
|
"Saved technologies: {}",
|
||||||
techs
|
techs
|
||||||
.iter()
|
.iter()
|
||||||
.map(|tech| format!("[{}]({})", tech.name, tech.link))
|
.map(|tech| format!("[{}]({}) **{}**", tech.name, tech.link, tech.tags.join(",")))
|
||||||
.collect::<Vec<String>>()
|
.collect::<Vec<String>>()
|
||||||
.join(", ")
|
.join(", ")
|
||||||
))
|
))
|
||||||
|
@ -94,8 +103,16 @@ pub async fn search(
|
||||||
ctx: Context<'_>,
|
ctx: Context<'_>,
|
||||||
#[description = "Technology name (can be a regex string)"] technology: String,
|
#[description = "Technology name (can be a regex string)"] technology: String,
|
||||||
#[description = "Regex options"] options: Option<String>,
|
#[description = "Regex options"] options: Option<String>,
|
||||||
|
#[description = "Technology tags (comma separated)"] tags: Option<String>,
|
||||||
) -> Result<(), Error> {
|
) -> 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 {
|
if found_techs.len() == 0 {
|
||||||
ctx.say("No technologies found").await?;
|
ctx.say("No technologies found").await?;
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|
|
@ -12,6 +12,7 @@ pub static DB: OnceCell<Database> = OnceCell::new();
|
||||||
pub struct Technology {
|
pub struct Technology {
|
||||||
pub link: String,
|
pub link: String,
|
||||||
pub name: String,
|
pub name: String,
|
||||||
|
pub tags: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
|
@ -20,11 +21,15 @@ struct AuthorizedUser {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Add a new technology to the database.
|
/// 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()
|
DB.get()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collection::<Technology>("technologies")
|
.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))?;
|
.map_err(|err| Error::new(ErrorKind::InvalidInput, err))?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -52,23 +57,17 @@ pub fn list_tech() -> Result<Vec<Technology>, Error> {
|
||||||
.collect())
|
.collect())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn search_tech(name: String, options: String) -> Result<Vec<Technology>, Error> {
|
pub fn search_tech(name: String, options: String, tags: &[&str]) -> 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
|
|
||||||
} }
|
|
||||||
};
|
|
||||||
|
|
||||||
Ok(DB
|
Ok(DB
|
||||||
.get()
|
.get()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.collection::<Technology>("technologies")
|
.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_err(|err| Error::new(ErrorKind::InvalidInput, err))?
|
||||||
.map(
|
.map(
|
||||||
|doc| doc.unwrap(), // todo: find a way to handle error
|
|doc| doc.unwrap(), // todo: find a way to handle error
|
||||||
|
|
Loading…
Reference in New Issue