zero4rs 2.0.0

zero4rs is a powerful, pragmatic, and extremely fast web framework for Rust
Documentation
use mongodb::bson::doc;

use crate::graphql::schema::{
    // CreateOwner, CreateProject, FetchOwner, FetchProject,
    Owner,
    Project,
};

use crate::core::mongo::MongoClient;

// use crate::core::error2::Error;
use crate::core::error2::Result;

pub const GRAPHQL_DB: &str = "graphql";

// Owners logic
pub async fn create_owner(client: &MongoClient, new_owner: Owner) -> Result<Option<Owner>> {
    let new_doc = Owner {
        id: None,
        name: new_owner.name.clone(),
        email: new_owner.email.clone(),
        phone: new_owner.phone.clone(),
    };

    let id = client.insert_one(GRAPHQL_DB, "owner", new_doc).await?;

    client.find_one(GRAPHQL_DB, "owner", &id).await
}

pub async fn get_owners(client: &MongoClient) -> Result<Vec<Owner>> {
    client.find(GRAPHQL_DB, "owner", doc! {}).await
}

pub async fn single_owner(client: &MongoClient, id: &str) -> Result<Option<Owner>> {
    client.find_one(GRAPHQL_DB, "owner", id).await
}

// Project logics
pub async fn create_project(client: &MongoClient, new_project: Project) -> Result<Option<Project>> {
    let new_doc = Project {
        id: None,
        owner_id: new_project.owner_id.clone(),
        name: new_project.name.clone(),
        description: new_project.description.clone(),
        status: new_project.status,
    };

    let id = client.insert_one(GRAPHQL_DB, "project", new_doc).await?;

    client.find_one(GRAPHQL_DB, "project", &id).await
}

pub async fn get_projects(client: &MongoClient) -> Result<Vec<Project>> {
    client.find(GRAPHQL_DB, "project", doc! {}).await
}

pub async fn single_project(client: &MongoClient, id: &str) -> Result<Option<Project>> {
    client.find_one(GRAPHQL_DB, "project", id).await
}