ordabok/src/db/models/users.rs
Lucien Cartier-Tilet 425e00acc1
All checks were successful
continuous-integration/drone/push Build is passing
Manually add users to database, better handling of errors in API
Also manually remove users from database, and list them only as admin
2023-01-16 00:09:50 +01:00

50 lines
1.3 KiB
Rust

use super::super::schema::{userfollows, users};
use diesel::prelude::*;
use crate::graphql::Context;
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
pub struct User {
pub id: String,
pub username: String,
}
#[juniper::graphql_object(Context = Context)]
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()
}
#[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>>()
}
}
#[derive(Queryable, Insertable, Debug, Clone, PartialEq, Eq)]
#[diesel(table_name = userfollows)]
pub struct UserFollow {
pub id: i32,
pub follower: String,
pub following: String,
}