como/como_gql/src/graphql.rs

69 lines
1.6 KiB
Rust
Raw Normal View History

2022-10-04 11:07:14 +02:00
use async_graphql::{Context, EmptySubscription, Object, Schema};
2022-10-02 20:52:29 +02:00
2022-10-04 11:07:14 +02:00
use como_domain::item::{requests::CreateItemDto, responses::CreatedItemDto};
2022-10-04 11:06:48 +02:00
use como_infrastructure::register::ServiceRegister;
2022-10-04 11:07:14 +02:00
2022-10-04 12:06:00 +02:00
pub type ComoSchema = Schema<QueryRoot, MutationRoot, EmptySubscription>;
2022-10-02 14:15:45 +02:00
pub struct MutationRoot;
#[Object]
impl MutationRoot {
async fn login(
&self,
ctx: &Context<'_>,
username: String,
password: String,
) -> anyhow::Result<bool> {
2022-10-04 11:06:48 +02:00
let service_register = ctx.data_unchecked::<ServiceRegister>();
2022-10-02 14:15:45 +02:00
2022-10-04 11:06:48 +02:00
let valid = service_register
.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> {
2022-10-04 11:06:48 +02:00
let service_register = ctx.data_unchecked::<ServiceRegister>();
2022-10-02 14:15:45 +02:00
2022-10-04 11:06:48 +02:00
let user_id = service_register
.user_service
.add_user(username, password)
.await?;
2022-10-02 14:15:45 +02:00
2022-10-02 14:16:13 +02:00
Ok(user_id)
2022-10-02 14:15:45 +02:00
}
2022-10-03 23:00:31 +02:00
async fn create_item(
&self,
ctx: &Context<'_>,
item: CreateItemDto,
) -> anyhow::Result<CreatedItemDto> {
2022-10-04 11:06:48 +02:00
let services_register = ctx.data_unchecked::<ServiceRegister>();
let created_item = services_register.item_service.add_item(item).await?;
Ok(created_item)
2022-10-03 23:00:31 +02:00
}
2022-10-02 14:15:45 +02:00
}
2022-10-02 12:12:08 +02:00
pub struct QueryRoot;
#[Object]
impl QueryRoot {
2022-10-04 11:07:14 +02:00
async fn hello(&self, _ctx: &Context<'_>) -> String {
2022-10-04 11:06:48 +02:00
"hello".into()
}
2022-10-02 12:12:08 +02:00
}