forked from ServiceStack/ServiceStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoQueryScripts.cs
More file actions
69 lines (57 loc) · 3.02 KB
/
AutoQueryScripts.cs
File metadata and controls
69 lines (57 loc) · 3.02 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
using System;
using System.Collections.Generic;
using System.Linq;
using ServiceStack.Script;
using ServiceStack.Text;
using ServiceStack.Web;
namespace ServiceStack
{
[Obsolete("Use AutoQueryScripts")]
public class TemplateAutoQueryFilters : AutoQueryScripts {}
public class AutoQueryScripts : ScriptMethods, IAutoQueryDbFilters
{
private IHttpRequest req(ScriptScopeContext scope) => scope.GetValue("Request") as IHttpRequest;
private ServiceStackHost appHost => HostContext.AppHost;
public object sendToAutoQuery(ScriptScopeContext scope, string requestName) =>
sendToAutoQuery(scope, TypeConstants.EmptyObjectDictionary, requestName, null);
public object sendToAutoQuery(ScriptScopeContext scope, object dto, string requestName) => sendToAutoQuery(scope, dto, requestName, null);
public object sendToAutoQuery(ScriptScopeContext scope, object dto, string requestName, object options)
{
try
{
if (requestName == null)
throw new ArgumentNullException(nameof(requestName));
if (dto == null)
throw new ArgumentNullException(nameof(dto));
var requestType = appHost.Metadata.GetOperationType(requestName);
if (requestType == null)
throw new ArgumentException("Request DTO not found: " + requestName);
if (requestType.HasInterface(typeof(IQueryData)))
{
if (!(Context.ScriptMethods.FirstOrDefault(x => x is ServiceStackScripts) is ServiceStackScripts ssFilter))
throw new NotImplementedException("sendToAutoQuery Data requires TemplateServiceStackFilters");
return ssFilter.sendToAutoQuery(scope, dto, requestName, options);
}
var autoQuery = appHost.TryResolve<IAutoQueryDb>();
if (autoQuery == null)
throw new NotSupportedException("The AutoQueryFeature plugin is not registered.");
var objDictionary = dto is Dictionary<string, object> od ? od : null;
var requestDto = objDictionary != null
? objDictionary.FromObjectDictionary(requestType)
: dto.GetType() == requestType
? dto
: dto.ConvertTo(requestType);
if (!(requestDto is IQueryDb aqDto))
throw new ArgumentException("Request DTO is not an AutoQuery DTO: " + requestName);
var reqParams = objDictionary?.ToStringDictionary() ?? TypeConstants.EmptyStringDictionary;
var q = autoQuery.CreateQuery(aqDto, reqParams, req(scope));
var response = autoQuery.Execute(aqDto, q);
return response;
}
catch (Exception ex)
{
throw new StopFilterExecutionException(scope, options, ex);
}
}
}
}