Exemple #1
0
        private CodeMemberMethod MakeUpdateLoadMethod(DomainClass domainClass, DomainMethod domainMethod)
        {
            var updateMethod = new CodeMemberMethod
            {
                Name       = $"{domainMethod.Name}",
                ReturnType = new CodeTypeReference("async Task<IActionResult>")
            };

            updateMethod.Parameters.Add(new CodeParameterDeclarationExpression
            {
                Type = new CodeTypeReference("Guid"),
                Name = "id"
            });
            updateMethod.Parameters.Add(new CodeParameterDeclarationExpression
            {
                Type = new CodeTypeReference($"[FromBody] {_nameBuilderUtil.UpdateApiCommandName(domainClass, domainMethod)}"),
                Name = "command"
            });

            updateMethod.Statements.Add(
                new CodeSnippetExpression($"return await Handler.{domainMethod.Name}{domainClass.Name}(id, command)"));

            updateMethod.Attributes = MemberAttributes.Final | MemberAttributes.Public;
            return(updateMethod);
        }
Exemple #2
0
        public CodeTypeMember BuildUpdateMethod(DomainMethod domainMethod, DomainClass domainClass)
        {
            var method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;

            var name = _nameBuilderUtil.UpdateCommandName(domainClass, domainMethod);

            method.Parameters.Add(new CodeParameterDeclarationExpression {
                Type = new CodeTypeReference("Guid"), Name = "id"
            });
            method.Parameters.Add(new CodeParameterDeclarationExpression {
                Type = new CodeTypeReference(name), Name = "command"
            });

            method.Name       = $"{domainMethod.Name}{domainClass.Name}";
            method.ReturnType = new CodeTypeReference("async Task<IActionResult>");

            method.Statements.Add(new CodeSnippetExpression($"var entity = await {domainClass.Name}Repository.Get{domainClass.Name}(id)"));
            var conditionalStatement = new CodeConditionStatement(
                new CodeSnippetExpression("entity != null"),
                new CodeStatement[]
            {
                new CodeExpressionStatement(new CodeSnippetExpression($"var validationResult = entity.{domainMethod.Name}(command)")),
                new CodeConditionStatement(
                    new CodeSnippetExpression("validationResult.Ok"),
                    new CodeStatement[] {
                    new CodeExpressionStatement(new CodeSnippetExpression($"var hookResult = await EventStore.AppendAll(validationResult.DomainEvents)")),
                    new CodeConditionStatement(
                        new CodeSnippetExpression("hookResult.Ok"),
                        new CodeStatement[]
                    {
                        new CodeExpressionStatement(new CodeSnippetExpression($"await {domainClass.Name}Repository.Update{domainClass.Name}(entity)")),
                        new CodeExpressionStatement(new CodeSnippetExpression("return new OkResult()")),
                    }
                        ),
                    new CodeExpressionStatement(new CodeSnippetExpression("return new BadRequestObjectResult(hookResult.Errors)"))
                }),
                new CodeExpressionStatement(new CodeSnippetExpression(@"return new BadRequestObjectResult(validationResult.DomainErrors)"))
            });

            method.Statements.Add(conditionalStatement);
            method.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression($@"new NotFoundObjectResult(new List<string> {{ $""Could not find Root {domainClass.Name} with ID: {{id}}"" }})")));

            return(method);
        }
Exemple #3
0
        public CodeNamespace Build(DomainMethod method, DomainClass domainClass)
        {
            var commandNameSpace = _nameSpaceBuilderUtil.WithName($"Application.{domainClass.Name}s").WithList().Build();
            var commandName      = _nameBuilderUtil.UpdateApiCommandName(domainClass, method);
            var properties       = method.Parameters.Select(param => new Property {
                Name = param.Name, Type = param.Type
            }).ToList();
            var loadProperties = method.LoadParameters.Select(param => new Property {
                Name = $"{param.Name}Id", Type = "Guid"
            }).ToList();

            properties.AddRange(loadProperties);
            var command         = _classBuilderUtil.Build(commandName);
            var codeConstructor = _constructorBuilderUtil.BuildPublic(properties);

            _propertyBuilderUtil.Build(command, properties);
            command.Members.Add(codeConstructor);
            commandNameSpace.Types.Add(command);
            return(commandNameSpace);
        }
Exemple #4
0
 public string UpdateApiCommandName(DomainClass domainClass, DomainMethod method)
 {
     return($"{domainClass.Name}{method.Name}ApiCommand");
 }
