use crate::{ domain::profile::{ConnectionDraft, ConnectionProfile, ProfileFile, SecretReference}, error::{AppError, SafeError}, platform::profile_file::ProfileRepository, }; pub struct ProfileService { repository: R, } impl ProfileService { pub fn new(repository: R) -> Self { Self { repository } } pub async fn create(&self, draft: ConnectionDraft) -> Result { 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) } }