Skip to content

Commit

Permalink
its: Added issue_management_context_use_case
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jan 6, 2022
1 parent 0148836 commit 9b6d8b8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 25 deletions.
15 changes: 9 additions & 6 deletions its/src/main.rs
@@ -1,18 +1,21 @@
use use_case::{issue_management_context_use_case, IssueManagementContextCommand};

use crate::{
entity::IssueTitle,
use_case::{create_issue_workflow, CreateIssue},
use_case::{create_issue_use_case, CreateIssue},
};

mod entity;
mod use_case;

#[argopt::subcmd(name = "issue-create")]
fn issue_create(#[opt(long = "title")] title: Option<String>) {
let title = IssueTitle::try_from(title.unwrap_or_default()).unwrap();
let command = CreateIssue::new(title);
let event = create_issue_workflow(command);
fn issue_create(#[opt(long = "title")] title: Option<String>) -> anyhow::Result<()> {
let issue_title = IssueTitle::try_from(title.unwrap_or_default()).unwrap();
let command = IssueManagementContextCommand::CreateIssue(CreateIssue { issue_title });
let event = issue_management_context_use_case(command)?;
println!("issue created : {:?}", event);
Ok(())
}

#[argopt::cmd_group(commands = [issue_create])]
fn main() {}
fn main() -> anyhow::Result<()> {}
64 changes: 45 additions & 19 deletions its/src/use_case.rs
@@ -1,21 +1,31 @@
use crate::entity::{Issue, IssueId, IssueNumber, IssueTitle};
use crate::entity::{
Issue, IssueAggregate, IssueAggregateCommand, IssueAggregateCreateIssue, IssueAggregateError,
IssueAggregateEvent, IssueCreated, IssueNumber, IssueTitle,
};
use limited_date_time::Instant;
use thiserror::Error;

#[derive(Debug)]
pub enum IssueManagementContextCommand {
CreateIssue(CreateIssue),
}

#[derive(Debug)]
pub struct CreateIssue {
issue_title: IssueTitle,
pub issue_title: IssueTitle,
}

impl CreateIssue {
pub fn new(issue_title: IssueTitle) -> Self {
Self { issue_title }
}
#[derive(Debug)]
pub enum IssueManagementContextEvent {
IssueCreated(IssueCreated),
}

#[derive(Clone, Debug)]
pub struct IssueCreated {
at: Instant,
issue: Issue,
#[derive(Debug, Error)]
pub enum IssueManagementContextError {
#[error("IssueAggregate")]
IssueAggregate(IssueAggregateError),
#[error("Unknown")]
Unknown,
}

#[derive(Debug, Default)]
Expand All @@ -31,27 +41,43 @@ impl IssueRepository {
IssueNumber::start_number()
}
}
}

pub fn save(&self, _events: IssueCreated) -> anyhow::Result<()> {
// TODO
Ok(())
pub fn issue_management_context_use_case(
command: IssueManagementContextCommand,
) -> Result<IssueManagementContextEvent, IssueManagementContextError> {
match command {
IssueManagementContextCommand::CreateIssue(command) => {
let event = create_issue_use_case(command)?;
Ok(IssueManagementContextEvent::IssueCreated(event))
}
}
}

pub fn create_issue_workflow(create_issue: CreateIssue) -> anyhow::Result<IssueCreated> {
pub fn create_issue_use_case(
command: CreateIssue,
) -> Result<IssueCreated, IssueManagementContextError> {
let issue_repository = IssueRepository::default(); // TODO: dependency

// io
let issue_number = issue_repository.next_issue_number();
let at = Instant::now();

// pure
let issue_id = IssueId::new(issue_number);
let issue = Issue::new(issue_id, create_issue.issue_title);
let event = IssueCreated { at, issue };
let event =
IssueAggregate::transaction(IssueAggregateCommand::Create(IssueAggregateCreateIssue {
issue_number,
issue_title: command.issue_title,
at,
}))
.map_err(IssueManagementContextError::IssueAggregate)?;

// io
issue_repository.save(event.clone())?;
// TODO: save issue

Ok(event)
if let IssueAggregateEvent::Created(event) = event {
Ok(event)
} else {
unreachable!()
}
}

0 comments on commit 9b6d8b8

Please sign in to comment.