Skip to content

Commit

Permalink
feat(prime-domain): added canonical impl
Browse files Browse the repository at this point in the history
  • Loading branch information
johnbchron committed Nov 25, 2024
1 parent 2928edb commit 963bff9
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 610 deletions.
610 changes: 10 additions & 600 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ db = { git = "https://github.com/rambit-systems/rambit" }
dvf = { git = "https://github.com/rambit-systems/rambit" }
hex = { git = "https://github.com/rambit-systems/rambit" }
model = { git = "https://github.com/rambit-systems/rambit" }
repos = { git = "https://github.com/rambit-systems/rambit" }

# async
tokio = { version = "1", features = ["full"] }
Expand Down
18 changes: 18 additions & 0 deletions crates/models/src/artifact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,21 @@ impl Model for Artifact {

fn id(&self) -> ArtifactRecordId { self.id }
}

/// A request to create a new [`Artifact`].
pub struct ArtifactCreateRequest {
/// The artifact's path.
pub path: ArtifactPath,
/// The artifact's compression status.
pub size: dvf::CompressionStatus,
}

impl From<ArtifactCreateRequest> for Artifact {
fn from(input: ArtifactCreateRequest) -> Self {
Self {
id: ArtifactRecordId::default(),
path: input.path,
size: input.size,
}
}
}
15 changes: 15 additions & 0 deletions crates/models/src/photo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,18 @@ impl Model for Photo {

fn id(&self) -> PhotoRecordId { self.id }
}

/// A request to create a new [`Photo`].
pub struct PhotoCreateRequest {
/// The photo's artifacts.
pub artifacts: PhotoArtifacts,
}

impl From<PhotoCreateRequest> for Photo {
fn from(input: PhotoCreateRequest) -> Self {
Self {
id: PhotoRecordId::default(),
artifacts: input.artifacts,
}
}
}
2 changes: 1 addition & 1 deletion crates/prime-domain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ edition = "2021"

[dependencies]
models = { path = "../models" }
repos = { path = "../repos" }

crunch.workspace = true
hex.workspace = true
repos.workspace = true

tracing.workspace = true

Expand Down
65 changes: 65 additions & 0 deletions crates/prime-domain/src/canonical.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use hex::health;
use models::{Photo, PhotoCreateRequest, PhotoRecordId};
use repos::FetchModelError;

use crate::PrimeDomainService;

/// The canonical implementation of [`PrimeDomainService`].
pub struct PrimeDomainServiceCanonical<
PR: repos::ModelRepository<
Model = Photo,
ModelCreateRequest = PhotoCreateRequest,
CreateError = repos::CreateModelError,
>,
> {
photo_repo: PR,
}

impl<PR> PrimeDomainServiceCanonical<PR>
where
PR: repos::ModelRepository<
Model = Photo,
ModelCreateRequest = PhotoCreateRequest,
CreateError = repos::CreateModelError,
>,
{
/// Creates a new [`PrimeDomainServiceCanonical`] with the given photo
/// repository.
pub fn new(photo_repo: PR) -> Self { Self { photo_repo } }
}

#[async_trait::async_trait]
impl<PR> PrimeDomainService for PrimeDomainServiceCanonical<PR>
where
PR: repos::ModelRepository<
Model = Photo,
ModelCreateRequest = PhotoCreateRequest,
CreateError = repos::CreateModelError,
>,
{
async fn fetch_photo_by_id(
&self,
id: PhotoRecordId,
) -> Result<Option<Photo>, FetchModelError> {
self.photo_repo.fetch_model_by_id(id).await
}
}

#[async_trait::async_trait]
impl<PR> health::HealthReporter for PrimeDomainServiceCanonical<PR>
where
PR: repos::ModelRepository<
Model = Photo,
ModelCreateRequest = PhotoCreateRequest,
CreateError = repos::CreateModelError,
>,
{
fn name(&self) -> &'static str { stringify!(PrimeDomainServiceCanonical) }
async fn health_check(&self) -> health::ComponentHealth {
health::AdditiveComponentHealth::from_futures(vec![self
.photo_repo
.health_report()])
.await
.into()
}
}
11 changes: 4 additions & 7 deletions crates/prime-domain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,17 @@ use std::sync::Arc;

pub use hex;
use hex::Hexagonal;
use models::{Artifact, ArtifactRecordId, Photo, PhotoRecordId};
use repos::db::FetchModelError;
use models::{Photo, PhotoRecordId};
use repos::FetchModelError;

pub use self::canonical::*;

/// A dynamic [`PrimeDomainService`] trait object.
pub type DynPrimeDomainService = Arc<Box<dyn PrimeDomainService>>;

/// The prime domain service trait.
#[async_trait::async_trait]
pub trait PrimeDomainService: Hexagonal {
/// Fetch an [`Artifact`] by its ID.
async fn fetch_cache_by_id(
&self,
id: ArtifactRecordId,
) -> Result<Option<Artifact>, FetchModelError>;
/// Fetch a [`Photo`] by its ID.
async fn fetch_photo_by_id(
&self,
Expand Down
2 changes: 1 addition & 1 deletion crates/repos/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Repositories for use in services.
use db::{FetchModelByIndexError, FetchModelError};
pub use db::{CreateModelError, FetchModelByIndexError, FetchModelError};
use hex::Hexagonal;
use miette::Result;
use models::EitherSlug;
Expand Down

0 comments on commit 963bff9

Please sign in to comment.