public static String Get(JsonSchema4 schema, String ns)
        {
            String Model, model;

            NameGenerator.CreatePascalAndCamel(schema.Title, out Model, out model);
            String Models, models;

            NameGenerator.CreatePascalAndCamel(schema.GetPluralName(), out Models, out models);
            String ModelId, modelId;

            NameGenerator.CreatePascalAndCamel(schema.GetKeyName(), out ModelId, out modelId);

            var    additionalAuthorize = "";
            String authName            = schema.GetAuthorizationRoleString();

            if (authName != null)
            {
                additionalAuthorize = $", Roles = {authName}";
            }
            return(Create(ns, Model, model, Models, models, additionalAuthorize, schema.GetKeyType().Name, ModelId, modelId, schema.GetExtraNamespaces(StrConstants.FileNewline)));
        }
        public static String Create(JsonSchema4 schema, Dictionary <String, JsonSchema4> others, String ns)
        {
            bool hasBase = false;

            var baseWriter = new BaseModelWriter("Entity", CreateAttributeBuilder());
            var baseClass  = ModelTypeGenerator.Create(schema, schema.GetPluralName(), baseWriter, ns, ns + ".Database", allowPropertyCallback: p =>
            {
                if (p.CreateEntity())
                {
                    hasBase = hasBase | p.IsAbstractOnEntity();
                    return(p.IsAbstractOnEntity());
                }
                return(false);
            });

            var mainWriter = new MainModelWriter(hasBase ? baseClass : null, "Entity", CreateAttributeBuilder(), new NoAttributeBuilder(), schema.AllowCreated(), schema.AllowModified(),
                                                 a =>
            {
                var interfaces = new String[] { a.BaseClassName, }
                .Concat(a.Writer.GetAdditionalInterfaces());

                a.Builder.AppendLine(
                    $@"    public partial class {a.Name}Entity{InterfaceListBuilder.Build(interfaces)}
    {{
        [Key]"
                    );

                a.Writer.CreateProperty(a.Builder, NameGenerator.CreatePascal(schema.GetKeyName()), new TypeWriterPropertyInfo(schema.GetKeyType()));
            }
                                                 )
            {
                AdditionalUsings = schema.GetExtraNamespaces(StrConstants.FileNewline)
            };

            return(ModelTypeGenerator.Create(schema, schema.GetPluralName(), mainWriter, ns, ns + ".Database",
                                             allowPropertyCallback: AllowProperty,
                                             additionalProperties: AdditionalProperties(schema, others)));
        }
예제 #3
0
        public static String Get(JsonSchema4 schema, String ns)
        {
            String Model, model;

            NameGenerator.CreatePascalAndCamel(schema.Title, out Model, out model);
            String Models, models;

            NameGenerator.CreatePascalAndCamel(schema.GetPluralName(), out Models, out models);
            String ModelId, modelId;

            NameGenerator.CreatePascalAndCamel(schema.GetKeyName(), out ModelId, out modelId);

            String queryProps = ModelTypeGenerator.Create(schema, schema.GetPluralName(), new QueryPropWriter(), schema, ns, ns, allowPropertyCallback: p =>
            {
                return(p.IsQueryable());
            });
            String customizer = ModelTypeGenerator.Create(schema, schema.GetPluralName(), new QueryCustomizerWriter(), schema, ns, ns, allowPropertyCallback: p =>
            {
                return(p.IsQueryable());
            });

            return(Create(ns, Model, model, Models, models, queryProps, customizer, schema.GetKeyType().GetTypeAsNullable(), ModelId, modelId, schema.GetExtraNamespaces(StrConstants.FileNewline)));
        }
        private static async Task GenerateClasses(GeneratorSettings settings)
        {
            if (settings.AppOutDir != null && settings.AppNamespace != null)
            {
                if (settings.WriteApp)
                {
                    //Entity
                    if (settings.Schema.CreateEntity())
                    {
                        if (settings.CreateGeneratedFiles)
                        {
                            WriteFile(settings.AppOutDir, EntityWriter.GetFileName(settings.Schema, false), PartialTypeGenerator.GetEntity(settings.Schema, settings.AppNamespace), false);
                        }
                        else
                        {
                            DeleteFile(settings.AppOutDir, EntityWriter.GetFileName(settings.Schema, true));
                        }
                        WriteFile(settings.AppOutDir, AppDbContextGenerator.GetFileName(settings.Schema), AppDbContextGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                        WriteFile(settings.AppOutDir, EntityWriter.GetFileName(settings.Schema, settings.CreateGeneratedFiles), EntityWriter.Create(settings.Schema, settings.OtherSchemas, settings.AppNamespace), true);

                        foreach (var relationship in settings.Schema.GetRelationshipSettings())
                        {
                            if (relationship.Kind == RelationKind.ManyToMany)
                            {
                                if (settings.CreateGeneratedFiles)
                                {
                                    WriteFile(settings.AppOutDir, JoinEntityWriter.GetFileName(relationship, false), PartialTypeGenerator.GetJoinEntity(settings.Schema, relationship, settings.AppNamespace), false);
                                }
                                else
                                {
                                    DeleteFile(settings.AppOutDir, JoinEntityWriter.GetFileName(relationship, true));
                                }
                                WriteFile(settings.AppOutDir, AppDbContextGenerator.GetManyToManyEntityDbContextFileName(relationship), AppDbContextGenerator.GetManyToManyEntityDbContext(relationship, settings.AppNamespace), settings.ForceWriteApi);
                                WriteFile(settings.AppOutDir, JoinEntityWriter.GetFileName(relationship, settings.CreateGeneratedFiles), JoinEntityWriter.Get(settings.Schema, settings.OtherSchemas, relationship, settings.AppNamespace), true);
                            }
                        }
                    }

                    //Input Model
                    if (settings.Schema.CreateInputModel())
                    {
                        if (settings.CreateGeneratedFiles)
                        {
                            WriteFile(settings.AppOutDir, InputModelWriter.GetFileName(settings.Schema, false), PartialTypeGenerator.GetInput(settings.Schema, settings.AppNamespace), false);
                        }
                        else
                        {
                            DeleteFile(settings.AppOutDir, InputModelWriter.GetFileName(settings.Schema, true));
                        }
                        WriteFile(settings.AppOutDir, InputModelWriter.GetFileName(settings.Schema, settings.CreateGeneratedFiles), await InputModelWriter.Create(settings.Schema, settings.OtherSchemas, settings.AppNamespace), true);
                    }

                    //Query Model
                    if (settings.Schema.CreateQuery())
                    {
                        if (settings.CreateGeneratedFiles)
                        {
                            WriteFile(settings.AppOutDir, QueryModelWriter.GetFileName(settings.Schema, false), QueryUserPartialGenerator.GetQuery(settings.Schema, settings.AppNamespace), false);
                        }
                        else
                        {
                            DeleteFile(settings.AppOutDir, QueryModelWriter.GetFileName(settings.Schema, true));
                        }
                        WriteFile(settings.AppOutDir, QueryModelWriter.GetFileName(settings.Schema, settings.CreateGeneratedFiles), QueryModelWriter.Get(settings.Schema, settings.AppNamespace, settings.CreateGeneratedFiles), true);
                    }

                    //View Model
                    if (settings.Schema.CreateViewModel())
                    {
                        if (settings.CreateGeneratedFiles)
                        {
                            WriteFile(settings.AppOutDir, ViewModelWriter.GetFileName(settings.Schema, false), ViewModelWriter.GetUserPartial(settings.Schema, settings.AppNamespace), false);
                        }
                        else
                        {
                            DeleteFile(settings.AppOutDir, ViewModelWriter.GetFileName(settings.Schema, true));
                        }
                        WriteFile(settings.AppOutDir, ViewModelWriter.GetFileName(settings.Schema, settings.CreateGeneratedFiles), await ViewModelWriter.Create(settings.Schema, settings.OtherSchemas, settings.AppNamespace, settings.CreateGeneratedFiles), true);
                    }

                    //Repository
                    if (settings.Schema.CreateRepository())
                    {
                        WriteFile(settings.AppOutDir, RepoGenerator.GetFileName(settings.Schema), RepoGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                        WriteFile(settings.AppOutDir, RepoInterfaceGenerator.GetFileName(settings.Schema), RepoInterfaceGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                        WriteFile(settings.AppOutDir, RepoConfigGenerator.GetFileName(settings.Schema), RepoConfigGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                    }

                    //Controller
                    if (settings.Schema.CreateController())
                    {
                        WriteFile(settings.AppOutDir, ControllerGenerator.GetFileName(settings.Schema), ControllerGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                        WriteFile(settings.AppOutDir, EntryPointGenerator.GetFileName(settings.Schema), EntryPointGenerator.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteApi);
                    }

                    //Mapping Profile
                    if (settings.Schema.CreateMappingProfile())
                    {
                        if (settings.CreateGeneratedFiles) //This if is more complicated because the generator is backward compared to the others
                        {
                            WriteFile(settings.AppOutDir, MappingProfileGenerator.GetFileName(settings.Schema, false), MappingProfileGenerator.Get(settings.Schema, settings.AppNamespace, true), settings.ForceWriteApi);
                            WriteFile(settings.AppOutDir, MappingProfileGenerator.GetFileName(settings.Schema, true), MappingProfileGenerator.GetGenerated(settings.Schema, settings.AppNamespace), true);
                        }
                        else
                        {
                            WriteFile(settings.AppOutDir, MappingProfileGenerator.GetFileName(settings.Schema, false), MappingProfileGenerator.Get(settings.Schema, settings.AppNamespace, false), true);
                            DeleteFile(settings.AppOutDir, MappingProfileGenerator.GetFileName(settings.Schema, true));
                        }
                    }

                    //Model Collection
                    if (settings.Schema.CreateModelCollection())
                    {
                        if (settings.CreateGeneratedFiles)
                        {
                            WriteFile(settings.AppOutDir, ModelCollectionGenerator.GetFileName(settings.Schema, false), ModelCollectionGenerator.GetUserPartial(settings.Schema, settings.AppNamespace), false);
                        }
                        else
                        {
                            DeleteFile(settings.AppOutDir, ModelCollectionGenerator.GetFileName(settings.Schema, true));
                        }
                        WriteFile(settings.AppOutDir, ModelCollectionGenerator.GetFileName(settings.Schema, settings.CreateGeneratedFiles), ModelCollectionGenerator.Get(settings.Schema, settings.AppNamespace, settings.CreateGeneratedFiles), true);
                    }

                    //Ui
                    if (settings.Schema.CreateUi())
                    {
                        var propertyNames = settings.Schema.Properties.Values.Where(i => i.CreateViewModel()).Select(i => NameGenerator.CreatePascal(i.Name));
                        WriteFile(settings.AppOutDir, CrudCshtmlInjectorGenerator.GetFileName(settings.Schema), CrudCshtmlInjectorGenerator.Get(settings.Schema, propertyNames: propertyNames), settings.ForceWriteUi);
                        WriteFile(settings.AppOutDir, CrudInjectorGenerator.GetFileName(settings.Schema), CrudInjectorGenerator.Get(settings.Schema), settings.ForceWriteUi);
                        WriteFile(settings.AppOutDir, CrudUiTypescriptGenerator.GetFileName(settings.Schema), CrudUiTypescriptGenerator.Get(settings.ModelName), settings.ForceWriteUi);
                        WriteFile(settings.AppOutDir, UiControllerGenerator.GetFileName(settings.Schema), UiControllerGenerator.Get(settings.Schema, settings.AppNamespace), true);
                    }
                }

                if (settings.WriteTests)
                {
                    WriteFile(settings.TestOutDir, ModelTestWrapper.GetFileName(settings.Schema), ModelTestWrapper.Get(settings.Schema, settings.AppNamespace, settings.CreateGeneratedFiles), settings.ForceWriteTests);
                    if (settings.CreateGeneratedFiles)
                    {
                        WriteFile(settings.TestOutDir, ModelTestWrapperGenerated.GetFileName(settings.Schema), ModelTestWrapperGenerated.Get(settings.Schema, settings.AppNamespace), true);
                    }

                    if (settings.Schema.CreateController())
                    {
                        if (!File.Exists(Path.Combine(settings.TestOutDir, $"{settings.ModelName}/Controller.cs"))) //Legacy File check
                        {
                            WriteFile(settings.TestOutDir, ControllerTests.GetFileName(settings.Schema), ControllerTests.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteTests);
                        }
                    }

                    if (settings.Schema.CreateMappingProfile())
                    {
                        if (!File.Exists(Path.Combine(settings.TestOutDir, $"{settings.ModelName}/Profile.cs"))) //Legacy File check
                        {
                            WriteFile(settings.TestOutDir, ProfileTests.GetFileName(settings.Schema), ProfileTests.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteTests);
                        }
                    }

                    if (settings.Schema.CreateRepository())
                    {
                        if (!File.Exists(Path.Combine(settings.TestOutDir, $"{settings.ModelName}/Repository.cs"))) //Legacy File check
                        {
                            WriteFile(settings.TestOutDir, RepositoryTests.GetFileName(settings.Schema), RepositoryTests.Get(settings.Schema, settings.AppNamespace), settings.ForceWriteTests);
                        }
                    }
                }
            }
        }
        public static String Get(JsonSchema4 schema, String ns, bool generated)
        {
            bool hasBase = false;

            var baseWriter = new QueryBaseModelWriter("Query", QueryPropertiesWriter.CreateAttributeBuilder())
            {
                InheritFrom = new String[] { "PagedCollectionQuery" }
            };
            var baseClass = ModelTypeGenerator.Create(schema, schema.GetPluralName(), baseWriter, ns, ns + ".Database", allowPropertyCallback: p =>
            {
                if (p.IsQueryable())
                {
                    hasBase = hasBase | p.IsAbstractOnQuery();
                    return(p.IsAbstractOnQuery());
                }
                return(false);
            });

            var baseClassName = "PagedCollectionQuery";

            if (hasBase)
            {
                baseClassName = $"{BaseModelWriter.CreateBaseClassName(schema.Title, "Query")}";
                baseClass     = $@"
{baseClass}
";
            }
            else
            {
                baseClass = "";
            }

            String Model, model;

            NameGenerator.CreatePascalAndCamel(schema.Title, out Model, out model);
            String Models, models;

            NameGenerator.CreatePascalAndCamel(schema.GetPluralName(), out Models, out models);
            String queryProps = ModelTypeGenerator.Create(schema, schema.GetPluralName(), new QueryPropertiesWriter(), schema, ns, ns, allowPropertyCallback: p =>
            {
                return(p.IsQueryable() && !p.IsAbstractOnQuery());
            });
            String queryCreate = ModelTypeGenerator.Create(schema, schema.GetPluralName(), new QueryCreateWriter(), schema, ns, ns, allowPropertyCallback: p =>
            {
                return(p.IsQueryable());
            });

            return(Create(ns, Model, model, Models, models, queryProps, queryCreate, schema.GetKeyType().GetTypeAsNullable(), baseClass, baseClassName, NameGenerator.CreatePascal(schema.GetKeyName()), schema.GetExtraNamespaces(StrConstants.FileNewline), generated));
        }
 public static String Get(String modelName)
 {
     return(Create(NameGenerator.CreatePascal(modelName)));
 }
예제 #7
0
        public static String Get(JsonSchema4 schema, String ns)
        {
            String Model, model;

            NameGenerator.CreatePascalAndCamel(schema.Title, out Model, out model);
            String Models, models;

            NameGenerator.CreatePascalAndCamel(schema.GetPluralName(), out Models, out models);
            return(Create(ns, Model, model, Models, models, schema.GetKeyType().Name, NameGenerator.CreateCamel(schema.GetKeyName())));
        }
        private static String Create(String Model, String model, String Models, String models, IEnumerable <String> propertyNames, bool hasQuery)
        {
            var cls = hasQuery ? @" class=""col-sm-8""" : null;
            var sb  = new StringBuilder(
                $@"@{{
    ViewData[""Title""] = ""{Models}"";
}}

<div data-hr-controller=""mainTable""{cls}>
    <h1>{Models}</h1>
    <load visible=""true"">
        <p>Working...</p>
    </load>
    <main>
        <div class=""table-responsive"">
            <table class=""table table-bordered"">
                <thead>
                    <tr>");

            sb.AppendLine();
            foreach (var name in propertyNames)
            {
                sb.AppendLine($"                        <th>{NameGenerator.CreatePascal(name)}</th>");
            }
            sb.Append(

                $@"                        <th>Edit</th>
                    </tr>
                </thead>
                <tbody data-hr-view=""listing"" data-hr-view-component=""mainTableBody""></tbody>
            </table>
            <template data-hr-component=""mainTableBody"">
                <table>
                    <tr>");

            sb.AppendLine();
            foreach (var name in propertyNames)
            {
                sb.AppendLine($"                        <td>{{{{{NameGenerator.CreateCamel(name)}}}}}</td>");
            }
            sb.Append(

                $@"                        <td>
                            <button data-hr-on-click=""edit"" class=""btn btn-default"" data-hr-toggle=""edit"" data-hr-style-off=""display:none;"">Edit</button>
                            <button data-hr-on-click=""view"" class=""btn btn-default"" data-hr-toggle=""view"" data-hr-style-off=""display:none;"">View</button>
                            <button data-hr-on-click=""del"" class=""btn btn-default"" data-hr-toggle=""del"" data-hr-style-off=""display:none;"">Delete</button>
                        </td>
                    </tr>
                </table>
            </template>
        </div>
        <button data-hr-on-click=""add"" class=""btn btn-default"" data-hr-toggle=""add"" data-hr-style-off=""display:none;"">Add</button>

        <page-numbers></page-numbers>
    </main>

    <error>
        An error occured loading the {models}. Please try again later.
    </error>
</div>");

            if (hasQuery)
            {
                sb.Append($@"
<div class=""col-sm-4"">
    <h3>Search</h3>
    <div data-hr-controller=""search"">
        <load visible=""true"">
            <p>Loading Search...</p>
        </load>
        <main>
            <form data-hr-on-submit=""submit"" data-hr-form=""input"">
                <p class=""bg-danger hiddenToggler"" data-hr-toggle=""mainError"" data-hr-view=""mainError"" data-hr-style-on=""display:block;"">{{{{message}}}}</p>

                <button type=""submit"" class=""btn btn-primary"" data-hr-form-end>Search</button>
            </form>
        </main>
        <error>
            An error occured loading the search form. Please try again later.
        </error>
    </div>
</div>");
            }

            sb.Append($@"

<modal data-hr-controller=""entryEditor"" title-text=""{Model}"" dialog-classes=""modal-lg"">
    <load class=""modal-body"">
        <p>Working...</p>
    </load>
    <main>
        <item-edit-form class=""form-horizontal"" data-hr-form-component=""hr.forms.horizontal""></item-edit-form>
    </main>
    <error class=""modal-body"">
        <p>An error occured loading the value. Please try again later.</p>
    </error>
</modal>");

            return(sb.ToString());
        }
예제 #9
0
        public static String Get(JsonSchema4 schema, String ns, bool generated)
        {
            String Model = NameGenerator.CreatePascal(schema.Title);

            return(Create(ns, Model, NameGenerator.CreatePascal(schema.GetKeyName()), schema.AllowCreated(), schema.AllowModified(), generated, schema.Properties.Values, schema.GetExtraNamespaces(StrConstants.FileNewline)));
        }