use sea_orm::*; use crate::database::entities::{server, prelude::*}; use anyhow::Result; use uuid::Uuid; #[derive(Clone)] pub struct ServerRepository { db: DatabaseConnection, } #[allow(dead_code)] impl ServerRepository { pub fn new(db: DatabaseConnection) -> Self { Self { db } } pub async fn create(&self, server_data: server::CreateServerDto) -> Result { let server = server::ActiveModel::from(server_data); let result = Server::insert(server).exec(&self.db).await?; Server::find_by_id(result.last_insert_id) .one(&self.db) .await? .ok_or_else(|| anyhow::anyhow!("Failed to retrieve created server")) } pub async fn find_all(&self) -> Result> { Ok(Server::find().all(&self.db).await?) } pub async fn find_by_id(&self, id: Uuid) -> Result> { Ok(Server::find_by_id(id).one(&self.db).await?) } pub async fn find_by_name(&self, name: &str) -> Result> { Ok(Server::find() .filter(server::Column::Name.eq(name)) .one(&self.db) .await?) } pub async fn find_by_hostname(&self, hostname: &str) -> Result> { Ok(Server::find() .filter(server::Column::Hostname.eq(hostname)) .one(&self.db) .await?) } pub async fn find_by_status(&self, status: &str) -> Result> { Ok(Server::find() .filter(server::Column::Status.eq(status)) .all(&self.db) .await?) } pub async fn update(&self, id: Uuid, server_data: server::UpdateServerDto) -> Result { let server = Server::find_by_id(id) .one(&self.db) .await? .ok_or_else(|| anyhow::anyhow!("Server not found"))?; let updated_server = server.apply_update(server_data); Ok(updated_server.update(&self.db).await?) } pub async fn delete(&self, id: Uuid) -> Result { let result = Server::delete_by_id(id).exec(&self.db).await?; Ok(result.rows_affected > 0) } pub async fn get_grpc_endpoint(&self, id: Uuid) -> Result { let server = self.find_by_id(id).await? .ok_or_else(|| anyhow::anyhow!("Server not found"))?; Ok(server.get_grpc_endpoint()) } pub async fn get_all(&self) -> Result> { Ok(Server::find().all(&self.db).await?) } pub async fn count(&self) -> Result { let count = Server::find().count(&self.db).await?; Ok(count) } }