|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Threading.Tasks; |
| 5 | +using Microsoft.AspNetCore.Mvc; |
| 6 | +using Microsoft.Extensions.Logging; |
| 7 | +using Newtonsoft.Json; |
| 8 | +using Newtonsoft.Json.Linq; |
| 9 | +using WorkflowCore.Interface; |
| 10 | +using WorkflowCore.Models; |
| 11 | +using WorkflowCore.Models.Search; |
| 12 | + |
| 13 | +namespace WebApiSample.Controllers |
| 14 | +{ |
| 15 | + [Route("api/[controller]")] |
| 16 | + [ApiController] |
| 17 | + public class WorkflowsController : Controller |
| 18 | + { |
| 19 | + private readonly IWorkflowController _workflowService; |
| 20 | + private readonly IWorkflowRegistry _registry; |
| 21 | + private readonly IPersistenceProvider _workflowStore; |
| 22 | + private readonly ISearchIndex _searchService; |
| 23 | + |
| 24 | + public WorkflowsController(IWorkflowController workflowService, ISearchIndex searchService, IWorkflowRegistry registry, IPersistenceProvider workflowStore) |
| 25 | + { |
| 26 | + _workflowService = workflowService; |
| 27 | + _workflowStore = workflowStore; |
| 28 | + _registry = registry; |
| 29 | + _searchService = searchService; |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | + [HttpGet] |
| 34 | + public async Task<IActionResult> Get(string terms, WorkflowStatus? status, string type, DateTime? createdFrom, DateTime? createdTo, int skip, int take = 10) |
| 35 | + { |
| 36 | + var filters = new List<SearchFilter>(); |
| 37 | + |
| 38 | + if (status.HasValue) |
| 39 | + filters.Add(StatusFilter.Equals(status.Value)); |
| 40 | + |
| 41 | + if (createdFrom.HasValue) |
| 42 | + filters.Add(DateRangeFilter.After(x => x.CreateTime, createdFrom.Value)); |
| 43 | + |
| 44 | + if (createdTo.HasValue) |
| 45 | + filters.Add(DateRangeFilter.Before(x => x.CreateTime, createdTo.Value)); |
| 46 | + |
| 47 | + if (!string.IsNullOrEmpty(type)) |
| 48 | + filters.Add(ScalarFilter.Equals(x => x.WorkflowDefinitionId, type)); |
| 49 | + |
| 50 | + var result = await _searchService.Search(terms, skip, take, filters.ToArray()); |
| 51 | + |
| 52 | + return Json(result); |
| 53 | + } |
| 54 | + |
| 55 | + [HttpGet("{id}")] |
| 56 | + public async Task<IActionResult> Get(string id) |
| 57 | + { |
| 58 | + var result = await _workflowStore.GetWorkflowInstance(id); |
| 59 | + return Json(result); |
| 60 | + } |
| 61 | + |
| 62 | + [HttpPost("{id}")] |
| 63 | + [HttpPost("{id}/{version}")] |
| 64 | + public async Task<IActionResult> Post(string id, int? version, string reference, [FromBody]JObject data) |
| 65 | + { |
| 66 | + string workflowId = null; |
| 67 | + var def = _registry.GetDefinition(id, version); |
| 68 | + if (def == null) |
| 69 | + return BadRequest(String.Format("Workflow defintion {0} for version {1} not found", id, version)); |
| 70 | + |
| 71 | + if ((data != null) && (def.DataType != null)) |
| 72 | + { |
| 73 | + var dataStr = JsonConvert.SerializeObject(data); |
| 74 | + var dataObj = JsonConvert.DeserializeObject(dataStr, def.DataType); |
| 75 | + workflowId = await _workflowService.StartWorkflow(id, version, dataObj, reference); |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + workflowId = await _workflowService.StartWorkflow(id, version, null, reference); |
| 80 | + } |
| 81 | + |
| 82 | + return Ok(workflowId); |
| 83 | + } |
| 84 | + |
| 85 | + [HttpPut("{id}/suspend")] |
| 86 | + public Task<bool> Suspend(string id) |
| 87 | + { |
| 88 | + return _workflowService.SuspendWorkflow(id); |
| 89 | + } |
| 90 | + |
| 91 | + [HttpPut("{id}/resume")] |
| 92 | + public Task<bool> Resume(string id) |
| 93 | + { |
| 94 | + return _workflowService.ResumeWorkflow(id); |
| 95 | + } |
| 96 | + |
| 97 | + [HttpDelete("{id}")] |
| 98 | + public Task<bool> Terminate(string id) |
| 99 | + { |
| 100 | + return _workflowService.TerminateWorkflow(id); |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments