예제 #1
0
        public ActionResult Generate(int appId = 0, string[] selectedTables = null, string selectedTheme = "")
        {
            try
            {
                string      userName    = User.Identity.Name;
                Application application = applicationRepository.FindFirst(ap => ap.Member.username == userName && ap.Id == appId);

                if (application != null)
                {
                    bool connectionSuccess = CheckConnection(application);
                    if (connectionSuccess == true)
                    {
                        DeleteExistsFile(application);

                        //Change Application Theme
                        application.theme_id = GetTheme(selectedTheme);
                        applicationRepository.Edit(application);

                        //Create Application Folders
                        string appMainDirectory = CreateAppDirectories(application);
                        if (!string.IsNullOrEmpty(appMainDirectory))
                        {
                            //Generate Database files
                            string connectionString = Globals.GetSQLServerConnectionString(application);
                            string appName          = application.Name;

                            var myGenerator = new MyGenerator(connectionString, appName, appMainDirectory);

                            //App Tables
                            var tablesType = myGenerator.DatabaseTables.Where(tb => selectedTables.Contains(tb.Name)).ToList();

                            //Create application models and mapping
                            myGenerator.GenerateCode();

                            CreateContextAndConfig(myGenerator.DatabaseTables.ToList(), application, appMainDirectory);

                            //Create application controllers
                            var controllerGenerator = new ControllerGenerator(myGenerator, appName, appMainDirectory);
                            controllerGenerator.Generate(tablesType, connectionString);

                            //Create application navigation menu
                            ApplicationUtilities utilities = new ApplicationUtilities(appMainDirectory, application.Name);
                            utilities.CreateAppNav(tablesType);

                            //Create Application project file
                            string[] controllers = tablesType.Select(tp => tp.Name).ToArray <string>();
                            string[] allModels   = myGenerator.DatabaseTables.Select(tp => tp.Name).ToArray <string>();
                            utilities.CreateProjectFile(controllers, allModels, application.Theme.FileName);

                            //Create Home Controller
                            utilities.CreateControllerHome(tablesType);

                            application.generated = true;
                            applicationRepository.Edit(application);

                            //Zip Application Folder
                            string member_public_id = application.Member.public_id;
                            utilities.ZipApplication(application);

                            //Check the application has folders or not
                            //DirectoryUtility.DeleteFolder(Globals.APP_DATA_PATH + "\\" + member_public_id + "\\" + application.Name + "_" + application.Id);

                            return(View("Success", new MessageView()
                            {
                                Message = "Your application has been generated successfully."
                            }));
                        }
                    }
                    else
                    {
                        return(View("Error", new MessageView()
                        {
                            Message = "An error while connecting to your database."
                        }));
                    }
                }
                ViewBag.appId = application.Id;
                return(View());
            }
            catch (Exception ex)
            {
                return(View("Error", new MessageView()
                {
                    Message = "An error occured while generating the application"
                }));
            }
        }