kjuulh f9f280278f
All checks were successful
continuous-integration/drone/push Build is passing
feat: with domain events
Signed-off-by: kjuulh <contact@kjuulh.io>
2024-02-12 23:13:34 +01:00

83 lines
2.0 KiB
Rust

use serde::Serialize;
#[mockall_double::double]
use crate::services::file_store::FileStore;
#[mockall_double::double]
use super::domain_events::DomainEvents;
use self::models::{ArtifactID, CommitArtifact};
pub struct ReleaseManager {
file_store: FileStore,
domain_events: DomainEvents,
}
impl ReleaseManager {
pub fn new(file_store: FileStore, domain_events: DomainEvents) -> Self {
Self {
file_store,
domain_events,
}
}
pub async fn commit_artifact(&self, request: CommitArtifact) -> anyhow::Result<ArtifactID> {
tracing::debug!("committing artifact: {:?}", request);
let artifact_id = ArtifactID::new();
self.file_store
.upload_files(artifact_id.clone(), Vec::new())
.await?;
self.domain_events
.publish_event(&serde_json::to_string(&CommittedArtifactEvent {
artifact_id: artifact_id.to_string(),
})?)
.await?;
Ok(artifact_id)
}
}
#[derive(Serialize)]
pub struct CommittedArtifactEvent {
artifact_id: String,
}
pub mod extensions;
pub mod models;
#[cfg(test)]
mod test {
use crate::services::domain_events::MockDomainEvents;
use crate::services::file_store::MockFileStore;
use super::*;
#[tokio::test]
async fn generated_artifact_id() -> anyhow::Result<()> {
let mut file_store = MockFileStore::default();
file_store
.expect_upload_files()
.times(1)
.returning(|_, _| Ok(()));
let mut domain_events = MockDomainEvents::default();
domain_events
.expect_publish_event()
.times(1)
.returning(|_| Ok(()));
let releaser_manager = ReleaseManager::new(file_store, domain_events);
releaser_manager
.commit_artifact(CommitArtifact {
app: "app".into(),
branch: "branch".into(),
})
.await?;
Ok(())
}
}