public static PagedResponseClassDefinition GetPagedResponseClassDefinition(this AspNetCoreProject project)
 => new PagedResponseClassDefinition
 {
     Namespaces =
     {
         "System",
         "System.Collections.Generic"
     },
     Namespace      = project.GetResponsesNamespace(),
     AccessModifier = AccessModifier.Public,
     Name           = "PagedResponse",
     GenericTypes   =
     {
         new GenericTypeDefinition
         {
             Name       = "TModel",
             Constraint = "TModel : class"
         }
     },
     Implements =
     {
         "IListResponse<TModel>"
     },
     Properties =
     {
         new PropertyDefinition(AccessModifier.Public, "string", "Message")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "bool", "DidError")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "string", "ErrorMessage")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "IEnumerable<TModel>", "Model")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "int", "PageSize")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "int", "PageNumber")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "int", "ItemsCount")
         {
             IsAutomatic = true
         },
         new PropertyDefinition(AccessModifier.Public, "double", "PageCount")
         {
             IsReadOnly = true,
             GetBody    =
             {
                 new CodeLine("ItemsCount < PageSize ? 1 : (int)(((double)ItemsCount / PageSize) + 1);")
             }
         }
     }
 };
Beispiel #2
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);
        }
Beispiel #3
0
 public static SingleResponseInterfaceDefinition GetSingleResponseInterfaceDefinition(this AspNetCoreProject project)
 => new SingleResponseInterfaceDefinition
 {
     Namespace      = project.GetResponsesNamespace(),
     AccessModifier = AccessModifier.Public,
     Name           = "ISingleResponse",
     GenericTypes   =
     {
         new GenericTypeDefinition
         {
             Name       = "TModel",
             Constraint = "TModel : class"
         }
     },
     Implements =
     {
         "IResponse"
     },
     Properties =
     {
         new PropertyDefinition("TModel", "Model")
     }
 };
        public static RequestModelExtensionsClassDefinition GetRequestExtensionsClassDefinition(this AspNetCoreProject project, ITable table)
        {
            var definition = new RequestModelExtensionsClassDefinition
            {
                Namespaces =
                {
                    "System",
                    project.GetEntityLayerNamespace()
                },
                Namespace      = project.GetRequestsNamespace(),
                AccessModifier = AccessModifier.Public,
                IsStatic       = true,
                Name           = string.Format("{0}RequestExtensions", project.EntityFrameworkCoreProject.GetEntityName(table))
            };

            if (!project.Database.HasDefaultSchema(table))
            {
                definition.Namespaces.AddUnique(project.GetEntityLayerNamespace(table.Schema));
            }

            definition.Methods.Add(GetToEntityMethod(project, table));
            definition.Methods.Add(GetToRequestModelMethod(project, table));

            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);
        }
 public static ListResponseInterfaceDefinition GetListResponseInterfaceDefinition(this AspNetCoreProject project)
 => new ListResponseInterfaceDefinition
 {
     Namespaces =
     {
         "System.Collections.Generic"
     },
     Namespace      = project.GetResponsesNamespace(),
     AccessModifier = AccessModifier.Public,
     Name           = "IListResponse",
     GenericTypes   =
     {
         new GenericTypeDefinition
         {
             Name       = "TModel",
             Constraint = "TModel : class"
         }
     },
     Implements =
     {
         "IResponse"
     },
     Properties =
     {
         new PropertyDefinition("IEnumerable<TModel>", "Model")
     }
 };