use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};
table! {
users {
id -> Integer,
first_name -> Text,
email -> Text,
}
}
#[derive(Queryable, Selectable, Debug, Clone, serde::Serialize, serde::Deserialize)]
#[diesel(table_name = users)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct User {
pub id: i32,
pub first_name: String,
pub email: String,
}
table! {
school_users(school_id, user_id){
school_id -> Integer,
user_id -> Integer,
}
}
#[derive(
Queryable,
Selectable,
Debug,
Associations,
// Identifiable,
PartialEq,
Clone,
serde::Serialize,
serde::Deserialize,
)]
#[diesel(table_name = school_users)]
#[diesel(belongs_to(School))]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct SchoolUser {
pub school_id: i32,
pub user_id: i32,
}
table! {
school{
id -> Integer,
name -> Text,
manager-> Integer,
}
}
#[derive(
Queryable,
Selectable,
Debug,
Identifiable,
PartialEq,
Clone,
serde::Serialize,
serde::Deserialize,
)]
#[diesel(table_name = school)]
#[diesel(check_for_backend(diesel::pg::Pg))]
pub struct School {
pub id: i32,
pub name: String,
pub manager: i32,
}
diesel::joinable!(school_users -> school (school_id));
diesel::allow_tables_to_appear_in_same_query!(school, school_users,);
pub async fn get_user_with_email(
email: &str,
c: actix_web::web::Data<diesel_async::pooled_connection::bb8::Pool<AsyncPgConnection>>,
) -> User {
let conn = c.into_inner();
let mut conn = conn.get().await.unwrap();
let user: Vec<crate::users::User> = users::table
.filter(users::email.eq(email))
.select(crate::users::User::as_select())
.load(&mut conn)
.await
.unwrap();
user[0].clone()
}
pub async fn get_schools(
id: i32,
c: actix_web::web::Data<diesel_async::pooled_connection::bb8::Pool<AsyncPgConnection>>,
) -> Vec<School> {
let conn = c.into_inner();
let mut conn = conn.get().await.unwrap();
let schools: Vec<crate::users::School> = school::table
.filter(school::manager.eq(id))
.select(School::as_select())
.load(&mut conn)
.await
.unwrap();
schools
}