Skip to content

Commit 23519b7

Browse files
committed
add /preview for applications
1 parent 55510ed commit 23519b7

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed

src/main.rs

+47
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,52 @@ async fn realtime_emitter(req: HttpRequest, stream: web::Payload) -> Result<Http
140140
resp
141141
}
142142

143+
144+
#[get("/application/{application_id}/preview")]
145+
async fn get_application_preview(req: HttpRequest, application_id: web::Path<String>) -> impl Responder {
146+
let token = auth::get_bearer_token(req);
147+
148+
if token.is_none() {
149+
return HttpResponse::Unauthorized().body("");
150+
}
151+
152+
let database = DATABASE_CLIENT
153+
.lock()
154+
.unwrap();
155+
156+
let database = database.as_ref();
157+
let database = database.unwrap();
158+
159+
let document_id = application_id.to_string();
160+
161+
let data = database
162+
.database("klerk")
163+
.collection::<InsertedData>("data")
164+
.find(doc! {
165+
"application_id": document_id
166+
})
167+
.skip(0)
168+
.sort(doc! {
169+
"time": -1
170+
})
171+
.await;
172+
173+
if data.is_err() {
174+
return HttpResponse::Unauthorized().body("");
175+
}
176+
177+
let mut data = data.unwrap();
178+
let mut pipe = vec![];
179+
180+
while data.advance().await.unwrap() {
181+
let document = data.current();
182+
183+
pipe.push(document.to_owned());
184+
}
185+
186+
return HttpResponse::Ok().json(pipe);
187+
}
188+
143189
#[get("/applications")]
144190
async fn get_applications(req: HttpRequest) -> impl Responder {
145191
let token = auth::get_bearer_token(req);
@@ -750,6 +796,7 @@ async fn main() -> std::io::Result<()> {
750796
.service(write_data)
751797
.service(delete_data)
752798
.service(update_data)
799+
.service(get_application_preview)
753800
.service(get_applications)
754801
.service(delete_application)
755802
.service(create_application);

0 commit comments

Comments
 (0)