2022-06-07 22:53:28 -04:00
|
|
|
use juniper::{graphql_object, GraphQLInputObject};
|
2022-07-09 23:11:36 +01:00
|
|
|
use mysql::{from_row, params, prelude::*, Row};
|
2019-12-07 21:16:46 +07:00
|
|
|
|
2022-07-09 23:11:36 +01:00
|
|
|
use crate::schemas::{product::Product, root::Context};
|
2019-12-07 21:16:46 +07:00
|
|
|
|
|
|
|
/// User
|
|
|
|
#[derive(Default, Debug)]
|
|
|
|
pub struct User {
|
|
|
|
pub id: String,
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
2022-07-09 23:11:36 +01:00
|
|
|
impl User {
|
|
|
|
pub(crate) fn from_row(row: Row) -> Self {
|
|
|
|
let (id, name, email) = from_row(row);
|
|
|
|
User { id, name, email }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 21:16:46 +07:00
|
|
|
#[derive(GraphQLInputObject)]
|
|
|
|
#[graphql(description = "User Input")]
|
|
|
|
pub struct UserInput {
|
|
|
|
pub name: String,
|
|
|
|
pub email: String,
|
|
|
|
}
|
|
|
|
|
2022-06-07 22:53:28 -04:00
|
|
|
#[graphql_object(Context = Context)]
|
2019-12-07 21:16:46 +07:00
|
|
|
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> {
|
2022-07-09 23:11:36 +01:00
|
|
|
let mut conn = context.db_pool.get().unwrap();
|
2020-04-03 16:14:30 +09:00
|
|
|
|
2022-07-09 23:11:36 +01:00
|
|
|
conn.exec(
|
|
|
|
"SELECT * FROM product WHERE user_id = :user_id",
|
|
|
|
params! { "user_id" => &self.id },
|
2020-04-03 16:16:17 +09:00
|
|
|
)
|
|
|
|
.unwrap()
|
2022-07-09 23:11:36 +01:00
|
|
|
.into_iter()
|
|
|
|
.map(Product::from_row)
|
|
|
|
.collect()
|
2019-12-07 21:16:46 +07:00
|
|
|
}
|
|
|
|
}
|