Ejemplo n.º 1
0
        public ActionResult ExportNode(DbContextIndexModel model, string[] ids)
        {
            if (ids != null && ids.Length > 0)
            {
                var zipSrcFolder = HttpContext.Server.MapPath("~/tmp/" + new Random().Next(100000, 999999).ToString());
                var zipFileName = zipSrcFolder + ".zip";

                Directory.CreateDirectory(zipSrcFolder);
                Directory.CreateDirectory(Path.Combine(zipSrcFolder, "models"));
                Directory.CreateDirectory(Path.Combine(zipSrcFolder, "boundeds"));
                List<Type> boundedTypes = new List<Type>();
                foreach (var id in ids)
                {
                    var dbContextType = Type.GetType(id);
                    var tables = dbContextType.GetProperties()
                        .Where(x =>
                            x.PropertyType.IsGenericType &&
                            x.PropertyType.GetGenericTypeDefinition() == typeof(DbSet<>));

                    foreach (var table in tables)
                    {
                        var tableType = table.PropertyType.GetGenericArguments()[0];
                        StringBuilder builder = new StringBuilder();
                        builder.AppendLine("var uuid = require('uuid');");
                        builder.AppendLine("var requireOptional = require('require-optional');");
                        builder.AppendLine("module.exports = {");
                        builder.AppendLine("    tableName: '" + table.Name.ToLower() + "',");
                        builder.AppendLine("    name: '" + tableType.Name + "',");
                        builder.AppendLine("    schema: {");
                        var columns = tableType.GetProperties().Where(x => x.GetCustomAttribute<DbContextExportAttribute>() == null || !x.GetCustomAttribute<DbContextExportAttribute>().Ignore).ToList();
                        for (int i = 0; i < columns.Count; i++)
                        {
                            var column = columns[i];
                            var attr = column.GetCustomAttribute<DbContextExportAttribute>();
                            var propertyName = attr != null && !string.IsNullOrEmpty(attr.Name) ? GetFieldName(attr.Name) : GetFieldName(column.Name);
                            builder.AppendLine(string.Format("        {0}: {{", propertyName));
                            BuildNodeType(column, builder);
                            if (i == columns.Count - 1)
                            {
                                builder.AppendLine("        }");
                            }
                            else
                            {
                                builder.AppendLine("        },");
                            }

                            var propType = column.PropertyType.ToWithoutNullableType();
                            if (propType.IsEnum)
                            {
                                if (!boundedTypes.Contains(propType))
                                {
                                    boundedTypes.Add(propType);
                                }
                            }
                        }
                        builder.AppendLine("    },");
                        builder.AppendLine(string.Format("    constructor: requireOptional('./{0}.ctor.js') || function(){{}},", tableType.Name.ToLower()));
                        builder.AppendLine(string.Format("    instanceActions: requireOptional('./{0}.instances.js') || {{}},", tableType.Name.ToLower()));

                        builder.AppendLine("}");
                        System.IO.File.WriteAllText(Path.Combine(zipSrcFolder, "models", tableType.Name.ToLower() + ".model.js"), builder.ToString());
                    }
                }
                foreach (var boundedType in boundedTypes)
                {
                    var text = BuildBoundedType(boundedType);
                    System.IO.File.WriteAllText(Path.Combine(zipSrcFolder, "boundeds", boundedType.Name.ToLower() + ".js"), text);
                }
                ZipFile.CreateFromDirectory(zipSrcFolder, zipFileName);
                return File(System.IO.File.ReadAllBytes(zipFileName), "application/zip", "dbcontext.zip");
            }
            return Template(model.CreateTemplate(Request.RequestContext, MaintDbContext));
        }
Ejemplo n.º 2
0
 public ActionResult Index(DbContextIndexModel model)
 {
     return Template(model.CreateTemplate(Request.RequestContext, MaintDbContext));
 }