Add user, language, word lookup
Also add some error management
This commit is contained in:
parent
d0a40b7ed8
commit
3220f5c005
@ -127,14 +127,44 @@ impl Database {
|
|||||||
|
|
||||||
pub fn all_users(&self) -> Result<Vec<User>, DatabaseError> {
|
pub fn all_users(&self) -> Result<Vec<User>, DatabaseError> {
|
||||||
use self::schema::users::dsl::users;
|
use self::schema::users::dsl::users;
|
||||||
users
|
users.load::<User>(&mut self.conn()?).map_err(|e| {
|
||||||
.load::<User>(&mut self.conn()?)
|
DatabaseError::new(
|
||||||
.map_err(|e| {
|
format!("Failed to retrieve languages: {:?}", e),
|
||||||
info!("Failed to retrieve languages from database: {:?}", e);
|
"Failed to retrieve languages",
|
||||||
})
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_language(
|
||||||
|
&self,
|
||||||
|
query: &str,
|
||||||
|
) -> Result<Vec<Language>, DatabaseError> {
|
||||||
|
use self::schema::languages::dsl;
|
||||||
|
dsl::languages
|
||||||
|
.filter(dsl::name.ilike(format!("%{}%", query)))
|
||||||
|
.load::<Language>(&mut self.conn()?)
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
DatabaseError::new(
|
DatabaseError::new(
|
||||||
format!("Failed to retrieve languages: {:?}", e),
|
format!(
|
||||||
|
"Failed to retrieve languages with query {}: {:?}",
|
||||||
|
query, e
|
||||||
|
),
|
||||||
|
"Failed to retrieve languages",
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn find_user(&self, query: &str) -> Result<Vec<User>, DatabaseError> {
|
||||||
|
use self::schema::users::dsl;
|
||||||
|
dsl::users
|
||||||
|
.filter(dsl::username.ilike(format!("%{}%", query)))
|
||||||
|
.load::<User>(&mut self.conn()?)
|
||||||
|
.map_err(|e| {
|
||||||
|
DatabaseError::new(
|
||||||
|
format!(
|
||||||
|
"Failed to retrieve users with query {}: {:?}",
|
||||||
|
query, e
|
||||||
|
),
|
||||||
"Failed to retrieve languages",
|
"Failed to retrieve languages",
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
@ -224,25 +254,46 @@ impl Database {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn words(&self, language: uuid::Uuid, word: &str) -> Vec<Word> {
|
pub fn words(
|
||||||
|
&self,
|
||||||
|
language: uuid::Uuid,
|
||||||
|
word: &str,
|
||||||
|
) -> Result<Vec<Word>, DatabaseError> {
|
||||||
use self::schema::words::dsl;
|
use self::schema::words::dsl;
|
||||||
if let Ok(conn) = &mut self.conn() {
|
dsl::words
|
||||||
match dsl::words
|
.filter(dsl::language.eq(language))
|
||||||
.filter(dsl::language.eq(language))
|
.filter(dsl::norm.eq(word))
|
||||||
.filter(dsl::norm.eq(word))
|
.load::<Word>(&mut self.conn()?)
|
||||||
.load::<Word>(conn)
|
.map_err(|e| {
|
||||||
{
|
DatabaseError::new(
|
||||||
Ok(val) => val,
|
format!(
|
||||||
Err(e) => {
|
"Failed to retrieve word {} from language {}: {:?}",
|
||||||
info!(
|
|
||||||
"Error retrieving {} from language {}: {:?}",
|
|
||||||
word, language, e
|
word, language, e
|
||||||
);
|
),
|
||||||
Vec::new()
|
"Failed to retrieve languages",
|
||||||
}
|
)
|
||||||
}
|
})
|
||||||
} else {
|
}
|
||||||
Vec::new()
|
|
||||||
}
|
pub fn find_word(
|
||||||
|
&self,
|
||||||
|
language: uuid::Uuid,
|
||||||
|
query: &str,
|
||||||
|
) -> Result<Vec<Word>, DatabaseError> {
|
||||||
|
use self::schema::words::dsl;
|
||||||
|
dsl::words
|
||||||
|
.filter(dsl::language.eq(language))
|
||||||
|
.filter(dsl::norm.ilike(format!("%{}%", query)))
|
||||||
|
.load::<Word>(&mut self.conn()?)
|
||||||
|
.map_err(|e| {
|
||||||
|
DatabaseError::new(
|
||||||
|
format!(
|
||||||
|
"Failed to retrieve words from language {} with query {}: {:?}",
|
||||||
|
language,
|
||||||
|
query, e
|
||||||
|
),
|
||||||
|
"Failed to retrieve languages",
|
||||||
|
)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
use juniper::FieldResult;
|
use juniper::FieldResult;
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use super::Context;
|
use super::Context;
|
||||||
use crate::db::{
|
use crate::db::{
|
||||||
@ -23,6 +24,26 @@ impl Query {
|
|||||||
context.db.all_languages().map_err(Into::into)
|
context.db.all_languages().map_err(Into::into)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[graphql(
|
||||||
|
name = "findLanguage",
|
||||||
|
description = "Find languages by username containing query",
|
||||||
|
arguments(query(description = "String to find in language name"))
|
||||||
|
)]
|
||||||
|
fn find_language(
|
||||||
|
context: &Context,
|
||||||
|
query: String,
|
||||||
|
) -> FieldResult<Vec<Language>> {
|
||||||
|
context.db.find_language(query.as_str()).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[graphql(
|
||||||
|
name = "allUsers",
|
||||||
|
description = "Fetch all users from database",
|
||||||
|
arguments(admin_key(
|
||||||
|
name = "adminKey",
|
||||||
|
description = "Administrator key. Without it, the query cannot be executed"
|
||||||
|
))
|
||||||
|
)]
|
||||||
fn all_users(
|
fn all_users(
|
||||||
context: &Context,
|
context: &Context,
|
||||||
admin_key: String,
|
admin_key: String,
|
||||||
@ -35,6 +56,15 @@ impl Query {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[graphql(
|
||||||
|
name = "findUser",
|
||||||
|
description = "Find users by username containing query",
|
||||||
|
arguments(query(description = "String to find in usernames"))
|
||||||
|
)]
|
||||||
|
fn find_user(context: &Context, query: String) -> FieldResult<Vec<User>> {
|
||||||
|
context.db.find_user(query.as_str()).map_err(Into::into)
|
||||||
|
}
|
||||||
|
|
||||||
#[graphql(
|
#[graphql(
|
||||||
description = "Retrieve a specific language from its name and its owner's id",
|
description = "Retrieve a specific language from its name and its owner's id",
|
||||||
arguments(
|
arguments(
|
||||||
@ -66,6 +96,34 @@ impl Query {
|
|||||||
context.db.word_id(id.as_str())
|
context.db.word_id(id.as_str())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[graphql(
|
||||||
|
name = "findWord",
|
||||||
|
description = "Retrieve a word from a specific language",
|
||||||
|
arguments(
|
||||||
|
language(
|
||||||
|
description = "UUID of the language to look the word for in"
|
||||||
|
),
|
||||||
|
query(description = "String to find in the word")
|
||||||
|
)
|
||||||
|
)]
|
||||||
|
fn find_word(
|
||||||
|
context: &Context,
|
||||||
|
language: String,
|
||||||
|
query: String,
|
||||||
|
) -> FieldResult<Vec<Word>> {
|
||||||
|
match Uuid::from_str(&language) {
|
||||||
|
Ok(uuid) => context
|
||||||
|
.db
|
||||||
|
.find_word(uuid, query.as_str())
|
||||||
|
.map_err(Into::into),
|
||||||
|
Err(e) => Err(DatabaseError::new(
|
||||||
|
format!("Failed to convert {} to a UUID: {:?}", language, e),
|
||||||
|
"Conversion Error",
|
||||||
|
)
|
||||||
|
.into()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[graphql(
|
#[graphql(
|
||||||
description = "Retrieve all words with a set normal form from a set language",
|
description = "Retrieve all words with a set normal form from a set language",
|
||||||
arguments(
|
arguments(
|
||||||
@ -81,13 +139,13 @@ impl Query {
|
|||||||
language: String,
|
language: String,
|
||||||
word: String,
|
word: String,
|
||||||
) -> FieldResult<Vec<Word>> {
|
) -> FieldResult<Vec<Word>> {
|
||||||
match uuid::Uuid::from_str(&language) {
|
match Uuid::from_str(&language) {
|
||||||
Ok(uuid) => Ok(context.db.words(uuid, word.as_str())),
|
Ok(uuid) => context
|
||||||
|
.db
|
||||||
|
.words(uuid, word.as_str())
|
||||||
|
.map_err(Into::into),
|
||||||
Err(e) => Err(DatabaseError::new(
|
Err(e) => Err(DatabaseError::new(
|
||||||
format!(
|
format!("Failed to convert {} to a UUID: {:?}", language, e),
|
||||||
"Failed to convert {} to a proper UUID: {:?}",
|
|
||||||
language, e
|
|
||||||
),
|
|
||||||
"Conversion Error",
|
"Conversion Error",
|
||||||
)
|
)
|
||||||
.into()),
|
.into()),
|
||||||
|
Loading…
Reference in New Issue
Block a user