Exemplo n.º 1
0
        public EntityFile Generate(IEntityType entity)
        {
            var template = new Controller();

            template.EntityName   = entity.ClrType.Name;
            template.EntityIdName = entity.GetKeys().First().Properties.First().Name;

            // Critter.Include(c => c.CritterType).Include(c => c.DadCritter).Include(c => c.MumCritter)
            template.ContextGetterString = template.EntityName;
            var includes = String.Join('.', entity.GetForeignKeys()
                                       .Select(fk => $"Include(v => v.{fk.DependentToPrincipal.Name})")
                                       );

            if (!String.IsNullOrWhiteSpace(includes))
            {
                template.ContextGetterString += "." + includes;
            }

            //ViewData["CritterTypeId"] = new SelectList(_context.CritterType, "CritterTypeId", "Comment");
            template.ForeignKeyDropDownCreationString = String.Join('\n', this.CreateFKDropDown(entity).Select(l => l + ");"));

            //ViewData["CritterTypeId"] = new SelectList(_context.CritterType, "CritterTypeId", "Comment", critter.CritterTypeId);
            if (entity.GetForeignKeys().Count() > 0)
            {
                template.ForeignKeyDropDownCreationWithSelectedIndexString
                    = this.CreateFKDropDown(entity)
                      .Zip(entity.GetForeignKeys()
                           .Select(fk => fk.Properties.First().Name),
                           (str, name) => str + $", val.{name}" + ");"
                           )
                      .Aggregate((one, two) => one + "\n" + two);
            }
            else
            {
                template.ForeignKeyDropDownCreationWithSelectedIndexString = "";
            }

            // "CritterTypeID,Description,Comment,Version, [etc.]"
            // SELF NOTE: All 'internal' fields should have a hidden input generated for them.
            template.FormBindingParams
                = entity.GetProperties()
                  .Select(p => p.Name)
                  .Aggregate((one, two) => one + "," + two);

            // HACK: Special support for Gender, since it has a max length of 1
            // if(String.IsNullOrWhitespace(val.Comment))
            //     val.Comment = "N/A";
            try
            {
                template.FixNullFieldsCode
                    = entity.GetProperties()
                      .Where(p => p.ClrType.UnderlyingSystemType == typeof(string))
                      .Select(p => $"if(String.IsNullOrWhiteSpace(val.{p.Name})) val.{p.Name} = \"{(p.Name == "Gender" ? "?" : "N/A")}\";")
                      .Aggregate((one, two) => one + "\n" + two);
            }
            catch
            {
                template.FixNullFieldsCode = "";
            }

            // Create the authorise attribute
            // [Authorize(RolesOR = "admin,staff")]
            string rolesOR = "";

            foreach (var item in db.MenuItem.Where(i => i.Controller == entity.ClrType.Name))
            {
                db.Entry(item).Collection(i => i.MenuHeaderItemMap).Load();
                foreach (var map in item.MenuHeaderItemMap)
                {
                    db.Entry(map).Reference(m => m.MenuHeader).Load();
                    db.Entry(map.MenuHeader).Reference(m => m.Role).Load();
                    rolesOR += map.MenuHeader.Role.Description + ",";
                }
            }

            if (String.IsNullOrWhiteSpace(rolesOR))
            {
                rolesOR = "[Forbidden to all]";
            }

            template.ControllerAuthAttrib = $"[Authorize(Roles = \"{rolesOR}\")]";

            return(new EntityFile()
            {
                data = template.TransformText(), path = Path.Join("Controllers/Generated/", entity.ClrType.Name + ".cs")
            });
        }