fplus_database/core/collections/
rkh.rs1use actix_web::web;
2use mongodb::{Client, Collection};
3use serde::{Serialize, Deserialize};
4use std::sync::Mutex;
5use anyhow::Result;
6
7use crate::core::common::get_collection;
8
9const COLLECTION_NAME: &str = "rkh";
10
11#[derive(Debug, Serialize, Deserialize)]
12pub struct RootKeyHolder {
13 pub github_handle: String,
14}
15
16pub async fn find(state: web::Data<Mutex<Client>>) -> Result<Vec<RootKeyHolder>> {
17 let rkh_collection: Collection<RootKeyHolder> = get_collection(state, COLLECTION_NAME).await?;
18 let mut cursor = rkh_collection.find(None, None).await?;
19 let mut ret = vec![];
20 while let Ok(result) = cursor.advance().await {
21 if result {
22 let d = match cursor.deserialize_current() {
23 Ok(d) => d,
24 Err(_) => { continue; }
25 };
26 ret.push(d);
27 } else {
28 break;
29 }
30 }
31 Ok(ret)
32}
33
34pub async fn insert(state: web::Data<Mutex<Client>>, rkh: RootKeyHolder) -> Result<()> {
35 let rkh_collection: Collection<RootKeyHolder> = get_collection(state, COLLECTION_NAME).await?;
36 rkh_collection.insert_one(rkh, None).await?;
37 Ok(())
38}