Skip to content

Commit 074fb83

Browse files
add update workflow endpoint (#153)
add update workflow endpoint Signed-off-by: Jack Schofield <[email protected]> Co-authored-by: Joe Batt <[email protected]>
1 parent 748c666 commit 074fb83

23 files changed

+1419
-397
lines changed

src/Common/Interfaces/IWorkflowService.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,12 @@ public interface IWorkflowService
2323
/// </summary>
2424
/// <param name="workflow">Workflow to create.</param>
2525
Task<string> CreateAsync(Workflow workflow);
26+
27+
/// <summary>
28+
/// Updates a workflow within the workflow repository.
29+
/// </summary>
30+
/// <param name="workflow">Workflow to Update.</param>
31+
/// <param name="id">Id of the workflow to Update.</param>
32+
Task<string?> UpdateAsync(Workflow workflow, string id);
2633
}
2734
}

src/Common/Services/WorkflowService.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,20 @@ public async Task<string> CreateAsync(Workflow workflow)
3434

3535
return await _workflowRepository.CreateAsync(workflow);
3636
}
37+
38+
public async Task<string?> UpdateAsync(Workflow workflow, string id)
39+
{
40+
Guard.Against.Null(workflow);
41+
Guard.Against.NullOrWhiteSpace(id);
42+
43+
var existingWorkflow = await _workflowRepository.GetByWorkflowIdAsync(id);
44+
45+
if (existingWorkflow is null)
46+
{
47+
return null;
48+
}
49+
50+
return await _workflowRepository.UpdateAsync(workflow, existingWorkflow);
51+
}
3752
}
3853
}

src/Database/Interfaces/IWorkflowRepository.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,5 +43,12 @@ public interface IWorkflowRepository
4343
/// </summary>
4444
/// <param name="workflow">Workflow object to create.</param>
4545
Task<string> CreateAsync(Workflow workflow);
46+
47+
/// <summary>
48+
/// Updates a workflow object and creates a new revision.
49+
/// </summary>
50+
/// <param name="workflow">Workflow object to create.</param>
51+
/// <param name="existingWorkflow">Existing Workflow object to update.</param>
52+
Task<string> UpdateAsync(Workflow workflow, WorkflowRevision existingWorkflow);
4653
}
4754
}

src/Database/WorkflowRepository.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,5 +127,23 @@ public async Task<string> CreateAsync(Workflow workflow)
127127

128128
return workflowRevision.WorkflowId;
129129
}
130+
131+
public async Task<string> UpdateAsync(Workflow workflow, WorkflowRevision existingWorkflow)
132+
{
133+
Guard.Against.Null(workflow, nameof(workflow));
134+
Guard.Against.Null(existingWorkflow, nameof(existingWorkflow));
135+
136+
var workflowRevision = new WorkflowRevision
137+
{
138+
Id = Guid.NewGuid().ToString(),
139+
WorkflowId = existingWorkflow.WorkflowId,
140+
Revision = ++existingWorkflow.Revision,
141+
Workflow = workflow
142+
};
143+
144+
await _workflowCollection.InsertOneAsync(workflowRevision);
145+
146+
return workflowRevision.WorkflowId;
147+
}
130148
}
131149
}

src/WorkflowManager/Controllers/WorkflowsController.cs

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache License 2.0
33

