37 lines
1.1 KiB
Rust
37 lines
1.1 KiB
Rust
use std::sync::Arc;
|
|
|
|
use sea_orm::{DatabaseConnection, prelude::*};
|
|
|
|
use crate::errors::service_error::ServiceError;
|
|
|
|
#[async_trait::async_trait]
|
|
pub trait ServerStateStore: Send + Sync {
|
|
async fn is_server_initialized(&self) -> Result<bool, ServiceError>;
|
|
}
|
|
|
|
pub struct ServerStateService {
|
|
connection: Arc<DatabaseConnection>,
|
|
}
|
|
|
|
impl ServerStateService {
|
|
pub fn new(connection: Arc<DatabaseConnection>) -> Self {
|
|
Self { connection }
|
|
}
|
|
}
|
|
|
|
#[async_trait::async_trait]
|
|
impl ServerStateStore for ServerStateService {
|
|
async fn is_server_initialized(&self) -> Result<bool, ServiceError> {
|
|
// For example, check if any admin user exists to determine if the server is initialized
|
|
let admin_exists = database::generated::entities::user::Entity::find()
|
|
.filter(database::generated::entities::user::Column::IsAdmin.eq(true))
|
|
.filter(database::generated::entities::user::Column::IsActive.eq(true))
|
|
.one(&*self.connection)
|
|
.await
|
|
.map_err(ServiceError::from)?
|
|
.is_some();
|
|
|
|
Ok(admin_exists)
|
|
}
|
|
}
|