From f99bd9b15cfd15643dd435c17c25bd4ce26d0371 Mon Sep 17 00:00:00 2001 From: "Shkar T. Noori" Date: Sun, 28 Apr 2024 11:17:50 +0300 Subject: [PATCH] Add GetAllTransitionDefinitions and GetAllowedTransitions methods to IWorkflow.cs --- .../Abstractions/IWorkflow.cs | 18 +++++++++++ src/DIT.Workflower/Query.cs | 13 ++++++++ src/DIT.Workflower/WorkflowDefinition.cs | 31 +++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 src/DIT.Workflower/Query.cs diff --git a/src/DIT.Workflower.DependencyInjection/Abstractions/IWorkflow.cs b/src/DIT.Workflower.DependencyInjection/Abstractions/IWorkflow.cs index 598a95c..fec6118 100644 --- a/src/DIT.Workflower.DependencyInjection/Abstractions/IWorkflow.cs +++ b/src/DIT.Workflower.DependencyInjection/Abstractions/IWorkflow.cs @@ -10,6 +10,15 @@ public interface IWorkflow string Reference => $"{Id}.v{Version}"; + /// + /// Retrieves all transition definitions for the workflow. + /// + /// The type of the workflow state. + /// The type of the workflow command. + /// The type of the workflow context. + /// A list of transition definitions. + public List> GetAllTransitionDefinitions(); + /// /// Gets a list of allowed transitions without any condition checks. /// @@ -24,4 +33,13 @@ public interface IWorkflow /// The incoming state /// A list of available transitions for the current context public List> GetAllowedTransitions(TContext context, TState from); + + /// + /// Retrieves a list of allowed transitions based on the provided request. + /// + /// The type of the workflow state. + /// The type of the workflow command. + /// The request object containing the necessary information. + /// An enumerable of allowed transitions. + public IEnumerable> GetAllowedTransitions(ListTransitionsRequest request); } diff --git a/src/DIT.Workflower/Query.cs b/src/DIT.Workflower/Query.cs new file mode 100644 index 0000000..0b071ab --- /dev/null +++ b/src/DIT.Workflower/Query.cs @@ -0,0 +1,13 @@ +namespace DIT.Workflower; + + +public sealed class ListTransitionsRequest +{ + public TState? From { get; set; } + + public TState? To { get; set; } + + public TCommand? Command { get; set; } + + public TContext? Context { get; set; } +} diff --git a/src/DIT.Workflower/WorkflowDefinition.cs b/src/DIT.Workflower/WorkflowDefinition.cs index 1b55ded..45ce199 100644 --- a/src/DIT.Workflower/WorkflowDefinition.cs +++ b/src/DIT.Workflower/WorkflowDefinition.cs @@ -12,6 +12,8 @@ public record WorkflowDefinition _transitions = transitions; } + public List> GetAllTransitionDefinitions() => _transitions; + /// /// Lists all allowed transitions from current state for the given context. /// @@ -33,4 +35,33 @@ public record WorkflowDefinition return query.Select(x => x.ToTransition()).ToList(); } + + + public IEnumerable> GetAllowedTransitions(ListTransitionsRequest request) + { + var query = _transitions.AsQueryable(); + + if (request.From is TState from) + { + query = query.Where(doc => doc.From.Equals(from)); + } + + if (request.To is TState to) + { + query = query.Where(doc => doc.To.Equals(to)); + } + + if (request.Command is TCommand command) + { + query = query.Where(doc => doc.Command.Equals(command)); + } + + if (request.Context is not null) + { + query = query.Where(doc => doc.Conditions == null || !doc.Conditions.Any(cond => !cond(request.Context))); + } + + return query.Select(x => x.ToTransition()); + } + }