use async_graphql::{Context, EmptySubscription, Object, Schema, SimpleObject}; use uuid::Uuid; use crate::services::users_service::UserService; pub type CibusSchema = Schema; pub struct MutationRoot; #[Object] impl MutationRoot { async fn login( &self, ctx: &Context<'_>, username: String, password: String, ) -> anyhow::Result { let user_service = ctx.data_unchecked::(); let valid = user_service.validate_user(username, password).await?; let returnvalid = match valid { Some(..) => true, None => false, }; Ok(returnvalid) } async fn register( &self, ctx: &Context<'_>, username: String, password: String, ) -> anyhow::Result { let user_service = ctx.data_unchecked::(); let user_id = user_service.add_user(username, password).await?; Ok(user_id) } } pub struct QueryRoot; #[Object] impl QueryRoot { async fn get_upcoming(&self, _ctx: &Context<'_>) -> Vec { vec![Event::new( None, "Some-name".into(), None, None, EventDate::new(2022, 08, 08, 23, 51), )] } } #[derive(SimpleObject)] pub struct Event { pub id: String, pub name: String, pub description: Option>, pub location: Option, pub date: EventDate, } impl Event { pub fn new( id: Option, name: String, description: Option>, location: Option, date: EventDate, ) -> Self { Self { id: id.unwrap_or_else(|| Uuid::new_v4().to_string()), name, description, location, date, } } } #[derive(SimpleObject)] pub struct EventDate { pub year: u32, pub month: u32, pub day: u32, pub hour: u32, pub minute: u32, } impl EventDate { pub fn new(year: u32, month: u32, day: u32, hour: u32, minute: u32) -> Self { Self { year, month, day, hour, minute, } } }