@@ -140,6 +140,52 @@ async fn realtime_emitter(req: HttpRequest, stream: web::Payload) -> Result<Http
140
140
resp
141
141
}
142
142
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
+
143
189
#[ get( "/applications" ) ]
144
190
async fn get_applications ( req : HttpRequest ) -> impl Responder {
145
191
let token = auth:: get_bearer_token ( req) ;
@@ -750,6 +796,7 @@ async fn main() -> std::io::Result<()> {
750
796
. service ( write_data)
751
797
. service ( delete_data)
752
798
. service ( update_data)
799
+ . service ( get_application_preview)
753
800
. service ( get_applications)
754
801
. service ( delete_application)
755
802
. service ( create_application) ;
0 commit comments