ordabok/src/db/models/users.rs

50 lines
1.3 KiB
Rust
Raw Normal View History

2023-01-03 15:11:43 +00:00
use super::super::schema::{userfollows, users};
use diesel::prelude::*;
use crate::graphql::Context;
2023-01-03 15:11:43 +00:00
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct User {
pub id: String,
pub username: String,
2023-01-03 15:11:43 +00:00
}
#[juniper::graphql_object(Context = Context)]
2023-01-04 18:31:52 +00:00
impl User {
#[graphql(description = "Appwrite ID of the user")]
pub fn id(&self) -> String {
self.id.clone()
}
#[graphql(description = "The user's apparent name")]
pub fn username(&self) -> String {
self.username.clone()
2023-01-04 18:31:52 +00:00
}
#[graphql(description = "Who the user follows")]
pub fn following(&self, context: &Context) -> Vec<User> {
use super::super::schema::{userfollows, users};
let conn = &mut context.db.conn().unwrap();
userfollows::dsl::userfollows
.filter(userfollows::dsl::follower.eq(self.id.clone()))
.load::<UserFollow>(conn)
.unwrap()
.iter()
.map(|f| {
users::dsl::users
.find(f.following.clone())
.first::<User>(conn)
.unwrap()
})
.collect::<Vec<User>>()
2023-01-04 18:31:52 +00:00
}
}
2023-01-03 15:11:43 +00:00
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = userfollows)]
pub struct UserFollow {
pub id: i32,
pub follower: String,
pub following: String,
}