Fill out skeleton methods in MVC Controller

This commit is contained in:
Geoffrey Smith 2019-08-02 22:30:49 +10:00
parent 0dd5f80d9f
commit 69748a4e20

View File

@ -17,8 +17,6 @@ uses
; ;
type type
TSwagDocToDelphiMVCFrameworkBuilder = class(TObject) TSwagDocToDelphiMVCFrameworkBuilder = class(TObject)
private private
FSwagDoc : TSwagDoc; FSwagDoc : TSwagDoc;
@ -45,6 +43,7 @@ uses
Json.Common.Helpers Json.Common.Helpers
, Winapi.Windows , Winapi.Windows
, System.IOUtils , System.IOUtils
, MVCFramework.Commons
; ;
{ TSwagDocToDelphiMVCFrameworkBuilder } { TSwagDocToDelphiMVCFrameworkBuilder }
@ -71,7 +70,6 @@ begin
Result := typeName; Result := typeName;
end; end;
constructor TSwagDocToDelphiMVCFrameworkBuilder.Create(SwagDoc: TSwagDoc); constructor TSwagDocToDelphiMVCFrameworkBuilder.Create(SwagDoc: TSwagDoc);
begin begin
FSwagDoc := SwagDoc; FSwagDoc := SwagDoc;
@ -110,7 +108,6 @@ begin
ConvertSwaggerDefinitionsToTypeDefinitions(LDelphiUnit); ConvertSwaggerDefinitionsToTypeDefinitions(LDelphiUnit);
LMVCController := TUnitTypeDefinition.Create; LMVCController := TUnitTypeDefinition.Create;
LMVCController.TypeName := 'TMyMVCController'; LMVCController.TypeName := 'TMyMVCController';
LMVCController.TypeInherited := 'TMVCController'; LMVCController.TypeInherited := 'TMVCController';
@ -118,7 +115,6 @@ begin
LDelphiUnit.AddType(LMVCController); LDelphiUnit.AddType(LMVCController);
for i := 0 to fSwagDoc.Paths.Count - 1 do for i := 0 to fSwagDoc.Paths.Count - 1 do
begin begin
for j := 0 to fSwagDoc.Paths[i].Operations.Count - 1 do for j := 0 to fSwagDoc.Paths[i].Operations.Count - 1 do
@ -144,6 +140,21 @@ begin
end; end;
end; end;
function ReturnStatusCode(inStatusCode: string):string;
begin
inStatusCode := inStatusCode.ToLower;
if (inStatusCode = 'default') or (inStatusCode = '200') then
Result := 'HTTP_STATUS.OK'
else if inStatusCode = '400' then
Result := 'HTTP_STATUS.BadRequest'
else if inStatusCode = '404' then
Result := 'HTTP_STATUS.NotFound'
else if inStatusCode = '405' then
Result := 'HTTP_STATUS.MethodNotAllowed'
else
Result := inStatusCode;
end;
procedure TSwagDocToDelphiMVCFrameworkBuilder.ConvertSwaggerResponsesToDelphiMethods(ADelphiUnit: TDelphiUnit; AMethod: TUnitMethod; AOperation: TSwagPathOperation); procedure TSwagDocToDelphiMVCFrameworkBuilder.ConvertSwaggerResponsesToDelphiMethods(ADelphiUnit: TDelphiUnit; AMethod: TUnitMethod; AOperation: TSwagPathOperation);
var var
LResponse: System.Generics.Collections.TPair<string, TSwagResponse>; LResponse: System.Generics.Collections.TPair<string, TSwagResponse>;
@ -158,76 +169,43 @@ begin
if LSchemaObj = nil then // No Return Info to Http Method if LSchemaObj = nil then // No Return Info to Http Method
begin begin
AMethod.Content.Add(' // ' + LResponse.Key + ' ' + LResponse.Value.Description); AMethod.Content.Add(' // ' + LResponse.Key + ' ' + LResponse.Value.Description);
AMethod.AddAttribute(' [MVCResponse(' + LResponse.Key + ', ' + QuotedStr(LResponse.Value.Description) + ')]'); AMethod.AddAttribute(' [MVCResponse(' + ReturnStatusCode(LResponse.Key) + ', ' + QuotedStr(LResponse.Value.Description) + ')]');
continue; continue;
end; end
else if LSchemaObj.TryGetValue('$ref', LRef) then else if LSchemaObj.TryGetValue('$ref', LRef) then
begin begin
AMethod.AddAttribute(' [MVCResponse(' + LResponse.Key + ', ' + QuotedStr(LResponse.Value.Description) + ', ' + ConvertRefToType(LRef) + ')]'); AMethod.AddAttribute(' [MVCResponse(' + ReturnStatusCode(LResponse.Key) + ', ' + QuotedStr(LResponse.Value.Description) + ', ' + ConvertRefToType(LRef) + ')]');
LResultParam := TUnitParameter.Create; LResultParam := TUnitParameter.Create;
LResultParam.ParamName := ConvertRefToVarName(LRef); LResultParam.ParamName := ConvertRefToVarName(LRef);
LResultParam.ParamType := TUnitTypeDefinition.Create; LResultParam.ParamType := TUnitTypeDefinition.Create;
LResultParam.ParamType.TypeName := ConvertRefToType(LRef); LResultParam.ParamType.TypeName := ConvertRefToType(LRef);
AMethod.AddLocalVariable(LResultParam); AMethod.AddLocalVariable(LResultParam);
AMethod.Content.Add(' ' + ConvertRefToVarName(LRef) + ' := ' + ConvertRefToType(LRef) + '.Create;'); AMethod.Content.Add(' ' + ConvertRefToVarName(LRef) + ' := ' + ConvertRefToType(LRef) + '.Create;');
for k := 0 to AOperation.Parameters.Count - 1 do AMethod.Content.Add('');
begin AMethod.Content.Add(' {TODO: Implement filling ' + ConvertRefToVarName(LRef) + ' }');
if AOperation.Parameters[k].InLocation <> rpiPath then AMethod.Content.Add(' Render(' + ReturnStatusCode(LResponse.Key) + ', ' + ConvertRefToVarName(LRef) + ');');
begin
LResultParam := TUnitParameter.Create;
LResultParam.ParamName := 'param' + CapitalizeFirstLetter(AOperation.Parameters[k].Name);
LResultParam.ParamType := TUnitTypeDefinition.Create;
LResultParam.ParamType.TypeName := 'String';
AMethod.AddLocalVariable(LResultParam);
AMethod.Content.Add(' param' + CapitalizeFirstLetter(AOperation.Parameters[k].Name) + ' := Context.Request.Params[' + QuotedStr(AOperation.Parameters[k].Name) + '];');
end;
end;
end end
else else
begin begin
if not LSchemaObj.TryGetValue('properties', LSchemaObj) then
continue;
if not LSchemaObj.TryGetValue('employees', LSchemaObj) then
continue;
if not LSchemaObj.TryGetValue('items', LSchemaObj) then if not LSchemaObj.TryGetValue('items', LSchemaObj) then
continue; continue;
if LSchemaObj.TryGetValue('$ref', LRef) then if LSchemaObj.TryGetValue('$ref', LRef) then
begin begin
AMethod.AddAttribute(' [MVCResponseList(' + LResponse.Key + ', ' + QuotedStr(LResponse.Value.Description) + ', ' + ConvertRefToType(LRef) + ')]'); ADelphiUnit.AddInterfaceUnit('Generics.Collections');
AMethod.AddAttribute(' [MVCResponseList(' + ReturnStatusCode(LResponse.Key) + ', ' + QuotedStr(LResponse.Value.Description) + ', ' + ConvertRefToType(LRef) + ')]');
LResultParam := TUnitParameter.Create; LResultParam := TUnitParameter.Create;
LResultParam.ParamName := ConvertRefToVarName(LRef); LResultParam.ParamName := ConvertRefToVarName(LRef);
LResultParam.ParamType := TUnitTypeDefinition.Create; LResultParam.ParamType := TUnitTypeDefinition.Create;
LResultParam.ParamType.TypeName := 'TObjectList<' + ConvertRefToType(LRef) + '>'; LResultParam.ParamType.TypeName := 'TObjectList<' + ConvertRefToType(LRef) + '>';
AMethod.AddLocalVariable(LResultParam); AMethod.AddLocalVariable(LResultParam);
ADelphiUnit.AddInterfaceUnit('Generics.Collections'); AMethod.Content.Add(' ' + ConvertRefToVarName(LRef) + ' := Context.Request.BodyAsListOf<' + ConvertRefToType(LRef) + '>;');
AMethod.Content.Add(' ' + ConvertRefToVarName(LRef) + ' := TObjectList<' + ConvertRefToType(LRef) + '>.Create;'); AMethod.Content.Add('');
for k := 0 to AOperation.Parameters.Count - 1 do AMethod.Content.Add(' {TODO: Implement filling ' + ConvertRefToVarName(LRef) + ' }');
begin AMethod.Content.Add('');
if AOperation.Parameters[k].InLocation <> rpiPath then AMethod.Content.Add(' Render(' + ReturnStatusCode(LResponse.Key) + ', ' + ConvertRefToVarName(LRef) + ');');
begin
LResultParam := TUnitParameter.Create;
LResultParam.ParamName := 'param' + AOperation.Parameters[k].Name;
LResultParam.ParamType := TUnitTypeDefinition.Create;
LResultParam.ParamType.TypeName := 'String';
AMethod.AddLocalVariable(LResultParam);
AMethod.Content.Add(' ' + AOperation.Parameters[k].Name + ' := Context.Request.Params[' + QuotedStr(AOperation.Parameters[k].Name) + '];');
end;
end;
end end
else else
begin begin
for k := 0 to AOperation.Parameters.Count - 1 do
begin
if AOperation.Parameters[k].InLocation <> rpiPath then
begin
LResultParam := TUnitParameter.Create;
LResultParam.ParamName := 'param' + AOperation.Parameters[k].Name;
LResultParam.ParamType := TUnitTypeDefinition.Create;
LResultParam.ParamType.TypeName := 'String';
AMethod.AddLocalVariable(LResultParam);
AMethod.Content.Add(' ' + AOperation.Parameters[k].Name + ' := Context.Request.Params[' + QuotedStr(AOperation.Parameters[k].Name) + '];');
end;
end;
AMethod.AddAttribute(' [MVCResponse(' + LResponse.Key + ', ' + QuotedStr(LResponse.Value.Description) + ')]'); AMethod.AddAttribute(' [MVCResponse(' + LResponse.Key + ', ' + QuotedStr(LResponse.Value.Description) + ')]');
end; end;
end; end;