public static ResponseClassDefinition GetResponseClassDefinition(this AspNetCoreProject project)
 => new ResponseClassDefinition
 {
     Namespaces =
     {
         "System"
     },
     Namespace  = project.GetResponsesNamespace(),
     Name       = "Response",
     Implements =
     {
         "IResponse"
     },
     Properties =
     {
         new PropertyDefinition("string", "Message"),
         new PropertyDefinition("bool",   "DidError"),
         new PropertyDefinition("string", "ErrorMessage")
     }
 };
 public static PagedResponseClassDefinition GetPagedResponseClassDefinition(this AspNetCoreProject project)
 => new PagedResponseClassDefinition
 {
     Namespaces =
     {
         "System",
         "System.Collections.Generic"
     },
     Namespace    = project.GetResponsesNamespace(),
     Name         = "PagedResponse",
     GenericTypes =
     {
         new GenericTypeDefinition
         {
             Name       = "TModel",
             Constraint = "TModel : class"
         }
     },
     Implements =
     {
         "IListResponse<TModel>"
     },
     Properties =
     {
         new PropertyDefinition("string",              "Message"),
         new PropertyDefinition("bool",                "DidError"),
         new PropertyDefinition("string",              "ErrorMessage"),
         new PropertyDefinition("IEnumerable<TModel>", "Model"),
         new PropertyDefinition("int",                 "PageSize"),
         new PropertyDefinition("int",                 "PageNumber"),
         new PropertyDefinition("int",                 "ItemsCount"),
         new PropertyDefinition("double",              "PageCount")
         {
             IsReadOnly = true,
             GetBody    =
             {
                 new CodeLine("ItemsCount < PageSize ? 1 : (int)(((double)ItemsCount / PageSize) + 1);")
             }
         }
     }
 };
        public static CSharpInterfaceDefinition GetPagedResponseInterfaceDefinition(this AspNetCoreProject project)
        {
            var definition = new CSharpInterfaceDefinition();

            definition.Namespaces.Add("System");
            definition.Namespace = project.GetResponsesNamespace();
            definition.Name      = "PagedResponse";

            definition.GenericTypes = new List <GenericTypeDefinition>
            {
                new GenericTypeDefinition {
                    Name = "TModel", Constraint = "TModel : class"
                }
            };

            definition.Implements.Add("IListResponse");
            definition.Properties.Add(new PropertyDefinition("Int32", "ItemsCount"));
            definition.Properties.Add(new PropertyDefinition("Int32", "PageCount"));

            return(definition);
        }
Ejemplo n.º 4
0
 public static SingleResponseInterfaceDefinition GetSingleResponseInterfaceDefinition(this AspNetCoreProject project)
 => new SingleResponseInterfaceDefinition
 {
     Namespace    = project.GetResponsesNamespace(),
     Name         = "ISingleResponse",
     GenericTypes =
     {
         new GenericTypeDefinition
         {
             Name       = "TModel",
             Constraint = "TModel : class"
         }
     },
     Implements =
     {
         "IResponse"
     },
     Properties =
     {
         new PropertyDefinition("TModel", "Model")
     }
 };
        public static CSharpClassDefinition GetSingleResponseClassDefinition(this AspNetCoreProject project)
        {
            var definition = new CSharpClassDefinition();

            definition.Namespaces.Add("System");
            definition.Namespace = project.GetResponsesNamespace();
            definition.Name      = "SingleResponse";

            definition.GenericTypes = new List <GenericTypeDefinition>
            {
                new GenericTypeDefinition {
                    Name = "TModel", Constraint = "TModel : class"
                }
            };

            definition.Implements.Add("ISingleResponse<TModel>");
            definition.Properties.Add(new PropertyDefinition("String", "Message"));
            definition.Properties.Add(new PropertyDefinition("Boolean", "DidError"));
            definition.Properties.Add(new PropertyDefinition("String", "ErrorMessage"));
            definition.Properties.Add(new PropertyDefinition("TModel", "Model"));

            return(definition);
        }
