/
WebApiServiceGeneratorProcessor.cs
300 lines (245 loc) · 13.1 KB
/
WebApiServiceGeneratorProcessor.cs
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) 2020 DHGMS Solutions and Contributors. All rights reserved.
// DHGMS Solutions and Contributors licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using Dhgms.Nucleotide.Generators.GeneratorProcessors;
using Dhgms.Nucleotide.Generators.Models;
using Dhgms.Nucleotide.Generators.PropertyInfo;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
namespace Dhgms.Nucleotide.Generators.Features.WebApi
{
public class WebApiServiceGeneratorProcessor : BaseClassLevelCodeGeneratorProcessor<IEntityGenerationModel>
{
/// <inheritdoc />
protected override IEnumerable<PropertyDeclarationSyntax> GetPropertyDeclarations(IEntityGenerationModel entityGenerationModel)
{
return null;
}
protected override string[] GetClassPrefixes() => null;
protected override string GetClassSuffix()
{
return "Controller";
}
protected override IList<string> GetUsings()
{
return new List<string>
{
"System.Threading.Tasks",
"Microsoft.Extensions.Logging",
"Microsoft.AspNetCore.Authorization",
"Microsoft.AspNetCore.Mvc"
};
}
protected override bool GetWhetherClassShouldBePartialClass() => true;
protected override bool GetWhetherClassShouldBeSealedClass() => true;
/// <inheritdoc />
protected override string GetBaseClass(IEntityGenerationModel entityGenerationModel)
{
var entityName = entityGenerationModel.ClassName;
return $"global::Whipstaff.AspNetCore.CrudApiController<Queries.IList{entityName}Query, RequestDtos.List{entityName}RequestDto, ResponseDtos.List{entityName}ResponseDto, Queries.IView{entityName}Query, ResponseDtos.View{entityName}ResponseDto, Commands.IAdd{entityName}Command, RequestDtos.Add{entityName}RequestDto, ResponseDtos.Add{entityName}ResponseDto, Commands.IDelete{entityName}Command, ResponseDtos.Delete{entityName}ResponseDto, Commands.IUpdate{entityName}Command, RequestDtos.Update{entityName}RequestDto, ResponseDtos.Update{entityName}ResponseDto, LoggerMessageActions.{entityName}LoggerMessageActions>";
}
protected override IEnumerable<string> GetImplementedInterfaces(IEntityGenerationModel entityGenerationModel)
{
return null;
/*
return new List<string>
{
$"Services.I{entityName}Service"
};
*/
}
/// <inheritdoc />
protected override IList<Tuple<Func<string, string>, string, Accessibility>> GetConstructorArguments()
{
var result = new List<Tuple<Func<string, string>, string, Accessibility>>
{
new Tuple<Func<string, string>, string, Accessibility>(_ => "Microsoft.AspNetCore.Authorization.IAuthorizationService", "authorizationService", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"Microsoft.Extensions.Logging.ILogger<{entityName}Controller>", "logger", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"MediatR.IMediator", "mediator", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"CommandFactories.I{entityName}CommandFactory", "commandFactory", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"QueryFactories.I{entityName}QueryFactory", "queryFactory", Accessibility.Private),
new Tuple<Func<string, string>, string, Accessibility>(entityName => $"LoggerMessageActions.{entityName}LoggerMessageActions", "logMessageActions", Accessibility.Private),
};
return result;
}
protected override IList<string> GetBaseConstructorArguments()
{
var result = new List<string>
{
"authorizationService",
"logger",
"mediator",
"commandFactory",
"queryFactory",
"logMessageActions",
};
return result;
}
protected override string[] GetClassLevelCommentSummary(string entityName)
{
return new[]
{
$"Web API Service for {entityName}"
};
}
protected override string[] GetClassLevelCommentRemarks(string entityName)
{
return null;
}
protected override IList<Tuple<string, IList<string>>> GetClassAttributes(IEntityGenerationModel entityDeclaration)
{
return new List<Tuple<string, IList<string>>>
{
new Tuple<string, IList<string>>("Microsoft.AspNetCore.Authorization.Authorize", null),
};
}
/// <inheritdoc />
protected override MethodDeclarationSyntax[] GetMethodDeclarations(IEntityGenerationModel entityGenerationModel)
{
var entityName = entityGenerationModel.ClassName;
var result = new []
{
GetAddActionResultAsyncDeclaration(entityName),
GetDeleteActionResultAsyncDeclaration(entityName),
GetListActionResultAsyncDeclaration(entityName),
GetUpdateActionResultAsyncDeclaration(entityName),
GetViewActionResultAsyncDeclaration(entityName),
//GetAddMethodDeclaration(entityName),
//GetDeleteMethodDeclaration(entityName),
//GetListMethodDeclaration(entityName),
//GetUpdateMethodDeclaration(entityName),
//GetViewMethodDeclaration(entityName),
GetCommandMethodDeclaration(entityName,"Add", $"RequestDtos.Add{entityName}RequestDto"),
GetCommandMethodDeclaration(entityName,"Delete", "long"),
GetCommandMethodDeclaration(entityName,"Update", $"RequestDtos.Update{entityName}RequestDto"),
GetPolicyMethodDeclaration(entityName, "Add"),
GetPolicyMethodDeclaration(entityName, "Delete"),
GetPolicyMethodDeclaration(entityName, "List"),
GetPolicyMethodDeclaration(entityName, "Update"),
GetPolicyMethodDeclaration(entityName, "View"),
/*
GetEventIdMethodDeclaration(entityName, "Add"),
GetEventIdMethodDeclaration(entityName, "Delete"),
GetEventIdMethodDeclaration(entityName, "List"),
GetEventIdMethodDeclaration(entityName, "Update"),
GetEventIdMethodDeclaration(entityName, "View"),
*/
GetQueryMethodDeclaration(entityName,"List", $"RequestDtos.List{entityName}RequestDto"),
GetQueryMethodDeclaration(entityName,"View", "long"),
};
return result;
}
private MethodDeclarationSyntax GetCommandMethodDeclaration(
string entityName,
string action,
string requestType)
{
var methodName = $"Get{action}CommandAsync";
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"await CommandFactory.Get{action}CommandAsync(request, claimsPrincipal, cancellationToken).ConfigureAwait(false)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName($"Task<Commands.I{action}{entityName}Command>");
var parameters = GetParams(new []
{
$"{requestType} request",
"System.Security.Claims.ClaimsPrincipal claimsPrincipal",
"System.Threading.CancellationToken cancellationToken",
});
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.AddBodyStatements(body);
return declaration;
}
private MethodDeclarationSyntax GetQueryMethodDeclaration(
string entityName,
string action,
string requestType)
{
var methodName = $"Get{action}QueryAsync";
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"await QueryFactory.Get{action}QueryAsync(request, claimsPrincipal, cancellationToken).ConfigureAwait(false)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName($"Task<Queries.I{action}{entityName}Query>");
var parameters = GetParams(new []
{
$"{requestType} request",
"System.Security.Claims.ClaimsPrincipal claimsPrincipal",
"System.Threading.CancellationToken cancellationToken",
});
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.AddBodyStatements(body);
return declaration;
}
protected override SeparatedSyntaxList<AttributeSyntax> GetAttributesForProperty(PropertyInfoBase propertyInfo)
{
return default(SeparatedSyntaxList<AttributeSyntax>);
}
private MethodDeclarationSyntax GetPolicyMethodDeclaration(string entityName, string action)
{
var methodName = $"Get{action}PolicyAsync";
var returnStatement = SyntaxFactory.ReturnStatement(SyntaxFactory.ParseExpression($"await Task.FromResult(\"Api.{entityName}.{action}\").ConfigureAwait(false)"));
var body = new StatementSyntax[]
{
returnStatement
};
var returnType = SyntaxFactory.ParseTypeName("Task<string>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword))
.AddBodyStatements(body);
return declaration;
}
private MethodDeclarationSyntax GetAddActionResultAsyncDeclaration(string entityName)
{
return GetOkActionResultDeclaration(entityName, "add");
}
private MethodDeclarationSyntax GetDeleteActionResultAsyncDeclaration(string entityName)
{
return GetOkActionResultDeclaration(entityName, "delete");
}
private MethodDeclarationSyntax GetListActionResultAsyncDeclaration(string entityName)
{
return GetOkActionResultDeclaration(entityName, "list");
}
private MethodDeclarationSyntax GetUpdateActionResultAsyncDeclaration(string entityName)
{
return GetOkActionResultDeclaration(entityName, "update");
}
private MethodDeclarationSyntax GetViewActionResultAsyncDeclaration(string entityName)
{
return GetOkActionResultDeclaration(entityName, "view");
}
private MethodDeclarationSyntax GetOkActionResultDeclaration(string entityName, string action)
{
var camelAction = action.Substring(0, 1).ToUpper() + action.Substring(1);
var lowerAction = action.Substring(0, 1).ToLower() + action.Substring(1);
var methodName = $"Get{camelAction}ActionResultAsync";
var baseMethodInvocationSyntax =
RoslynGenerationHelpers.GetMethodOnClassInvocationSyntax("Ok", new [] {$"{lowerAction}Response"}, false);
var taskFromResult =
RoslynGenerationHelpers.GetStaticMethodInvocationSyntax("Task", "FromResult",
baseMethodInvocationSyntax, true);
var returnStatement = SyntaxFactory.ReturnStatement(taskFromResult);
var body = new StatementSyntax[]
{
returnStatement
};
var parameters = GetParams(new []{ $"ResponseDtos.{camelAction}{entityName}ResponseDto {lowerAction}Response"});
var returnType = SyntaxFactory.ParseTypeName("System.Threading.Tasks.Task<IActionResult>");
var declaration = SyntaxFactory.MethodDeclaration(returnType, methodName)
.WithParameterList(parameters)
.AddModifiers(SyntaxFactory.Token(SyntaxKind.ProtectedKeyword), SyntaxFactory.Token(SyntaxKind.AsyncKeyword), SyntaxFactory.Token(SyntaxKind.OverrideKeyword))
.AddBodyStatements(body);
return declaration;
}
}
}