diff --git a/src/db/models/languages.rs b/src/db/models/languages.rs index b0eefbf..b439ba7 100644 --- a/src/db/models/languages.rs +++ b/src/db/models/languages.rs @@ -72,41 +72,30 @@ impl Language { relationship: AgentLanguageRelation, ) -> Result, DatabaseError> { use schema::langandagents::dsl; - match &mut db.conn() { - Ok(conn) => Ok(dsl::langandagents - .filter(dsl::language.eq(self.id)) - .filter(dsl::relationship.eq(relationship)) - .load::(conn) - .map_err(|e| { - DatabaseError::new( - format!( - "Failed to retrieve language relationship: {:?}", - e - ), - "Database reading error", - ) - })? - .iter() - .map(|v| { - use schema::users::dsl; - dsl::users.find(v.agent.clone()).first::(conn) - }) - .filter_map(|author| match author { - Ok(val) => Some(val), - Err(e) => { - info!( - "Failed ot retrieve author from database: {:?}", - e - ); - - None - } - }) - .collect::>()), - Err(e) => { - panic!("Could not connect to the database: {:?}", e); - } - } + let conn = &mut db.conn()?; + Ok(dsl::langandagents + .filter(dsl::language.eq(self.id)) + .filter(dsl::relationship.eq(relationship)) + .load::(conn) + .map_err(|e| { + DatabaseError::new( + format!("Failed to retrieve language relationship: {e:?}"), + "Database reading error", + ) + })? + .iter() + .map(|v| { + use schema::users::dsl; + dsl::users.find(v.agent.clone()).first::(conn) + }) + .filter_map(|author| match author { + Ok(val) => Some(val), + Err(e) => { + info!("Failed ot retrieve author from database: {:?}", e); + None + } + }) + .collect::>()) } } diff --git a/src/db/models/words.rs b/src/db/models/words.rs index 8406c0c..4ccc070 100644 --- a/src/db/models/words.rs +++ b/src/db/models/words.rs @@ -133,22 +133,19 @@ impl Word { } #[graphql(description = "Language to which the word belongs")] - fn language(&self, context: &Context) -> Language { + fn language(&self, context: &Context) -> FieldResult { use schema::languages::dsl; - match &mut context.db.conn() { - Ok(conn) => { - match dsl::languages.find(self.language).first::(conn) - { - Ok(lang) => lang, - Err(e) => { - panic!("Failed to retrieve language {} of word {} from database: {:?}", - self.language, self.norm, e - ) - } - } - } - Err(e) => panic!("Failed to connect to database: {:?}", e), - } + use std::convert::Into; + dsl::languages + .find(self.language) + .first::(&mut context.db.conn()?) + .map_err(|e| DatabaseError::new( + format!( + "Failed to retrieve language {} of word {} from database: {e:?}", + self.language, self.norm + ), + "Database Error" + ).into()) } #[graphql(