shindo_coding_utils 0.3.0

A utils crates which will be used in various micro-services
Documentation
use async_trait::async_trait;
use sea_orm::*;
use serde::{Deserialize, Serialize};
use std::sync::Arc;

use crate::schemas::investor::{ActiveModel, Entity, Model};

#[derive(Deserialize, Serialize, Debug)]
pub struct Investor {
    pub user_id: String,
    pub username: String,
}

#[async_trait]
pub trait InvestorRepository: Send + Sync {
    async fn insert_investor(&self, investor: Investor)
    -> Result<InsertResult<ActiveModel>, DbErr>;

    async fn find_all_investors(&self) -> Result<Vec<Model>, DbErr>;
}

pub struct SeaOrmInvestorRepository {
    db_conn: Arc<DatabaseConnection>,
}

impl SeaOrmInvestorRepository {
    pub fn new(db_conn: Arc<DatabaseConnection>) -> Self {
        Self { db_conn }
    }
}

#[async_trait]
impl InvestorRepository for SeaOrmInvestorRepository {
    async fn insert_investor(
        &self,
        investor: Investor,
    ) -> Result<InsertResult<ActiveModel>, DbErr> {
        let active_model = ActiveModel {
            user_id: Set(investor.user_id),
            username: Set(investor.username),
            ..Default::default()
        };

        let insert_result = Entity::insert(active_model).exec(&*self.db_conn).await?;
        Ok(insert_result)
    }

    async fn find_all_investors(&self) -> Result<Vec<Model>, DbErr> {
        Entity::find().all(&*self.db_conn).await
    }
}