44
using System;
5+
using System.Net;
56
using System.Threading.Tasks;
67
using Microsoft.AspNetCore.Http;
78
using Microsoft.AspNetCore.Mvc;
@@ -64,7 +65,7 @@ public async Task<IActionResult> GetAsync([FromRoute] string id)
6465
{
6566
if (string.IsNullOrWhiteSpace(id) || !Guid.TryParse(id, out _))
6667
{
67-
this._logger.LogDebug($"{nameof(GetAsync)} - Failed to validate {nameof(id)}");
68+
_logger.LogDebug($"{nameof(GetAsync)} - Failed to validate {nameof(id)}");
6869

6970
return Problem($"Failed to validate {nameof(id)}, not a valid guid", $"/workflows/{id}", 400);
7071
}
@@ -91,7 +92,7 @@ public async Task<IActionResult> CreateAsync([FromBody] Workflow workflow)
9192
{
9293
if (!workflow.IsValid(out var validationErrors))
9394
{
94-
this._logger.LogDebug($"{nameof(CreateAsync)} - Failed to validate {nameof(workflow)}: {validationErrors}");
95+
_logger.LogDebug($"{nameof(CreateAsync)} - Failed to validate {nameof(workflow)}: {validationErrors}");
9596

9697
return Problem($"Failed to validate {nameof(workflow)}: {string.Join(", ", validationErrors)}", $"/workflows", 400);
9798
}
@@ -107,4 +108,48 @@ public async Task<IActionResult> CreateAsync([FromBody] Workflow workflow)
107108
return Problem($"Unexpected error occured: {e.Message}", $"/workflows", 500);
108109
}
109110
}
111+
112+
/// <summary>
113+
/// Updates a workflow and creates a new revision
114+
/// </summary>
115+
/// <param name="workflow">The Workflow.</param>
116+
/// <returns>The ID of the created Workflow.</returns>
117+
[HttpPut("{id}")]
118+
[ProducesResponseType(StatusCodes.Status201Created, Type = typeof(CreateWorkflowResponse))]
119+
[ProducesResponseType(StatusCodes.Status404NotFound)]
120+
[ProducesResponseType(StatusCodes.Status400BadRequest)]
121+
public async Task<IActionResult> UpdateAsync([FromBody] Workflow workflow, [FromRoute] string id)
122+
{
123+
if (string.IsNullOrWhiteSpace(id) || !Guid.TryParse(id, out _))
124+
{
125+
_logger.LogDebug($"{nameof(UpdateAsync)} - Failed to validate {nameof(id)}");
126+
127+
return Problem($"Failed to validate {nameof(id)}, not a valid guid", $"/workflows/{id}", (int)HttpStatusCode.BadRequest);
128+
}
129+
130+
if (!workflow.IsValid(out var validationErrors))
131+
{
132+
_logger.LogDebug($"{nameof(UpdateAsync)} - Failed to validate {nameof(workflow)}: {validationErrors}");
133+
134+
return Problem($"Failed to validate {nameof(workflow)}: {string.Join(", ", validationErrors)}", $"/workflows/{id}", (int)HttpStatusCode.BadRequest);
135+
}
136+
137+
try
138+
{
139+
var workflowId = await _workflowService.UpdateAsync(workflow, id);
140+
141+
if (workflowId == null)
142+
{
143+
_logger.LogDebug($"{nameof(UpdateAsync)} - Failed to find workflow with Id: {id}");
144+
145+
return NotFound($"Failed to find workflow with Id: {id}");
146+
}
147+
148+
return StatusCode(StatusCodes.Status201Created, new CreateWorkflowResponse(workflowId));
149+
}
150+
catch (Exception e)
151+
{
152+
return Problem($"Unexpected error occured: {e.Message}", $"/workflows", (int)HttpStatusCode.InternalServerError);
153+
}
154+
}
110155
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
Feature: WorkflowApi
2+
3+
API to interact with clinician workflows
4+
5+
@WorkflowUpdateAPI
6+
Scenario Outline: Get all clinical workflows from API - Single workflow
7+
Given I have an endpoint /workflows
8+
And I have a clinical workflow Basic_Workflow_1
9+
When I send a GET request
10+
Then I will get a 200 response
11+
And I can see 1 workflow is returned
12+
13+
@WorkflowUpdateAPI
14+
Scenario Outline: Get all clinical workflows from API - Multiple workflows
15+
Given I have an endpoint /workflows
16+
And I have a clinical workflow Basic_Workflow_2
17+
And I have a clinical workflow Basic_Workflow_3
18+
When I send a GET request
19+
Then I will get a 200 response
20+
And I can see 2 workflows are returned
21+
22+
@WorkflowUpdateAPI
23+
Scenario Outline: Get all clinical workflows from API - No workflows
24+
Given I have an endpoint /workflows
25+
When I send a GET request
26+
Then I will get a 200 response
27+
And I can see 0 workflows are returned
28+
29+
@WorkflowAPI
30+
Scenario: Update workflow with valid details
31+
Given I have a clinical workflow Basic_Workflow_1_static
32+
And I have an endpoint /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3
33+
And I have a body Basic_Workflow_Update_1
34+
When I send a PUT request
35+
Then I will get a 201 response
36+
And the Id c86a437d-d026-4bdf-b1df-c7a6372b89e3 is returned in the response body
37+
And multiple workflow revisions now exist with correct details
38+
39+
@WorkflowAPI
40+
Scenario Outline: Update workflow with invalid details
41+
Given I have a clinical workflow Basic_Workflow_1_static
42+
And I have an endpoint <endpoint>
43+
And I have a body <put_body>
44+
When I send a PUT request
45+
Then I will get a 400 response
46+
And I will recieve the error message <message>
47+
Examples:
48+
| endpoint | put_body | message |
49+
| /workflows/1 | Basic_Workflow_Update_1 | Failed to validate id, not a valid guid |
50+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_Name_Length | is not a valid Workflow Name |
51+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_Desc_Length | is not a valid Workflow Description |
52+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_AETitle_Length | is not a valid AE Title |
53+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_DataOrg | is not a valid Informatics Gateway - dataOrigins |
54+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_ExportDest | is not a valid Informatics Gateway - exportDestinations |
55+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_TaskDesc_Length | is not a valid taskDescription |
56+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_TaskType_Length | is not a valid taskType |
57+
| /workflows/c86a437d-d026-4bdf-b1df-c7a6372b89e3 | Invalid_Workflow_Update_TaskArgs | is not a valid args |
58+
59+
@WorkflowAPI
60+
Scenario: Update workflow where workflow Id does not exist
61+
Given I have a clinical workflow Basic_Workflow_1
62+
And I have an endpoint /workflows/52b87b54-a728-4796-9a79-d30867da2a6e
63+
And I have a body Basic_Workflow_Update_1
64+
When I send a PUT request
65+
Then I will get a 404 response
66+
And I will recieve the error message Failed to find workflow with Id: 52b87b54-a728-4796-9a79-d30867da2a6e
67+
68+

0 commit comments

Comments
 (0)