Skip to content

Commit

Permalink
its: Added IssueBlockLinkAggregate::from_events
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Mar 6, 2022
1 parent 3335cdf commit b231c89
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
1 change: 0 additions & 1 deletion its/crates/domain/src/domain/aggregate/issue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use crate::{

use super::IssueBlockLinkAggregate;
use super::IssueBlockLinkAggregateError;
use super::IssueBlockLinkAggregateResult;

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct IssueAggregate {
Expand Down
57 changes: 40 additions & 17 deletions its/crates/domain/src/domain/aggregate/issue_block_link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,31 @@ pub struct IssueBlockLinkAggregate {
version: Version,
}

pub type IssueBlockLinkAggregateResult =
Result<(IssueBlockLinkAggregate, IssueBlockLinkAggregateEvent), IssueBlockLinkAggregateError>;

impl IssueBlockLinkAggregate {
pub fn from_event(event: IssueBlocked) -> Result<Self, IssueBlockLinkAggregateError> {
Self::block(
event.at(),
event.issue_id().clone(),
event.blocked_issue_id().clone(),
)
.map(|issue_block_link| issue_block_link.truncate_events())
}

fn truncate_events(&self) -> Self {
Self {
events: vec![],
issue_block_link: self.issue_block_link.clone(),
version: self.version.clone(),
pub fn from_events(
events: &[IssueBlockLinkAggregateEvent],
) -> Result<Self, IssueBlockLinkAggregateError> {
let first_event = match events.first() {
Some(event) => match event {
IssueBlockLinkAggregateEvent::Blocked(event) => Ok(event),
IssueBlockLinkAggregateEvent::Unblocked => {
Err(IssueBlockLinkAggregateError::InvalidEventSequence)
}
},
None => Err(IssueBlockLinkAggregateError::InvalidEventSequence),
}?;
let issue_block_link = Self::from_event(first_event)?;
for event in events.iter().skip(1) {
match event {
IssueBlockLinkAggregateEvent::Blocked(_) => {
return Err(IssueBlockLinkAggregateError::InvalidEventSequence);
}
IssueBlockLinkAggregateEvent::Unblocked => {
todo!()
}
}
}
Ok(issue_block_link)
}

pub fn block(
Expand Down Expand Up @@ -76,6 +82,23 @@ impl IssueBlockLinkAggregate {
version: updated_version,
})
}

fn from_event(event: &IssueBlocked) -> Result<Self, IssueBlockLinkAggregateError> {
Self::block(
event.at(),
event.issue_id().clone(),
event.blocked_issue_id().clone(),
)
.map(Self::truncate_events)
}

fn truncate_events(self) -> Self {
Self {
events: vec![],
issue_block_link: self.issue_block_link,
version: self.version,
}
}
}

#[cfg(test)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use thiserror::Error;
pub enum IssueBlockLinkAggregateError {
#[error("Block")]
Block,
#[error("InvalidEventSequence")]
InvalidEventSequence,
#[error("NextVersion")]
NoNextVersion,
}

0 comments on commit b231c89

Please sign in to comment.