|
1 | | -namespace CaseManagement.CMMN.Apis |
| 1 | +using CaseManagement.CMMN.Persistence; |
| 2 | +using Microsoft.AspNetCore.Mvc; |
| 3 | +using Newtonsoft.Json.Linq; |
| 4 | +using System.Linq; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace CaseManagement.CMMN.Apis |
2 | 8 | { |
3 | | - public class CaseDefinitionsController |
| 9 | + [Route(CMMNConstants.RouteNames.CaseDefinitions)] |
| 10 | + public class CaseDefinitionsController : Controller |
4 | 11 | { |
| 12 | + private readonly ICMMNDefinitionsQueryRepository _queryRepository; |
| 13 | + |
| 14 | + public CaseDefinitionsController(ICMMNDefinitionsQueryRepository queryRepository) |
| 15 | + { |
| 16 | + _queryRepository = queryRepository; |
| 17 | + } |
| 18 | + |
| 19 | + [HttpGet] |
| 20 | + public async Task<IActionResult> Get() |
| 21 | + { |
| 22 | + var result = await _queryRepository.GetAll(); |
| 23 | + return new OkObjectResult(new JArray(result.Select(r => new JObject |
| 24 | + { |
| 25 | + { "id", r.id }, |
| 26 | + { "name", r.name }, |
| 27 | + { "create_datetime", r.creationDate } |
| 28 | + }))); |
| 29 | + } |
| 30 | + |
| 31 | + [HttpGet("{id}")] |
| 32 | + public async Task<IActionResult> Get(string id) |
| 33 | + { |
| 34 | + var result = await _queryRepository.FindDefinitionById(id); |
| 35 | + if (result == null) |
| 36 | + { |
| 37 | + return new NotFoundResult(); |
| 38 | + } |
| 39 | + |
| 40 | + return new OkObjectResult(ToDto(result)); |
| 41 | + } |
| 42 | + |
| 43 | + [HttpGet("{id}/cases")] |
| 44 | + public async Task<IActionResult> GetCases(string id) |
| 45 | + { |
| 46 | + var result = await _queryRepository.FindDefinitionById(id); |
| 47 | + if (result == null) |
| 48 | + { |
| 49 | + return new NotFoundResult(); |
| 50 | + } |
| 51 | + |
| 52 | + return new OkObjectResult(result.@case); |
| 53 | + } |
| 54 | + |
| 55 | + private static JObject ToDto(tDefinitions def) |
| 56 | + { |
| 57 | + var result = new JObject |
| 58 | + { |
| 59 | + { "id", def.id }, |
| 60 | + { "name", def.name }, |
| 61 | + { "create_datetime", def.creationDate }, |
| 62 | + }; |
| 63 | + result.Add("cases", ToDto(def.@case)); |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + private static JArray ToDto(tCase[] cases) |
| 68 | + { |
| 69 | + var cmmnCases = new JArray(); |
| 70 | + foreach (var cmmnCase in cases) |
| 71 | + { |
| 72 | + cmmnCases.Add(new JObject |
| 73 | + { |
| 74 | + { "id", cmmnCase.id }, |
| 75 | + { "name", cmmnCase.name } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + return cmmnCases; |
| 80 | + } |
5 | 81 | } |
6 | 82 | } |
0 commit comments