-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataSources.cs
More file actions
97 lines (87 loc) · 5.06 KB
/
DataSources.cs
File metadata and controls
97 lines (87 loc) · 5.06 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
using System.Collections.Generic;
using System.Linq;
using Saber.Core;
using Saber.Vendor;
namespace Saber.Vendors.DataSets
{
public class DataSources : IVendorDataSources
{
//Register data sources with Saber that can be used with the List component
public string Vendor { get; set; } = "Data Set";
public string Prefix { get; set; } = "dataset";
public string Description { get; set; } = "Create database tables, columns of various data types, and rows of data from within Saber's Editor, then use your tables as data sources.";
public void Init()
{}
public List<KeyValuePair<string, string>> List()
{
//get list of available Data Sets as data sources
var datasets = Query.DataSets.GetList(null, true, true);
return datasets.Select(a => new KeyValuePair<string, string>(a.datasetId.ToString(), a.label)).ToList();
}
public DataSource Get(string key)
{
//get information about a data set as a data source
var id = int.Parse(key.Replace("dataset-", ""));
return Cache.DataSources.ContainsKey(id) ? Cache.DataSources[id] : new DataSource();
}
public void Create(IRequest request, string key, Dictionary<string, string> columns)
{
var datasetId = int.Parse(key);
var lang = columns.ContainsKey("lang") ? columns["lang"] : request.User.Language;
Query.DataSets.AddRecord(request.User.UserId, datasetId, lang, columns.Where(a => !DataSets.ExcludedFields.Contains(a.Key))
.Select(a => new Query.Models.DataSets.Field() { Name = a.Key, Value = a.Value }).ToList());
Cache.DataSets = null; //reset cache for data sets
}
public void Update(IRequest request, string key, string id, Dictionary<string, string> columns)
{
var datasetId = int.Parse(key);
var lang = columns.ContainsKey("lang") ? columns["lang"] : request.User.Language;
Query.DataSets.UpdateRecord(request.User.UserId, datasetId, int.Parse(id), lang, columns.Where(a => !DataSets.ExcludedFields.Contains(a.Key))
.Select(a => new Query.Models.DataSets.Field() { Name = a.Key, Value = a.Value }).ToList());
Cache.DataSets = null; //reset cache for data sets
}
public List<Dictionary<string, string>> Filter(IRequest request, string key, int start, int length, string lang = "en", List<DataSource.FilterGroup> filters = null, List<DataSource.OrderBy> orderBy = null)
{
//get filtered records from a data set
var datasetId = int.Parse(key);
var dataset = Cache.DataSets[datasetId];
var results = Query.DataSets.GetRecords(request, datasetId, start, length, lang, dataset.userdata ? request.User.UserId : 0, filters, orderBy)?
.Select(a => a.ToDictionary(k => k.Key, v => v.Value?.ToString() ?? ""));
if(results == null)
{
return new List<Dictionary<string, string>>();
}
return results.ToList();
}
public Dictionary<string, List<Dictionary<string, string>>> Filter(IRequest request, string key, string lang = "en", Dictionary<string, DataSource.PositionSettings> positions = null, Dictionary<string, List<DataSource.FilterGroup>> filters = null, Dictionary<string, List<DataSource.OrderBy>> orderBy = null, string[] childKeys = null)
{
var datasetId = int.Parse(key);
var dataset = Cache.DataSets[datasetId];
return Query.DataSets.GetRecordsInRelationships(request, datasetId, lang, dataset.userdata ? request.User.UserId : 0, positions, filters, orderBy, childKeys)?
.ToDictionary(a => a.Key, a =>
{
return new List<Dictionary<string, string>>(a.Value.Select(b =>
{
var c = new Dictionary<string, string>();
foreach(var k in b)
{
c.Add(k.Key, k.Value.ToString() ?? "");
}
return c;
}).ToList());
}) ?? new Dictionary<string, List<Dictionary<string, string>>>();
}
int IVendorDataSources.FilterTotal(IRequest request, string key, string lang, List<DataSource.FilterGroup> filter)
{
var datasetId = int.Parse(key);
var dataset = Cache.DataSets[datasetId];
return Query.DataSets.GetRecordCount(request, datasetId, lang, dataset.userdata ? request.User.UserId : 0, filter);
}
Dictionary<string, int> IVendorDataSources.FilterTotal(IRequest request, string key, string lang, Dictionary<string, List<DataSource.FilterGroup>> filters, string[] childKeys)
{
var datasetId = int.Parse(key);
var dataset = Cache.DataSets[datasetId];
return Query.DataSets.GetRecordCountInRelationships(request, datasetId, lang, dataset.userdata ? request.User.UserId : 0, filters, childKeys);
}
}
}