add checks on search params
This commit is contained in:
parent
bf83807318
commit
f397b8bdf9
|
@ -929,7 +929,7 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "polodb_core"
|
||||
version = "4.3.1"
|
||||
version = "4.3.2"
|
||||
dependencies = [
|
||||
"bson",
|
||||
"byteorder",
|
||||
|
@ -1035,9 +1035,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.8.1"
|
||||
version = "1.8.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af83e617f331cc6ae2da5443c602dfa5af81e517212d9d611a5b3ba1777b5370"
|
||||
checksum = "d1a59b5d8e97dee33696bf13c5ba8ab85341c002922fba050069326b9c498974"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
|
@ -1046,9 +1046,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "regex-syntax"
|
||||
version = "0.7.1"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5996294f19bd3aae0453a862ad728f60e6600695733dd5df01da90c54363a3c"
|
||||
checksum = "436b050e76ed2903236f032a59761c1eb99e1b0aead2c257922771dab1fc8c78"
|
||||
|
||||
[[package]]
|
||||
name = "reqwest"
|
||||
|
@ -1357,8 +1357,10 @@ dependencies = [
|
|||
"once_cell",
|
||||
"poise",
|
||||
"polodb_core",
|
||||
"regex",
|
||||
"serde",
|
||||
"tokio",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
|
|
@ -6,9 +6,10 @@ authors = ["Antoine Langlois <antoine.l@antoine-langlois.net>"]
|
|||
description = "A Discord bot for the Tech channel"
|
||||
|
||||
[dependencies]
|
||||
once_cell = "1.17.1"
|
||||
once_cell = "1.17"
|
||||
poise = "0.5"
|
||||
polodb_core = { path = "../PoloDB/src/polodb_core" }
|
||||
# polodb_core = "4.2.0"
|
||||
regex = "1.8"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
tokio = { version = "1.28.0", features = ["macros", "rt-multi-thread"] }
|
||||
tokio = { version = "1.28", features = ["macros", "rt-multi-thread"] }
|
||||
url = "2.3"
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use poise::command;
|
||||
use url::Url;
|
||||
|
||||
use crate::database::*;
|
||||
|
||||
|
@ -52,6 +53,11 @@ pub async fn add(
|
|||
#[description = "Technology name"] technology: String,
|
||||
#[description = "Git repository link"] link: 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())?;
|
||||
|
||||
ctx.say(format!("Added {technology} with link {link}",))
|
||||
|
@ -86,7 +92,7 @@ pub async fn list(ctx: Context<'_>) -> Result<(), Error> {
|
|||
#[command(slash_command, prefix_command)]
|
||||
pub async fn search(
|
||||
ctx: Context<'_>,
|
||||
#[description = "Technology name"] technology: String,
|
||||
#[description = "Technology name (can be a regex string)"] technology: String,
|
||||
#[description = "Regex options"] options: Option<String>,
|
||||
) -> Result<(), Error> {
|
||||
let found_techs = search_tech(technology, options.map_or(String::new(), |opts| opts))?;
|
||||
|
|
|
@ -53,14 +53,22 @@ pub fn list_tech() -> Result<Vec<Technology>, Error> {
|
|||
}
|
||||
|
||||
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
|
||||
} }
|
||||
};
|
||||
|
||||
Ok(DB
|
||||
.get()
|
||||
.unwrap()
|
||||
.collection::<Technology>("technologies")
|
||||
.find(doc! { "name": {"$regex": Regex {
|
||||
pattern: name,
|
||||
options: options,
|
||||
}} })
|
||||
.find(doc)
|
||||
.map_err(|err| Error::new(ErrorKind::InvalidInput, err))?
|
||||
.map(
|
||||
|doc| doc.unwrap(), // todo: find a way to handle error
|
||||
|
|
Loading…
Reference in New Issue