2019-12-07 21:16:46 +07:00
|
|
|
use juniper;
|
|
|
|
use mysql::{from_row, params};
|
|
|
|
|
|
|
|
use crate::schemas::product::Product;
|
|
|
|
use crate::schemas::root::Context;
|
|
|
|
|
|
|
|
/// User
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct User {
|
|
|
|
pub id: String,
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(GraphQLInputObject)]
|
|
|
|
#[graphql(description = "User Input")]
|
|
|
|
pub struct UserInput {
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[juniper::object(Context = Context)]
|
|
|
|
impl User {
|
2019-12-07 23:59:24 +06:00
|
|
|
fn id(&self) -> &str {
|
|
|
|
&self.id
|
|
|
|
}
|
2019-12-07 21:16:46 +07:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
&self.name
|
|
|
|
}
|
2019-12-07 23:59:24 +06:00
|
|
|
fn email(&self) -> &str {
|
|
|
|
&self.email
|
|
|
|
}
|
2019-12-07 21:16:46 +07:00
|
|
|
|
|
|
|
fn products(&self, context: &Context) -> Vec<Product> {
|
|
|
|
let mut conn = context.dbpool.get().unwrap();
|
2019-12-07 23:59:24 +06:00
|
|
|
let products = conn
|
|
|
|
.prep_exec(
|
|
|
|
"select * from product where user_id=:user_id",
|
|
|
|
params! {
|
|
|
|
"user_id" => &self.id
|
|
|
|
},
|
|
|
|
)
|
2019-12-07 21:16:46 +07:00
|
|
|
.map(|result| {
|
2019-12-07 23:59:24 +06:00
|
|
|
result
|
|
|
|
.map(|x| x.unwrap())
|
|
|
|
.map(|mut row| {
|
|
|
|
let (id, user_id, name, price) = from_row(row);
|
|
|
|
Product {
|
|
|
|
id,
|
|
|
|
user_id,
|
|
|
|
name,
|
|
|
|
price,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
})
|
|
|
|
.unwrap();
|
2019-12-07 21:16:46 +07:00
|
|
|
products
|
|
|
|
}
|
|
|
|
}
|