api: add the rest of the fields to User

This commit is contained in:
Valentin Tolmer 2021-08-27 01:10:45 +02:00 committed by nitnelave
parent a1f40a32a5
commit 848cc86d73
6 changed files with 43 additions and 31 deletions

View File

@ -2,8 +2,9 @@ query ListUsersQuery($filters: RequestFilter) {
users(filters: $filters) { users(filters: $filters) {
id id
email email
groups { displayName
id firstName
} lastName
creationDate
} }
} }

View File

@ -1,8 +0,0 @@
use graphql_client::GraphQLQuery;
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../schema.graphql",
query_path = "queries/list_users.graphql"
)]
pub struct ListUsersQuery;

View File

@ -4,7 +4,6 @@ mod api;
mod app; mod app;
mod cookies; mod cookies;
mod create_user; mod create_user;
mod graphql_api;
mod login; mod login;
mod logout; mod logout;
mod user_details; mod user_details;

View File

@ -1,19 +1,24 @@
use crate::{ use crate::api::HostService;
api::HostService,
graphql_api::{
list_users_query::{self, RequestFilter, ResponseData},
ListUsersQuery,
},
};
use anyhow::{anyhow, Result}; use anyhow::{anyhow, Result};
use lldap_model::*; use graphql_client::GraphQLQuery;
use yew::format::Json; use yew::format::Json;
use yew::prelude::*; use yew::prelude::*;
use yew::services::{fetch::FetchTask, ConsoleService}; use yew::services::{fetch::FetchTask, ConsoleService};
#[derive(GraphQLQuery)]
#[graphql(
schema_path = "../schema.graphql",
query_path = "queries/list_users.graphql",
response_derives = "Debug",
custom_scalars_module = "chrono"
)]
pub struct ListUsersQuery;
use list_users_query::{RequestFilter, ResponseData};
pub struct UserTable { pub struct UserTable {
link: ComponentLink<Self>, link: ComponentLink<Self>,
users: Option<Result<Vec<User>>>, users: Option<Result<Vec<list_users_query::ListUsersQueryUsers>>>,
// Used to keep the request alive long enough. // Used to keep the request alive long enough.
_task: Option<FetchTask>, _task: Option<FetchTask>,
} }
@ -54,15 +59,7 @@ impl Component for UserTable {
fn update(&mut self, msg: Self::Message) -> ShouldRender { fn update(&mut self, msg: Self::Message) -> ShouldRender {
match msg { match msg {
Msg::ListUsersResponse(Ok(users)) => { Msg::ListUsersResponse(Ok(users)) => {
self.users = Some(Ok(users self.users = Some(Ok(users.users.into_iter().collect()));
.users
.into_iter()
.map(|u| User {
user_id: u.id,
email: u.email,
..Default::default()
})
.collect()));
ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str()); ConsoleService::log(format!("Response: {:?}", Json(&self.users)).as_str());
true true
} }
@ -87,7 +84,7 @@ impl Component for UserTable {
.map(|u| { .map(|u| {
html! { html! {
<tr> <tr>
<td>{&u.user_id}</td> <td>{&u.id}</td>
<td>{&u.email}</td> <td>{&u.email}</td>
<td>{&u.display_name.as_ref().unwrap_or(&String::new())}</td> <td>{&u.display_name.as_ref().unwrap_or(&String::new())}</td>
<td>{&u.first_name.as_ref().unwrap_or(&String::new())}</td> <td>{&u.first_name.as_ref().unwrap_or(&String::new())}</td>

View File

@ -26,9 +26,16 @@ type Query {
users(filters: RequestFilter): [User!]! users(filters: RequestFilter): [User!]!
} }
"NaiveDateTime"
scalar NaiveDateTime
type User { type User {
id: String! id: String!
email: String! email: String!
displayName: String
firstName: String
lastName: String
creationDate: NaiveDateTime!
"The groups to which this user belongs." "The groups to which this user belongs."
groups: [Group!]! groups: [Group!]!
} }

View File

@ -145,6 +145,22 @@ impl<Handler: BackendHandler + Sync> User<Handler> {
&self.user.email &self.user.email
} }
fn display_name(&self) -> Option<&String> {
self.user.display_name.as_ref()
}
fn first_name(&self) -> Option<&String> {
self.user.first_name.as_ref()
}
fn last_name(&self) -> Option<&String> {
self.user.last_name.as_ref()
}
fn creation_date(&self) -> chrono::NaiveDateTime {
self.user.creation_date
}
/// The groups to which this user belongs. /// The groups to which this user belongs.
async fn groups(&self, context: &Context<Handler>) -> FieldResult<Vec<Group<Handler>>> { async fn groups(&self, context: &Context<Handler>) -> FieldResult<Vec<Group<Handler>>> {
Ok(context Ok(context