Clarify GraphQL following methods on user

Remove `following` and replace it with `usersFollowed` and
`languagesFollowed`
This commit is contained in:
Lucien Cartier-Tilet 2023-01-18 11:12:57 +01:00
parent c5f5e770e2
commit 4faaaf244d
Signed by: phundrak
GPG Key ID: BD7789E705CB8DCA
2 changed files with 50 additions and 6 deletions

View File

@ -293,7 +293,7 @@ pub struct LangTranslatesTo {
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)] #[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = userfollowlanguage)] #[diesel(table_name = userfollowlanguage)]
pub struct UserFollowLanguage { pub struct UserFollowLanguage {
id: i32, pub id: i32,
lang: Uuid, pub lang: Uuid,
userid: String, pub userid: String,
} }

View File

@ -1,10 +1,11 @@
use super::{ use super::{
super::schema, super::schema,
languages::{Language, UserFollowLanguage},
words::{Word, WordLearning, WordLearningStatus}, words::{Word, WordLearning, WordLearningStatus},
}; };
use diesel::prelude::*; use diesel::prelude::*;
use juniper::FieldResult; use juniper::FieldResult;
use tracing::{debug, info}; use tracing::{debug, info, warn};
use schema::{userfollows, users}; use schema::{userfollows, users};
@ -29,8 +30,8 @@ impl User {
} }
#[graphql(description = "Who the user follows")] #[graphql(description = "Who the user follows")]
pub fn following(&self, context: &Context) -> FieldResult<Vec<User>> { pub fn users_followed(&self, context: &Context) -> FieldResult<Vec<User>> {
use super::super::schema::{userfollows, users}; use schema::{userfollows, users};
let conn = &mut context.db.conn().map_err(|e| { let conn = &mut context.db.conn().map_err(|e| {
DatabaseError::new( DatabaseError::new(
format!("Failed to connect to database: {e:?}"), format!("Failed to connect to database: {e:?}"),
@ -68,6 +69,49 @@ impl User {
.collect::<Vec<User>>()) .collect::<Vec<User>>())
} }
#[graphql(description = "Which languages the user follows")]
pub fn languages_followed(
&self,
context: &Context,
) -> FieldResult<Vec<Language>> {
use schema::userfollowlanguage::dsl;
let conn = &mut context.db.conn().map_err(|e| {
DatabaseError::new(
format!("Failed to connect to database: {e:?}"),
"Database connection error",
)
})?;
Ok(dsl::userfollowlanguage
.filter(dsl::userid.eq(self.id.clone()))
.load::<UserFollowLanguage>(conn)
.map_err(|e| {
DatabaseError::new(
format!(
"Failed to retrieve user follows from database: {e:?}"
),
"Database reading error",
)
})?
.iter()
.filter_map(|lang_follow| {
use schema::languages::dsl;
match dsl::languages
.find(lang_follow.lang)
.first::<Language>(conn) {
Ok(language) => Some(language),
Err(e) => {
warn!(
"Failed to retrieve language {} from database: {e:?}",
lang_follow.lang
);
None
}
}
})
.collect::<Vec<Language>>()
)
}
#[graphql( #[graphql(
description = "What words the user is learning or has learned", description = "What words the user is learning or has learned",
arguments(status( arguments(status(