fplus_database/core/collections/
notary.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 = "notary";
10
11#[derive(Debug, Serialize, Deserialize)]
12pub struct Notary {
13 pub github_handle: String,
14 pub on_chain_address: String,
15}
16
17
18pub async fn find(state: web::Data<Mutex<Client>>) -> Result<Vec<Notary>> {
19 let notary_collection: Collection<Notary> = get_collection(state, COLLECTION_NAME).await?;
20 let mut cursor = notary_collection.find(None, None).await?;
21 let mut ret = vec![];
22 while let Ok(result) = cursor.advance().await {
23 if result {
24 let d = match cursor.deserialize_current() {
25 Ok(d) => d,
26 Err(_) => { continue; }
27 };
28 ret.push(d);
29 } else {
30 break;
31 }
32 }
33 Ok(ret)
34}
35
36pub async fn insert(state: web::Data<Mutex<Client>>, notary: Notary) -> Result<()> {
37 let notary_collection: Collection<Notary> = get_collection(state, COLLECTION_NAME).await?;
38 notary_collection.insert_one(notary, None).await?;
39 Ok(())
40}