como/como_bin/src/graphql.rs

107 lines
2.2 KiB
Rust
Raw Normal View History

2022-10-02 14:15:45 +02:00
use async_graphql::{Context, EmptySubscription, Object, Schema, SimpleObject};
2022-10-02 20:52:29 +02:00
2022-10-02 12:12:08 +02:00
use uuid::Uuid;
2022-10-02 14:15:45 +02:00
use crate::services::users_service::UserService;
pub type CibusSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
pub struct MutationRoot;
#[Object]
impl MutationRoot {
async fn login(
&self,
ctx: &Context<'_>,
username: String,
password: String,
) -> anyhow::Result<bool> {
let user_service = ctx.data_unchecked::<UserService>();
let valid = user_service.validate_user(username, password).await?;
2022-10-02 20:51:06 +02:00
let returnvalid = match valid {
2022-10-02 14:15:45 +02:00
Some(..) => true,
None => false,
2022-10-02 20:51:06 +02:00
};
Ok(returnvalid)
2022-10-02 14:15:45 +02:00
}
async fn register(
&self,
ctx: &Context<'_>,
username: String,
password: String,
) -> anyhow::Result<String> {
let user_service = ctx.data_unchecked::<UserService>();
let user_id = user_service.add_user(username, password).await?;
2022-10-02 14:16:13 +02:00
Ok(user_id)
2022-10-02 14:15:45 +02:00
}
}
2022-10-02 12:12:08 +02:00
pub struct QueryRoot;
#[Object]
impl QueryRoot {
2022-10-02 14:15:45 +02:00
async fn get_upcoming(&self, _ctx: &Context<'_>) -> Vec<Event> {
2022-10-02 12:12:08 +02:00
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<Vec<String>>,
pub location: Option<String>,
pub date: EventDate,
}
impl Event {
pub fn new(
id: Option<String>,
name: String,
description: Option<Vec<String>>,
location: Option<String>,
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,
}
}
}