-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataAccessController.cs
More file actions
239 lines (211 loc) · 7.97 KB
/
DataAccessController.cs
File metadata and controls
239 lines (211 loc) · 7.97 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
using Microsoft.AspNetCore.Mvc;
using System.Linq;
using System.Collections.Generic;
using DataExplorerApi.Models;
using Microsoft.EntityFrameworkCore;
namespace DataExplorerApi
{
public static class EnumerableExtensions
{
public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
var seenKeys = new HashSet<TKey>();
foreach (var element in source)
{
if (seenKeys.Add(keySelector(element)))
{
yield return element;
}
}
}
}
public class ActivityStep
{
public string Step { get; set; }
public string StepName { get; set; }
}
public class EventInfo
{
public string Event { get; set; }
public string EventName { get; set; }
}
public class ActivityInfo
{
public string Activity { get; set; }
public string ActivityName { get; set; }
public List<ActivityStep> Steps { get; set; } = new List<ActivityStep>();
public List<EventInfo> Events { get; set; } = new List<EventInfo>();
}
public class StatementInfo
{
public string Id { get; set; }
public string Name { get; set; }
public string SubjectName { get; set; }
public string PredicateName { get; set; }
public string ObjectName { get; set; }
}
[Route("api/[controller]")]
[ApiController]
public class DataAccessController : ControllerBase
{
private readonly VastDataContext _context;
public DataAccessController(VastDataContext context)
{
_context = context;
}
[HttpGet("concepts")]
public IActionResult Concepts(string? query = null)
{
var tempResults = _context.PrimaryIndices
.Where(pi => pi.Concept != null && pi.ConceptName != null);
if (!string.IsNullOrEmpty(query))
{
tempResults = tempResults.Where(pi => pi.ConceptName.ToLower().Contains(query.ToLower()));
}
return Ok(tempResults
.GroupBy(pi => pi.Concept)
.Select(g => new
{
Concept = g.Key,
ConceptName = g.First().ConceptName
})
.ToList());
}
// The route becomes: api/DataAccess/search?searchType={searchType}&conceptId={conceptId}
[HttpGet("search")]
public IActionResult Search(int searchType, string? conceptId = null, string? id = null)
{
switch (searchType)
{
case 0:
return Ok(GetActivitiesByConcept(conceptId, id));
case 1:
return Ok(GetEventsByConcept(conceptId));
case 2:
return Ok(GetProductsByConcept(conceptId));
case 3:
return Ok(GetStatementsByConcept(conceptId));
default:
return BadRequest("Invalid search type.");
}
}
[HttpGet("product-images")]
public IActionResult ProductImages([FromQuery] string? activityId,[FromQuery] string[] eventId = null,[FromQuery] string[] stepId = null)
{
var tempResult = _context.PrimaryIndices
.Where(pi => pi.Activity == activityId);
if (eventId.Length>0)
{
tempResult = tempResult.Where(pi => eventId.Contains(pi.Event));
}
if (stepId.Length>0)
{
tempResult = tempResult.Where(pi => stepId.Contains(pi.Step));
}
var tempResult2 = tempResult.Select(pi => pi.Product)
.Distinct();
return Ok(_context.ProductIndices
.Where(p => tempResult2.Contains(p.Object))
.Select(p => p.VastImageUriref)
.Distinct()
.Where(p => p != null)
.ToList()
);
}
private List<ActivityInfo> GetActivitiesByConcept(string? conceptId = null, string? id = null)
{
IQueryable<string> tempResult;
if (id == null)
{
tempResult = _context.PrimaryIndices
.Where(pi => pi.Concept == conceptId || conceptId == null)
.Select(pi => pi.Activity)
.Distinct();
}
else
{
tempResult = _context.PrimaryIndices
.Where(pi => pi.Activity == id)
.Select(pi => pi.Activity)
.Distinct();
}
// 1. Fetch the activity data
var activitiesData = _context.ActivityIndices
.Where(a => tempResult.Contains(a.Activity))
.Select(a => new
{
a.Activity,
a.ActivityName,
a.Step,
a.StepName
})
.ToList();
// 2. Process and shape the data in memory
var groupedActivities = activitiesData
.GroupBy(a => new { a.Activity, a.ActivityName })
.Select(group => new ActivityInfo
{
Activity = group.Key.Activity,
ActivityName = group.Key.ActivityName,
Steps = group.Select(g => new ActivityStep
{
Step = g.Step,
StepName = g.StepName
})
.DistinctBy(step => new { step.Step, step.StepName }) // Ensuring unique steps
.ToList()
})
.ToList();
//Go through the grouped activities and add the events
foreach (var activity in groupedActivities)
{
activity.Events = _context.EventIndices
.Where(e => e.Activity == activity.Activity)
.Select(e => new EventInfo
{
Event = e.Event,
EventName = e.EventName
})
.ToList();
}
return groupedActivities;
}
private List<string> GetEventsByConcept(string conceptId)
{
return _context.PrimaryIndices
.Where(pi => pi.Concept == conceptId)
.Select(pi => pi.Event)
.Distinct()
.ToList();
}
private List<ProductIndex> GetProductsByConcept(string? conceptId)
{
var uniqueMatchingProducts = _context.PrimaryIndices
.Where(pi => pi.Concept == conceptId || conceptId == null)
.Select(pi => pi.Product)
.Distinct();
return _context.ProductIndices
.Where(p => uniqueMatchingProducts.Contains(p.Object))
.ToList();
}
private List<StatementInfo> GetStatementsByConcept(string? conceptId)
{
var tempResults = _context.PrimaryIndices
.Where(pi => pi.Concept == conceptId || conceptId == null)
.Select(pi => pi.Statement)
.Distinct()
.ToList();
return _context.StatementIndices
.Where(s => tempResults.Contains(s.Object))
.Select(s => new StatementInfo
{
Id = s.Object,
SubjectName = s.SubjectNameDescription,
PredicateName = s.PredicateNameDescription,
ObjectName = (s.ObjectRelationNameDescription ?? "") + (s.StatementObjectNameDescription ?? ""),
Name = s.Name
})
.ToList();
}
}
}