Ejemplo n.º 6
0
        public static ResponsesExtensionClassDefinition GetResponsesExtensionsClassDefinition(this AspNetCoreProject project)
        {
            var definition = new ResponsesExtensionClassDefinition
            {
                Namespaces =
                {
                    "System",
                    "System.Net",
                    "Microsoft.AspNetCore.Mvc",
                    "Microsoft.Extensions.Logging"
                },
                Namespace = project.GetResponsesNamespace(),
                IsStatic  = true,
                Name      = "ResponsesExtensions"
            };

            definition.Methods.Add(new MethodDefinition("void", "SetError", new ParameterDefinition("IResponse", "response"), new ParameterDefinition("ILogger", "logger"), new ParameterDefinition("string", "action"), new ParameterDefinition("Exception", "ex"))
            {
                IsStatic    = true,
                IsExtension = true,
                Lines       =
                {
                    new CodeLine("response.DidError = true;"),
                    new CodeLine("response.ErrorMessage = ex.Message;"),
                    new CodeLine(),
                    new TodoLine("Add additional logic to save exception"),
                    new CodeLine("logger?.LogCritical(\"There was an error on '{0}': {1}\", action, ex);")
                }
            });

            definition.Methods.Add(new MethodDefinition("IActionResult ", "ToHttpResponse", new ParameterDefinition("IResponse", "response"))
            {
                IsStatic    = true,
                IsExtension = true,
                Lines       =
                {
                    new CodeLine("var status = HttpStatusCode.OK;"),
                    new CodeLine(),
                    new CodeLine("if (response.DidError)"),
                    new CodeLine(1,                                   "status = HttpStatusCode.InternalServerError;"),
                    new CodeLine(),
                    new CodeLine("return new ObjectResult(response)"),
                    new CodeLine("{"),
                    new CodeLine(1,                                   "StatusCode = (int)status"),
                    new CodeLine("};")
                }
            });

            definition.Methods.Add(new MethodDefinition("IActionResult ", "ToHttpResponse", new ParameterDefinition("ISingleResponse<TModel>", "response"))
            {
                IsStatic     = true,
                IsExtension  = true,
                GenericTypes =
                {
                    new GenericTypeDefinition
                    {
                        Name       = "TModel",
                        Constraint = "TModel : class"
                    }
                },
                Lines =
                {
                    new CodeLine("var status = HttpStatusCode.OK;"),
                    new CodeLine(),
                    new CodeLine("if (response.Model == null)"),
                    new CodeLine(1,                                   "status = HttpStatusCode.NotFound;"),
                    new CodeLine(),
                    new CodeLine("if (response.DidError)"),
                    new CodeLine(1,                                   "status = HttpStatusCode.InternalServerError;"),
                    new CodeLine(),
                    new CodeLine("return new ObjectResult(response)"),
                    new CodeLine("{"),
                    new CodeLine(1,                                   "StatusCode = (int)status"),
                    new CodeLine("};")
                }
            });

            definition.Methods.Add(new MethodDefinition("IActionResult ", "ToHttpResponse", new ParameterDefinition("IListResponse<TModel>", "response"))
            {
                IsStatic     = true,
                IsExtension  = true,
                GenericTypes =
                {
                    new GenericTypeDefinition
                    {
                        Name       = "TModel",
                        Constraint = "TModel : class"
                    }
                },
                Lines =
                {
                    new CodeLine("var status = HttpStatusCode.OK;"),
                    new CodeLine(),
                    new CodeLine("if (response.Model == null)"),
                    new CodeLine(1,                                   "status = HttpStatusCode.NoContent;"),
                    new CodeLine(),
                    new CodeLine("if (response.DidError)"),
                    new CodeLine(1,                                   "status = HttpStatusCode.InternalServerError;"),
                    new CodeLine(),
                    new CodeLine("return new ObjectResult(response)"),
                    new CodeLine("{"),
                    new CodeLine(1,                                   "StatusCode = (int)status"),
                    new CodeLine("};")
                }
            });

            return(definition);
        }
        public static CSharpClassDefinition GetResponsesExtensionsClassDefinition(this AspNetCoreProject project)
        {
            var classDefinition = new CSharpClassDefinition
            {
                Namespaces = new List <string>
                {
                    "System",
                    "System.Net",
                    "Microsoft.AspNetCore.Mvc",
                    "Microsoft.Extensions.Logging"
                },
                Namespace = project.GetResponsesNamespace(),
                Name      = "ResponsesExtensions",
                IsStatic  = true
            };

            classDefinition.Methods.Add(new MethodDefinition("void", "SetError", new ParameterDefinition("IResponse", "response"), new ParameterDefinition("Exception", "ex"), new ParameterDefinition("ILogger", "logger"))
            {
                IsStatic    = true,
                IsExtension = true,
                Lines       = new List <ILine>
                {
                    new CodeLine("response.DidError = true;"),
                    new CodeLine("response.ErrorMessage = ex.Message;"),
                    new CodeLine(),
                    new TodoLine("Add logic to save exception in file"),
                    new CodeLine("logger?.LogError(ex.ToString());")
                }
            });

            classDefinition.Methods.Add(new MethodDefinition("IActionResult ", "ToHttpResponse", new ParameterDefinition("ISingleResponse<TModel>", "response"))
            {
                IsStatic     = true,
                IsExtension  = true,
                GenericTypes = new List <GenericTypeDefinition>
                {
                    new GenericTypeDefinition {
                        Name = "TModel", Constraint = "TModel : class"
                    }
                },
                Lines = new List <ILine>
                {
                    new CodeLine("var status = HttpStatusCode.OK;"),
                    new CodeLine(),
                    new CodeLine("if (response.Model == null)"),
                    new CodeLine("{"),
                    new CodeLine(1, "status = HttpStatusCode.NotFound;"),
                    new CodeLine("}"),
                    new CodeLine(),
                    new CodeLine("if (response.DidError)"),
                    new CodeLine("{"),
                    new CodeLine(1, "status = HttpStatusCode.InternalServerError;"),
                    new CodeLine("}"),
                    new CodeLine(),
                    new CodeLine("return new ObjectResult(response)"),
                    new CodeLine("{"),
                    new CodeLine(1, "StatusCode = (Int32)status"),
                    new CodeLine("};")
                }
            });

            classDefinition.Methods.Add(new MethodDefinition("IActionResult ", "ToHttpResponse", new ParameterDefinition("IListResponse<TModel>", "response"))
            {
                IsStatic     = true,
                IsExtension  = true,
                GenericTypes = new List <GenericTypeDefinition>
                {
                    new GenericTypeDefinition {
                        Name = "TModel", Constraint = "TModel : class"
                    }
                },
                Lines = new List <ILine>
                {
                    new CodeLine("var status = HttpStatusCode.OK;"),
                    new CodeLine(),
                    new CodeLine("if (response.Model == null)"),
                    new CodeLine("{"),
                    new CodeLine(1, "status = HttpStatusCode.NoContent;"),
                    new CodeLine("}"),
                    new CodeLine(),
                    new CodeLine("if (response.DidError)"),
                    new CodeLine("{"),
                    new CodeLine(1, "status = HttpStatusCode.InternalServerError;"),
                    new CodeLine("}"),
                    new CodeLine(),
                    new CodeLine("return new ObjectResult(response)"),
                    new CodeLine("{"),
                    new CodeLine(1, "StatusCode = (Int32)status"),
                    new CodeLine("};")
                }
            });

            return(classDefinition);
        }