fplus_database/core/collections/
logs.rs

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