feat: update
All checks were successful
continuous-integration/drone/push Build is passing

Signed-off-by: kjuulh <contact@kjuulh.io>
This commit is contained in:
2024-04-10 20:50:48 +02:00
parent ec8d0b5ebc
commit 74ea9ddf79
35 changed files with 5 additions and 11 deletions

View File

@@ -0,0 +1,4 @@
pub mod mutation;
pub mod query;
pub mod schema;
pub mod subscription;

View File

@@ -0,0 +1,38 @@
use std::sync::Arc;
use async_graphql::{Context, Object, Result, SimpleObject, ID};
use scel_core::{services::Download, App};
pub struct MutationRoot;
#[derive(SimpleObject)]
struct RequestDownloadResponse {
id: ID,
}
#[Object]
impl MutationRoot {
async fn request_download(
&self,
ctx: &Context<'_>,
download_link: String,
) -> Result<RequestDownloadResponse> {
let app = ctx.data_unchecked::<Arc<App>>();
let download = app
.download_service
.clone()
.add_download(Download {
id: None,
link: download_link,
progress: None,
file_name: None,
})
.await
.unwrap();
Ok(RequestDownloadResponse {
id: download.id.unwrap().into(),
})
}
}

View File

@@ -0,0 +1,32 @@
use std::sync::Arc;
use async_graphql::{Context, Object, Result, SimpleObject, ID};
use scel_core::App;
#[derive(SimpleObject, Clone)]
pub struct Download {
pub id: ID,
pub link: String,
pub progress: Option<u32>,
pub file_name: Option<String>,
}
pub struct QueryRoot;
#[Object]
impl QueryRoot {
async fn get_download(&self, ctx: &Context<'_>, id: ID) -> Result<Option<Download>> {
let app = ctx.data_unchecked::<Arc<App>>();
match app.download_service.get_download(id.to_string()).await {
Ok(Some(d)) => Ok(Some(Download {
id: ID::from(d.id.expect("ID could not be found")),
progress: None,
link: d.link,
file_name: None,
})),
Ok(None) => Ok(None),
Err(e) => Err(e.into()),
}
}
}

View File

@@ -0,0 +1,5 @@
use async_graphql::Schema;
use super::{mutation::MutationRoot, query::QueryRoot, subscription::SubscriptionRoot};
pub type ScelSchema = Schema<QueryRoot, MutationRoot, SubscriptionRoot>;

View File

@@ -0,0 +1,49 @@
use std::sync::Arc;
use async_graphql::{
async_stream::stream, futures_util::Stream, Context, Object, Subscription, ID,
};
use scel_core::App;
use super::query::Download;
pub struct SubscriptionRoot;
struct DownloadChanged {
download: Download,
}
#[Object]
impl DownloadChanged {
async fn download(&self) -> Download {
self.download.clone()
}
}
#[Subscription]
impl SubscriptionRoot {
async fn get_download(&self, ctx: &Context<'_>, id: ID) -> impl Stream<Item = DownloadChanged> {
let app = ctx.data_unchecked::<Arc<App>>();
let mut stream = app
.download_service
.subscribe_download(id.to_string())
.await;
stream! {
while stream.changed().await.is_ok() {
let next_download = (*stream.borrow()).clone();
let id = ID::from(next_download.id.unwrap());
yield DownloadChanged {
download: Download {
id: id,
link: next_download.link,
file_name: next_download.file_name,
progress: next_download.progress,
}
}
}
}
}
}