Exemple #5
0
 public string EventName(DomainClass domainClass, DomainMethod method)
 {
     return($"{domainClass.Name}{method.Name}Event");
 }
Exemple #6
0
        public CodeTypeMember BuildUpdateLoadMethod(DomainMethod domainMethod, DomainClass domainClass)
        {
            var method = new CodeMemberMethod();

            method.Attributes = MemberAttributes.Public | MemberAttributes.Final;

            var name = _nameBuilderUtil.UpdateApiCommandName(domainClass, domainMethod);

            method.Parameters.Add(new CodeParameterDeclarationExpression {
                Type = new CodeTypeReference("Guid"), Name = "id"
            });
            method.Parameters.Add(new CodeParameterDeclarationExpression {
                Type = new CodeTypeReference(name), Name = "apiCommand"
            });

            method.Name       = $"{domainMethod.Name}{domainClass.Name}";
            method.ReturnType = new CodeTypeReference("async Task<IActionResult>");

            var newListStatement = new CodeExpressionStatement(new CodeSnippetExpression($"var errorList = new List<string>()"));

            var codeStatements = new List <CodeStatement>();

            foreach (var loadParam in domainMethod.LoadParameters)
            {
                var codeExpressionStatement = new CodeExpressionStatement(new CodeSnippetExpression($"var {loadParam.Name} = await {loadParam.Type}Repository.Get{loadParam.Type}(apiCommand.{loadParam.Name}Id)"));
                var ifNullStatement         = new CodeExpressionStatement(new CodeSnippetExpression($"if ({loadParam.Name} == null) errorList.Add({_nameBuilderUtil.ErrorMessageFor(loadParam)})"));
                codeStatements.Add(codeExpressionStatement);
                codeStatements.Add(ifNullStatement);
            }

            var ifErrorListStatement = new CodeExpressionStatement(new CodeSnippetExpression($@"if (errorList.Count > 0) return new NotFoundObjectResult(errorList)"));

            var constArguments = domainMethod.LoadParameters.Select(param => param.Name);

            string constructorSignatur = String.Empty;

            foreach (var loadParam in constArguments)
            {
                constructorSignatur += $"{loadParam}, ";
            }

            constructorSignatur = constructorSignatur.Substring(0, constructorSignatur.Length - 2);

            var newCommandStatement = new CodeExpressionStatement(new CodeSnippetExpression($"var command = new {_nameBuilderUtil.UpdateCommandName(domainClass, domainMethod)}({constructorSignatur})"));

            method.Statements.Add(new CodeSnippetExpression($"var entity = await {domainClass.Name}Repository.Get{domainClass.Name}(id)"));
            var ifEntityFoundStatements = new CodeStatement[]
            {
                new CodeExpressionStatement(new CodeSnippetExpression($"var validationResult = entity.{domainMethod.Name}(command)")),
                new CodeConditionStatement(
                    new CodeSnippetExpression("validationResult.Ok"),
                    new CodeStatement[] {
                    new CodeExpressionStatement(new CodeSnippetExpression($"var hookResult = await EventStore.AppendAll(validationResult.DomainEvents)")),
                    new CodeConditionStatement(
                        new CodeSnippetExpression("hookResult.Ok"),
                        new CodeStatement[]
                    {
                        new CodeExpressionStatement(new CodeSnippetExpression($"await {domainClass.Name}Repository.Update{domainClass.Name}(entity)")),
                        new CodeExpressionStatement(new CodeSnippetExpression("return new OkResult()")),
                    }
                        ),
                    new CodeExpressionStatement(new CodeSnippetExpression("return new BadRequestObjectResult(hookResult.Errors)"))
                }),
                new CodeExpressionStatement(new CodeSnippetExpression(@"return new BadRequestObjectResult(validationResult.DomainErrors)"))
            };
            var statements = ifEntityFoundStatements.ToList();

            statements.Insert(0, newCommandStatement);
            statements.Insert(0, ifErrorListStatement);
            statements.InsertRange(0, codeStatements);
            statements.Insert(0, newListStatement);

            var conditionalStatement = new CodeConditionStatement(
                new CodeSnippetExpression("entity != null"),
                statements.ToArray());

            method.Statements.Add(conditionalStatement);
            method.Statements.Add(new CodeMethodReturnStatement(new CodeSnippetExpression($@"new NotFoundObjectResult(new List<string> {{ $""Could not find Root {domainClass.Name} with ID: {{id}}"" }})")));

            return(method);
        }