Files
azure-database-tui/src/services/profiles.rs
T
Louis Frei 8dda5dcf23
validate / macos-arm64 (push) Canceled after 0s
validate / windows-x86_64 (push) Canceled after 0s
feat: current state
2026-08-13 20:18:30 +02:00

43 lines
1.5 KiB
Rust

use crate::{
domain::profile::{ConnectionDraft, ConnectionProfile, ProfileFile, SecretReference},
error::{AppError, SafeError},
platform::profile_file::ProfileRepository,
};
pub struct ProfileService<R: ProfileRepository> {
repository: R,
}
impl<R: ProfileRepository> ProfileService<R> {
pub fn new(repository: R) -> Self {
Self { repository }
}
pub async fn create(&self, draft: ConnectionDraft) -> Result<ConnectionProfile, AppError> {
let draft = draft
.validate()
.map_err(|_| AppError(SafeError::input("Profile details are invalid")))?;
let id = crate::domain::ids::ProfileId::new();
let reference = if draft.save_secret {
Some(SecretReference {
service: "azure-database-tui".to_owned(),
account: format!("profile/{id}"),
})
} else {
None
};
let profile = ConnectionProfile {
id,
azure_resource_id: crate::domain::ids::AzureResourceId::new(draft.azure_resource_id)
.map_err(|_| AppError(SafeError::input("Profile details are invalid")))?,
server_host: draft.server_host,
port: draft.port,
database: draft.database,
username: draft.username,
tls: draft.tls,
secret_reference: reference,
};
let mut file: ProfileFile = self.repository.load().await?;
file.profiles.push(profile.clone());
self.repository.save(&file).await?;
Ok(profile)
}
}