use models::users::*;

use crate::AppService;
use crate::error::AppError;
use sea_orm::*;
use uuid::Uuid;
impl AppService {
    pub async fn utils_find_user_by_username(
        &self,
        username: String,
    ) -> Result<user::Model, AppError> {
        user::Entity::find()
            .filter(user::Column::Username.eq(username))
            .one(&self.db)
            .await
            .ok()
            .flatten()
            .ok_or(AppError::UserNotFound)
    }
    pub async fn utils_find_user_by_uid(&self, uid: Uuid) -> Result<user::Model, AppError> {
        user::Entity::find_by_id(uid)
            .one(&self.db)
            .await
            .ok()
            .flatten()
            .ok_or(AppError::UserNotFound)
    }
    pub async fn utils_find_user_by_email(&self, email: String) -> Result<user::Model, AppError> {
        let user_email = user_email::Entity::find()
            .filter(user_email::Column::Email.eq(email))
            .one(&self.db)
            .await
            .ok()
            .flatten()
            .ok_or(AppError::UserNotFound)?;
        let user = user::Entity::find_by_id(user_email.user)
            .one(&self.db)
            .await
            .ok()
            .flatten()
            .ok_or(AppError::UserNotFound)?;
        Ok(user)
    }
}
