Esempio n. 1
0
    private static ParameterSyntax[] actionArgs(AppApiActionTemplate actionTemplate, bool includeModifier)
    {
        var parameters = new List <ParameterSyntax>();

        if (includeModifier)
        {
            parameters.Add
            (
                Parameter(Identifier("modifier"))
                .WithType(PredefinedType(Token(SyntaxKind.StringKeyword)))
            );
        }
        if (!actionTemplate.HasEmptyModel())
        {
            parameters.Add
            (
                Parameter(Identifier("model"))
                .WithType
                (
                    new TypeSyntaxFromValueTemplate(actionTemplate.ModelTemplate).Value()
                )
            );
        }
        return(parameters.ToArray());
    }
Esempio n. 2
0
    private static SyntaxNodeOrToken[] postArgs(AppApiActionTemplate actionTemplate, bool includeModifier)
    {
        var args = new List <SyntaxNodeOrToken>();

        args.AddRange
        (
            new SyntaxNodeOrToken[]
        {
            Argument
            (
                LiteralExpression
                (
                    SyntaxKind.StringLiteralExpression,
                    Literal(actionTemplate.Name)
                )
            ),
            Token(SyntaxKind.CommaToken)
        }
        );
        if (includeModifier)
        {
            args.AddRange
            (
                new SyntaxNodeOrToken[]
            {
                Argument(IdentifierName("modifier")),
                Token(SyntaxKind.CommaToken)
            }
            );
        }
        else
        {
            args.AddRange
            (
                new SyntaxNodeOrToken[]
            {
                Argument
                (
                    LiteralExpression
                    (
                        SyntaxKind.StringLiteralExpression,
                        Literal("")
                    )
                ),
                Token(SyntaxKind.CommaToken)
            }
            );
        }
        if (actionTemplate.HasEmptyModel())
        {
            args.Add
            (
                Argument
                (
                    ObjectCreationExpression(IdentifierName("EmptyRequest"))
                    .WithArgumentList(ArgumentList())
                )
            );
        }
        else
        {
            args.Add(Argument(IdentifierName("model")));
        }
        return(args.ToArray());
    }
 private InvocationExpressionSyntax executeInvocationExpression(AppApiGroupTemplate group, AppApiActionTemplate action)
 {
     return(InvocationExpression
            (
                MemberAccessExpression
                (
                    SyntaxKind.SimpleMemberAccessExpression,
                    InvocationExpression
                    (
                        MemberAccessExpression
                        (
                            SyntaxKind.SimpleMemberAccessExpression,
                            InvocationExpression
                            (
                                MemberAccessExpression
                                (
                                    SyntaxKind.SimpleMemberAccessExpression,
                                    IdentifierName("api"),
                                    IdentifierName("Group")
                                )
                            )
                            .WithArgumentList
                            (
                                ArgumentList
                                (
                                    SingletonSeparatedList
                                    (
                                        Argument
                                        (
                                            LiteralExpression
                                            (
                                                SyntaxKind.StringLiteralExpression,
                                                Literal(group.Name)
                                            )
                                        )
                                    )
                                )
                            ),
                            GenericName(Identifier("Action"))
                            .WithTypeArgumentList
                            (
                                TypeArgumentList
                                (
                                    SeparatedList <TypeSyntax>
                                    (
                                        new SyntaxNodeOrToken[]
     {
         typeSyntax(action.ModelTemplate),
         Token(SyntaxKind.CommaToken),
         typeSyntax(action.ResultTemplate)
     }
                                    )
                                )
                            )
                        )
                    )
                    .WithArgumentList
                    (
                        ArgumentList
                        (
                            SingletonSeparatedList
                            (
                                Argument
                                (
                                    LiteralExpression
                                    (
                                        SyntaxKind.StringLiteralExpression,
                                        Literal(action.Name)
                                    )
                                )
                            )
                        )
                    ),
                    IdentifierName("Execute")
                )
            )
            .WithArgumentList
            (
                ArgumentList
                (
                    SeparatedList
                    (
                        new ArgumentSyntax[]
     {
         Argument
         (
             action.HasEmptyModel()
                                 ? (ExpressionSyntax)newEmptyRequest()
                                 : IdentifierName("model")
         )
     }
                    )
                )
            ));
 }
    private MethodDeclarationSyntax actionDeclarationForPost(AppApiGroupTemplate group, AppApiActionTemplate action)
    {
        var actionDeclaration =
            MethodDeclaration
            (
                GenericName(Identifier("Task")
                            )
                .WithTypeArgumentList
                (
                    TypeArgumentList
                    (
                        SingletonSeparatedList <TypeSyntax>
                        (
                            GenericName(Identifier("ResultContainer"))
                            .WithTypeArgumentList
                            (
                                TypeArgumentList
                                (
                                    SingletonSeparatedList
                                    (
                                        typeSyntax(action.ResultTemplate)
                                    )
                                )
                            )
                        )
                    )
                ),
                Identifier(action.Name)
            )
            .WithAttributeLists
            (
                SingletonList
                (
                    AttributeList
                    (
                        SingletonSeparatedList
                        (
                            Attribute(IdentifierName("HttpPost"))
                        )
                    )
                )
            )
            .WithModifiers
            (
                TokenList(Token(SyntaxKind.PublicKeyword))
            );

        if (!action.HasEmptyModel())
        {
            actionDeclaration = actionDeclaration
                                .WithParameterList
                                (
                ParameterList
                (
                    SingletonSeparatedList
                    (
                        Parameter(Identifier("model"))
                        .WithType(typeSyntax(action.ModelTemplate))
                        .WithAttributeLists
                        (
                            SingletonList
                            (
                                AttributeList
                                (
                                    SingletonSeparatedList
                                    (
                                        Attribute(IdentifierName("FromBody"))
                                    )
                                )
                            )
                        )
                    )
                )
                                );
        }
        return(actionDeclaration
               .WithBody
               (
                   Block
                   (
                       SingletonList <StatementSyntax>
                       (
                           ReturnStatement
                           (
                               executeInvocationExpression(group, action)
                           )
                       )
                   )
               ));
    }
    private MethodDeclarationSyntax actionDeclarationForView(AppApiGroupTemplate group, AppApiActionTemplate action)
    {
        var actionDeclaration = MethodDeclaration
                                (
            GenericName(Identifier("Task"))
            .WithTypeArgumentList
            (
                TypeArgumentList
                (
                    SingletonSeparatedList <TypeSyntax>
                    (
                        IdentifierName("IActionResult")
                    )
                )
            ),
            Identifier(action.Name)
                                )
                                .WithModifiers
                                (
            TokenList
            (
                new[]
        {
            Token(SyntaxKind.PublicKeyword),
            Token(SyntaxKind.AsyncKeyword)
        }
            )
                                )
                                .WithBody
                                (
            Block
            (
                LocalDeclarationStatement
                (
                    VariableDeclaration(IdentifierName("var"))
                    .WithVariables
                    (
                        SingletonSeparatedList
                        (
                            VariableDeclarator(Identifier("result"))
                            .WithInitializer
                            (
                                EqualsValueClause
                                (
                                    AwaitExpression
                                    (
                                        executeInvocationExpression(group, action)
                                    )
                                )
                            )
                        )
                    )
                ),
                ReturnStatement
                (
                    InvocationExpression(IdentifierName(action.IsView() ? "View" : "PartialView"))
                    .WithArgumentList
                    (
                        ArgumentList
                        (
                            SingletonSeparatedList
                            (
                                Argument
                                (
                                    MemberAccessExpression
                                    (
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        MemberAccessExpression
                                        (
                                            SyntaxKind.SimpleMemberAccessExpression,
                                            IdentifierName("result"),
                                            IdentifierName("Data")
                                        ),
                                        IdentifierName("ViewName")
                                    )
                                )
                            )
                        )
                    )
                )
            )
                                );

        if (!action.HasEmptyModel())
        {
            actionDeclaration = actionDeclaration
                                .WithParameterList
                                (
                ParameterList
                (
                    SingletonSeparatedList
                    (
                        Parameter(Identifier("model"))
                        .WithType(typeSyntax(action.ModelTemplate))
                    )
                )
                                );
        }
        if (action.IsPartialView())
        {
            actionDeclaration = actionDeclaration
                                .WithAttributeLists
                                (
                SingletonList
                (
                    AttributeList
                    (
                        SingletonSeparatedList
                        (
                            Attribute(IdentifierName("ResponseCache"))
                            .WithArgumentList
                            (
                                AttributeArgumentList
                                (
                                    SingletonSeparatedList
                                    (
                                        AttributeArgument
                                        (
                                            LiteralExpression
                                            (
                                                SyntaxKind.StringLiteralExpression,
                                                Literal("Default")
                                            )
                                        )
                                        .WithNameEquals
                                        (
                                            NameEquals(IdentifierName("CacheProfileName"))
                                        )
                                    )
                                )
                            )
                        )
                    )
                )
                                );
        }
        return(actionDeclaration);
    }
    private MethodDeclarationSyntax actionDeclarationForRedirect(AppApiGroupTemplate group, AppApiActionTemplate action)
    {
        var actionDeclaration = MethodDeclaration
                                (
            GenericName(Identifier("Task"))
            .WithTypeArgumentList
            (
                TypeArgumentList
                (
                    SingletonSeparatedList <TypeSyntax>
                    (
                        IdentifierName("IActionResult")
                    )
                )
            ),
            Identifier(action.Name)
                                )
                                .WithModifiers
                                (
            TokenList
            (
                new[]
        {
            Token(SyntaxKind.PublicKeyword),
            Token(SyntaxKind.AsyncKeyword)
        }
            )
                                )
                                .WithBody
                                (
            Block
            (
                LocalDeclarationStatement
                (
                    VariableDeclaration
                    (
                        IdentifierName("var")
                    )
                    .WithVariables
                    (
                        SingletonSeparatedList
                        (
                            VariableDeclarator(Identifier("result"))
                            .WithInitializer
                            (
                                EqualsValueClause
                                (
                                    AwaitExpression
                                    (
                                        executeInvocationExpression(group, action)
                                    )
                                )
                            )
                        )
                    )
                ),
                ReturnStatement
                (
                    InvocationExpression(IdentifierName("Redirect"))
                    .WithArgumentList
                    (
                        ArgumentList
                        (
                            SingletonSeparatedList
                            (
                                Argument
                                (
                                    MemberAccessExpression
                                    (
                                        SyntaxKind.SimpleMemberAccessExpression,
                                        MemberAccessExpression
                                        (
                                            SyntaxKind.SimpleMemberAccessExpression,
                                            IdentifierName("result"),
                                            IdentifierName("Data")
                                        ),
                                        IdentifierName("Url")
                                    )
                                )
                            )
                        )
                    )
                )
            )
                                );

        if (!action.HasEmptyModel())
        {
            actionDeclaration = actionDeclaration
                                .WithParameterList
                                (
                ParameterList
                (
                    SingletonSeparatedList
                    (
                        Parameter(Identifier("model"))
                        .WithType(typeSyntax(action.ModelTemplate))
                    )
                )
                                );
        }
        return(actionDeclaration);
    }