-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathQueryPlansController.cs
More file actions
150 lines (133 loc) · 5 KB
/
Copy pathQueryPlansController.cs
File metadata and controls
150 lines (133 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
using Automation.UI.Models;
using Automation.UI.Services.Persistence;
using Microsoft.AspNetCore.Mvc;
namespace Automation.UI.Controllers;
public class QueryPlansController(IQueryPlanTemplateStore store) : Controller
{
[HttpGet]
public async Task<IActionResult> Index(CancellationToken ct)
{
var templates = await store.GetAllAsync(ct);
return View(templates);
}
[HttpGet]
public IActionResult GetDefaults()
{
var defaults = QueryPlanDefaults.GetDefaultAsInput();
var model = new QueryPlanTemplate
{
Id = Guid.NewGuid(),
Name = "New Query Plan",
EhrDescription = defaults.EhrDescription ?? "Epic",
LookBack = defaults.LookBack ?? "P0D",
InitialQueries = defaults.InitialQueries.Select(ToQueryEntry).ToList(),
SupplementalQueries = defaults.SupplementalQueries.Select(ToQueryEntry).ToList()
};
return Json(model);
}
[HttpGet]
public async Task<IActionResult> GetJson(Guid id, CancellationToken ct)
{
var template = await store.GetByIdAsync(id, ct);
if (template == null) return NotFound();
return Json(template);
}
[HttpGet]
public async Task<IActionResult> AcquiredTypes(Guid id, CancellationToken ct)
{
var template = await store.GetByIdAsync(id, ct);
if (template == null) return NotFound();
return Json(template.GetAcquiredResourceTypes().OrderBy(t => t).ToList());
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SaveInline([FromBody] QueryPlanTemplate model, CancellationToken ct)
{
if (string.IsNullOrWhiteSpace(model.Name))
return BadRequest("Query plan name is required.");
var existing = await store.GetByIdAsync(model.Id, ct);
if (existing is { IsSystem: true })
return StatusCode(StatusCodes.Status403Forbidden, "Forbidden: system plan cannot be modified.");
model.IsSystem = false;
model.UpdatedAt = DateTimeOffset.UtcNow;
await store.UpsertAsync(model, ct);
return Json(new { id = model.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> DeleteInline([FromBody] IdRequest request, CancellationToken ct)
{
var template = await store.GetByIdAsync(request.Id, ct);
if (template == null)
return NotFound();
if (template.IsSystem)
return StatusCode(StatusCodes.Status403Forbidden, "Forbidden: system plan cannot be deleted.");
await store.DeleteAsync(request.Id, ct);
return Ok();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> CloneInline([FromBody] IdRequest request, CancellationToken ct)
{
var source = await store.GetByIdAsync(request.Id, ct);
if (source == null) return NotFound();
var clone = new QueryPlanTemplate
{
Id = Guid.NewGuid(),
Name = $"{source.Name} (Copy)",
Description = source.Description,
IsSystem = false,
IsDefault = false,
EhrDescription = source.EhrDescription,
LookBack = source.LookBack,
InitialQueries = source.InitialQueries.Select(CloneEntry).ToList(),
SupplementalQueries = source.SupplementalQueries.Select(CloneEntry).ToList(),
UpdatedAt = DateTimeOffset.UtcNow
};
await store.UpsertAsync(clone, ct);
return Json(new { id = clone.Id });
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> SetDefaultInline([FromBody] IdRequest request, CancellationToken ct)
{
var template = await store.GetByIdAsync(request.Id, ct);
if (template == null) return NotFound();
await store.SetDefaultAsync(request.Id, ct);
return Ok();
}
private static QueryEntry ToQueryEntry(QueryPlanQueryEntry src) => new()
{
ResourceType = src.ResourceType,
QueryConfigType = src.QueryConfigType,
OperationType = src.OperationType,
Paged = src.Paged,
Parameters = src.Parameters.Select(p => new QueryParameter
{
ParameterType = p.ParameterType,
Name = p.Name,
Variable = p.Variable,
Format = p.Format,
Literal = p.Literal,
Resource = p.Resource,
PagedValue = p.PagedValue
}).ToList()
};
private static QueryEntry CloneEntry(QueryEntry src) => new()
{
ResourceType = src.ResourceType,
QueryConfigType = src.QueryConfigType,
OperationType = src.OperationType,
Paged = src.Paged,
Parameters = src.Parameters.Select(p => new QueryParameter
{
ParameterType = p.ParameterType,
Name = p.Name,
Variable = p.Variable,
Format = p.Format,
Literal = p.Literal,
Resource = p.Resource,
PagedValue = p.PagedValue
}).ToList()
};
}