Added apis
This commit is contained in:
69
como_gql/src/graphql.rs
Normal file
69
como_gql/src/graphql.rs
Normal file
@@ -0,0 +1,69 @@
|
||||
use async_graphql::{Context, EmptySubscription, Object, Schema, SimpleObject};
|
||||
|
||||
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto, ItemState};
|
||||
use como_infrastructure::register::ServiceRegister;
|
||||
use uuid::Uuid;
|
||||
|
||||
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 service_register = ctx.data_unchecked::<ServiceRegister>();
|
||||
|
||||
let valid = service_register
|
||||
.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<String> {
|
||||
let service_register = ctx.data_unchecked::<ServiceRegister>();
|
||||
|
||||
let user_id = service_register
|
||||
.user_service
|
||||
.add_user(username, password)
|
||||
.await?;
|
||||
|
||||
Ok(user_id)
|
||||
}
|
||||
|
||||
async fn create_item(
|
||||
&self,
|
||||
ctx: &Context<'_>,
|
||||
item: CreateItemDto,
|
||||
) -> anyhow::Result<CreatedItemDto> {
|
||||
let services_register = ctx.data_unchecked::<ServiceRegister>();
|
||||
|
||||
let created_item = services_register.item_service.add_item(item).await?;
|
||||
|
||||
Ok(created_item)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct QueryRoot;
|
||||
|
||||
#[Object]
|
||||
impl QueryRoot {
|
||||
async fn hello(&self, ctx: &Context<'_>) -> String {
|
||||
"hello".into()
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user