Пример #1
0
 public static void GenerateDelete(string viewsPath, TableModelCollection model, string projectName)
 {
     foreach (TableModel table in model.tableModels)
     {
         using (FileStream fs = new FileStream(viewsPath + "\\" + table.dbTable + "Delete.cshtml", FileMode.Create))
         {
             using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
             {
                 w.WriteLine("@page \"/" + table.dbTable.ToLower() + "/delete/{id}\"");
                 w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                 w.WriteLine("<button onclick=\"@Back\">Back</button>");
                 w.WriteLine("<h1>Delete " + table.dbTable + "</h1>\n");
                 w.WriteLine("Are you sure you want to delete this entity");
                 w.WriteLine("\t\t\t<td><button onclick=\"@Yes\">Yes</button> |<button onclick=\"@No\">No</button></td>");
                 w.WriteLine("@functions{");
                 w.WriteLine("\t[Parameter]\n\tprivate string Id {get; set;}");
                 w.WriteLine("\tvoid Back()\n\t{\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t}");
                 w.WriteLine("\tpublic async Task Yes(){\n\t\tawait Http.DeleteAsync(\"/api/" + table.dbTable.ToLower() + "/delete/\"+Id);" +
                             "\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t}");
                 w.WriteLine("\tpublic void No(){\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t}");
                 w.WriteLine("}");
             }
         }
     }
     foreach (NNModel nNModel in model.nnRelations)
     {
         using (FileStream fs = new FileStream(viewsPath + "\\" + nNModel.nnTable + "Delete.cshtml", FileMode.Create))
         {
             using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
             {
                 w.WriteLine("@page \"/" + nNModel.nnTable.ToLower() + "/{table}/delete/{id1}/{id2}\"");
                 w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                 w.WriteLine("<h1>Delete " + nNModel.nnTable + "</h1>\n");
                 w.WriteLine("Are you sure you want to delete this entity");
                 w.WriteLine("\t\t\t<td><button onclick=\"@Yes\">Yes</button> |<button onclick=\"@No\">No</button></td>");
                 w.WriteLine("@functions{");
                 w.WriteLine("\t[Parameter]\n\tprivate string Id1 {get; set;}");
                 w.WriteLine("\t[Parameter]\n\tprivate string Id2 {get; set;}");
                 w.WriteLine("\t[Parameter]\n\tprivate string Table {get; set;}");
                 w.WriteLine("\tpublic async Task Yes(){\n\t\tawait Http.DeleteAsync(\"/api/" + nNModel.nnTable.ToLower() + "/delete/\"+Id1+\"/\"+Id2);" +
                             "\n\t\turiHelper.NavigateTo(\"/\" + Table.ToLower() + \"s\");\n\t}");
                 w.WriteLine("\tpublic void No(){\n\t\turiHelper.NavigateTo(\"/\" + Table.ToLower() + \"s\");\n\t}");
                 w.WriteLine("}");
             }
         }
     }
 }
Пример #2
0
        public static void GenerateDataAccessLayer(string path, TableModelCollection model, string project)
        {
            foreach (TableModel table in model.tableModels)
            {
                using (FileStream fs = new FileStream(path + "\\" + table.dbTable + "AccessLayer.cs", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("using System;\nusing " + project + ".Server.Models;\n" + "using " + project + ".Shared.Models;\nusing System.Collections.Generic;\n");
                        w.WriteLine("using System.Linq;\nusing Microsoft.EntityFrameworkCore;");
                        w.WriteLine("namespace " + project + ".Server.DataAccess\n{");
                        w.WriteLine("\tpublic class " + table.dbTable + "AccessLayer\n{\n");
                        w.WriteLine("\t\tContext _context = new Context();\n");
                        //get all
                        w.WriteLine("\t\tpublic IEnumerable<" + table.dbTable + "> GetAll()\n\t\t{");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\treturn _context." + table.dbTable + ";\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n");
                        // post
                        w.WriteLine("\t\tpublic void Add(" + table.dbTable + " " + table.dbTable.ToLower() + "){");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\t_context." + table.dbTable + ".Add(" + table.dbTable.ToLower() + ");");
                        w.WriteLine("\t\t\t\t_context.SaveChanges();\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\n\t\t\t}\n\t\t}\n\n");
                        //update
                        w.WriteLine("\t\tpublic void Update(" + table.dbTable + " " + table.dbTable.ToLower() + "){");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\tif(" + table.dbTable.ToLower() + " != null){\n\t\t\t\t\t_context." + table.dbTable + ".Update(" + table.dbTable.ToLower() + ");");
                        w.WriteLine("\t\t\t\t\t_context.SaveChanges();\n\t\t\t\t}\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\n\t\t\t}\n\t\t}\n\n");
                        // get by id
                        w.WriteLine("\t\tpublic " + table.dbTable + " GetById(int id){");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\treturn _context." + table.dbTable + ".Find(id);\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch (Exception e){\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n");
                        //delete
                        w.WriteLine("\t\tpublic void Delete(int id){");
                        w.WriteLine("\t\t\t" + table.dbTable + " entity = _context." + table.dbTable + ".Find(id);");
                        w.WriteLine("\t\t\tif (entity != null){\n\t\t\t\t_context." + table.dbTable + ".Remove(entity);\n\t\t\t\t_context.SaveChanges();\n\t\t\t}");
                        w.WriteLine("\t\t}");
                        //get data for children
                        foreach (var child in table.children)
                        {
                            w.WriteLine("\t\tpublic List<" + child.dbTable + "> Get" + child.dbTable + "Children(int id){");
                            w.WriteLine("\t\t\treturn _context." + table.dbTable + ".Where(x=>x.Id==id).Include(x=>x." + child.dbTable + ")." +
                                        "SelectMany(x=>x." + child.dbTable + ").ToList();\n\t\t}");
                        }
                        w.WriteLine("\t}\n}");
                    }
                }
            }
            foreach (NNModel nnModel in model.nnRelations)
            {
                using (FileStream fs = new FileStream(path + "\\" + nnModel.nnTable + "AccessLayer.cs", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("using System;\nusing " + project + ".Server.Models;\n" + "using " + project + ".Shared.Models;\nusing System.Collections.Generic;\n");
                        w.WriteLine("using System.Linq;\nusing Microsoft.EntityFrameworkCore;");
                        w.WriteLine("namespace " + project + ".Server.DataAccess\n{");
                        w.WriteLine("\tpublic class " + nnModel.nnTable + "AccessLayer\n{\n");
                        w.WriteLine("\t\tContext _context = new Context();\n");
                        //get for first
                        w.WriteLine("\t\tpublic List<" + nnModel.nnProps.table2 + "> Get" + nnModel.nnProps.table2 + "(int id)\n\t\t{");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\treturn _context." + nnModel.nnTable + ".Where(x=>x." + nnModel.nnProps.attr1 + " == id)." +
                                    "Include(x=>x." + nnModel.nnProps.table2 + ").Select(x=>x." + nnModel.nnProps.table2 + ").Distinct().ToList();\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n");
                        //get for second
                        w.WriteLine("\t\tpublic List<" + nnModel.nnProps.table1 + "> Get" + nnModel.nnProps.table1 + "(int id)\n\t\t{");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\treturn _context." + nnModel.nnTable + ".Where(x=>x." + nnModel.nnProps.attr2 + " == id)." +
                                    "Include(x=>x." + nnModel.nnProps.table1 + ").Select(x=>x." + nnModel.nnProps.table1 + ").Distinct().ToList();\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n");
                        //get nn by ids
                        w.WriteLine("\t\tpublic " + nnModel.nnTable + " GetById(int id1,int id2){");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\treturn _context." + nnModel.nnTable + "" +
                                    ".Where(x=>x." + nnModel.nnProps.attr1 + "==id1 && id2==x." + nnModel.nnProps.attr2 + ").SingleOrDefault();\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch (Exception e){\n\t\t\t\treturn null;\n\t\t\t}\n\t\t}\n");
                        //delete nnTable
                        w.WriteLine("\t\tpublic void Delete(int id1, int id2){");
                        w.WriteLine("\t\t\t" + nnModel.nnTable + " entity = _context." + nnModel.nnTable + "" +
                                    ".Where(x=>x." + nnModel.nnProps.attr1 + " == id1 && id2 == x." + nnModel.nnProps.attr2 + ").SingleOrDefault();");
                        w.WriteLine("\t\t\tif (entity != null){\n\t\t\t\t_context." + nnModel.nnTable + ".Remove(entity);\n\t\t\t\t_context.SaveChanges();\n\t\t\t}");
                        w.WriteLine("\t\t}");
                        //post nnTable
                        w.WriteLine("\t\tpublic void Add(" + nnModel.nnTable + " model){");
                        w.WriteLine("\t\t\ttry{");
                        w.WriteLine("\t\t\t\t" + nnModel.nnTable + " entity = _context." + nnModel.nnTable + "" +
                                    ".Where(x=>x." + nnModel.nnProps.attr1 + " == model." + nnModel.nnProps.attr1 + " && model." + nnModel.nnProps.attr2 + " == x." + nnModel.nnProps.attr2 + ").SingleOrDefault();");
                        w.WriteLine("\t\t\t\tif (entity == null){");
                        w.WriteLine("\t\t\t\t\t_context." + nnModel.nnTable + ".Add(model);");
                        w.WriteLine("\t\t\t\t\t_context.SaveChanges();\n\t\t\t\t}\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\n\t\t\t}\n\t\t}\n\n");
                        //update nnTable
                        w.WriteLine("\t\tpublic void Update(" + nnModel.nnTable + " " + nnModel.nnTable.ToLower() + "){");
                        w.WriteLine("\t\t\ttry{\n\t\t\t\tif(" + nnModel.nnTable.ToLower() + " != null){\n\t\t\t\t\t" +
                                    "_context." + nnModel.nnTable + ".Update(" + nnModel.nnTable.ToLower() + ");");
                        w.WriteLine("\t\t\t\t\t_context.SaveChanges();\n\t\t\t\t}\n\t\t\t}");
                        w.WriteLine("\t\t\tcatch(Exception e){\n\n\t\t\t}\n\t\t}\n\n");

                        w.WriteLine("\t}\n}"); //closing for namespace and class
                    }
                }
            }
        }
        public static void generateAuthorization(TableModelCollection model, string path, string projectName)
        {
            Dictionary <string, List <int> > readPermissions  = new Dictionary <string, List <int> >();
            Dictionary <string, List <int> > writePermissions = new Dictionary <string, List <int> >();

            foreach (TableModel table in model.tableModels)
            {
                readPermissions.Add(table.dbTable.ToLower(), new List <int>(table.readPermissions));
                writePermissions.Add(table.dbTable.ToLower(), new List <int>(table.writePermissions));
            }
            //authorization class
            using (FileStream fs = new FileStream(path + "\\AuthorizationStore.cs", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine("using System.Collections.Generic;\n");
                    w.WriteLine("namespace " + projectName + ".Server\n{");
                    w.WriteLine("\tpublic static class AuthorizationStore\n\t{");
                    w.WriteLine("\t\tprivate static int roleId=0;");
                    w.WriteLine("\t\tprivate static Dictionary<string, List<int>> readPermissions = new Dictionary<string, List<int>>()\n\t\t{");
                    string s = "";
                    foreach (KeyValuePair <string, List <int> > pair in readPermissions)
                    {
                        s += "\t\t\t{ \"" + pair.Key + "\", new List<int>(){ " + string.Join(',', pair.Value) + " } },\n";
                    }
                    s = s.Substring(0, s.Length - 2);
                    w.WriteLine(s);
                    s = "";
                    w.WriteLine("\t\t};\n\t\tprivate static Dictionary<string, List<int>> writePermissions = new Dictionary<string, List<int>>()\n\t\t{");
                    foreach (KeyValuePair <string, List <int> > pair in writePermissions)
                    {
                        s += "\t\t\t{ \"" + pair.Key + "\", new List<int>(){ " + string.Join(',', pair.Value) + " } },\n";
                    }
                    s = s.Substring(0, s.Length - 2);
                    w.WriteLine(s);
                    w.WriteLine("\t\t};");
                    w.WriteLine("\t\tpublic static int getRoleId()\n\t\t{\n\t\t\treturn roleId;\n\t\t}");
                    w.WriteLine("\t\tpublic static void setRoleId(int id)\n\t\t{\n\t\t\troleId=id;\n\t\t}");
                    w.WriteLine("\t\tpublic static bool checkReadPermission(string controller)\n\t\t{");
                    w.WriteLine("\t\t\tif (readPermissions[controller].Contains(roleId))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}");
                    w.WriteLine("\t\t\treturn false;\n\t\t}");
                    w.WriteLine("\t\tpublic static bool checkWritePermissions(string controller)\n\t\t{");
                    w.WriteLine("\t\t\tif (writePermissions[controller].Contains(roleId))\n\t\t\t{\n\t\t\t\treturn true;\n\t\t\t}");
                    w.WriteLine("\t\t\treturn false;\n\t\t}");
                    w.WriteLine("\t}\n}");
                }
            }
            //accountcontroller
            using (FileStream fs = new FileStream(path + "\\AccountController.cs", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine("using System.Linq;\nusing " + projectName + ".Server.Models;");
                    w.WriteLine("using " + projectName + ".Shared.Models;\nusing Microsoft.AspNetCore.Mvc;\n");
                    w.WriteLine("namespace " + projectName + ".Server.Controllers\n{");
                    w.WriteLine("\tpublic class AccountController: Controller\n\t{");
                    w.WriteLine("\t\tContext _context = new Context();\n");
                    //login
                    w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/login\")]");
                    w.WriteLine("\t\tpublic IActionResult LogIn([FromBody] UserModel model)\n\t\t{");
                    w.WriteLine("\t\t\tvar user = _context.Person.Where(x=>model.password==x.Password && model.userName==x.UserName).SingleOrDefault();");
                    w.WriteLine("\t\t\tif(user != null)\n\t\t{");
                    w.WriteLine("\t\t\t\tAuthorizationStore.setRoleId(user.RoleId);\n\t\t\t\treturn new ObjectResult(user.RoleId);\n\t\t\t}");
                    w.WriteLine("\t\t\treturn new ObjectResult(0);\n\t\t}");
                    w.WriteLine("");
                    //register
                    w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/register\")]");
                    w.WriteLine("\t\tpublic IActionResult Register([FromBody] Person model)\n\t\t{");
                    w.WriteLine("\t\t\tif(ModelState.IsValid)\n\t\t\t{");
                    w.WriteLine("\t\t\t\t_context.Person.Add(model);\n\t\t\t\t_context.SaveChanges();");
                    w.WriteLine("\t\t\t\tAuthorizationStore.setRoleId(model.RoleId);");
                    w.WriteLine("\t\t\t\treturn new ObjectResult(model.RoleId);\n\t\t\t}");
                    w.WriteLine("\t\t\telse\n\t\t\t{\n\t\t\t\treturn new ObjectResult(0);\n\t\t\t}");
                    w.WriteLine("\n\t\t}");
                    w.WriteLine("\t}\n}");
                }
            }

            //log in screen
            using (FileStream fs = new FileStream(path + "\\Login.cshtml", FileMode.Create))
            {
                using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                {
                    w.WriteLine("@page \"/\"\n@page \"/login\"\n");
                    w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper\n");
                    w.WriteLine("<h5> @message </h5>\n\n<div> ");
                    w.WriteLine("\t<label> UserName </label>\n\t<input type=\"text\" bind=\"@UserName\" asp-for=\"username\" />");
                    w.WriteLine("\t<label> Password </label>\n\t<input type=\"password\" bind=\"@Password\" asp-for=\"password\" />");
                    w.WriteLine("\t<button onclick=\"@LogIn\">Log in </button>\n</div>");
                    w.WriteLine("\n@functions{");
                    w.WriteLine("\tpublic string UserName {get; set;}\n\tpublic string Password{get; set;}\n\tpublic string message = \"\";");
                    w.WriteLine("\tpublic async Task LogIn()\n\t{");
                    w.WriteLine("\t\tvar data = new " + projectName + ".Shared.Models.UserModel {password = Password, userName= UserName};");
                    w.WriteLine("\t\tvar response = await Http.PostJsonAsync<int>(\"/api/login\", data);");
                    w.WriteLine("\t\tif (response.ToString()==\"0\")\n\t\t{");
                    w.WriteLine("\t\t\tmessage=\"User wasn't found, please check username and password\";\n\t\t}");
                    w.WriteLine("\t\telse\n\t\t{");
                    w.WriteLine("\t\t\tAuthorizationStore.setRoleId(response);\n\t\t\turiHelper.NavigateTo(\"/start\");\n\t\t}");
                    w.WriteLine("\t}\n}");
                }
            }
        }
Пример #4
0
        public static void GenerateController(string path, TableModelCollection model, string projectName)
        {
            foreach (TableModel table in model.tableModels)
            {
                using (FileStream fs = new FileStream(path + "\\" + table.dbTable + "sController.cs", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("using " + projectName + ".Server.DataAccess;\nusing " + projectName + ".Shared.Models;\nusing Microsoft.AspNetCore.Mvc;\n" +
                                    "using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;");
                        w.WriteLine();
                        w.WriteLine("namespace " + projectName + ".Server.Controllers \n{");
                        w.WriteLine("\tpublic class " + table.dbTable + "Controller : Controller \n\t{");
                        w.WriteLine("\t\tRepo _repository=new Repo();\n\t\tRepo2 _repo2 = new Repo2();");
                        //get all
                        w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "s\")]");
                        w.WriteLine("\t\tpublic IEnumerable<" + table.dbTable + "> Get(){");
                        if (model.authorization)
                        {
                            w.WriteLine();
                        }
                        w.WriteLine("\t\t\treturn _repository.GetAll();\n\t\t}\n");
                        //get by id
                        w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "/{id}\")]");
                        w.WriteLine("\t\tpublic " + table.dbTable + " GetById(int id){");
                        w.WriteLine("\t\t\tif(id==0){\n\t\t\t\treturn new " + table.dbTable + "();\n\t\t\t}\n\t\t\telse{");
                        w.WriteLine("\t\t\t\treturn _repository.GetById(id);\n\t\t\t}\n\t\t}\n");
                        //post
                        w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "/create\")]");
                        w.WriteLine("\t\tpublic void Post([FromBody] " + table.dbTable + " model){");
                        w.WriteLine("\t\t\tif (ModelState.IsValid){\n\t\t\t\t_repository.Add(model);");
                        w.WriteLine("\t\t\t}\n\t\t}");
                        //update
                        w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "/edit\")]");
                        w.WriteLine("\t\tpublic void Update([FromBody] " + table.dbTable + " model){");
                        w.WriteLine("\t\t\tif (ModelState.IsValid) {\n\t\t\t\t_repository.Update(model);");
                        w.WriteLine("\t\t\t}\n\t\t}");
                        //delete
                        w.WriteLine("\t\t[HttpDelete]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "/delete/{id}\")]");
                        w.WriteLine("\t\tpublic void Delete(int id){");
                        w.WriteLine("\t\t\t_repository.Delete(id);");
                        w.WriteLine("\t\t}");

                        if (table.atributes.Where(x => x.foreignKey == true).Count() > 0)
                        {
                            Dictionary <string, List <string> > tableValuePairs = new Dictionary <string, List <string> >();
                            foreach (var attr in table.atributes.Where(x => x.foreignKey == true))
                            {
                                if (tableValuePairs.ContainsKey(attr.fkTable))
                                {
                                    tableValuePairs[attr.fkTable].Add(attr.fkValue);
                                }
                                else
                                {
                                    tableValuePairs[attr.fkTable] = new List <string>();
                                    tableValuePairs[attr.fkTable].Add(attr.fkValue);
                                }
                            }

                            foreach (var fkTable in tableValuePairs.Keys)
                            {
                                foreach (var value in tableValuePairs[fkTable])
                                {
                                    w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "s/" + fkTable.ToLower() + value.ToLower() + "\")]");
                                    w.WriteLine("\t\tpublic List<SelectListItem> Get" + fkTable + value + "SelectList(){");
                                    w.WriteLine("\t\t\tvar all=_repo2.GetAll();\n\t\t\tList<SelectListItem> options = new List<SelectListItem>();");
                                    w.WriteLine("\t\t\tforeach(var option in all){\n\t\t\t\toptions.Add(new SelectListItem(option.Id, option." + value + "));\n\t\t\t}");
                                    w.WriteLine("\t\t\treturn options;\n\t\t}");
                                }
                            }
                        }
                        foreach (var child in table.children)
                        {
                            w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + table.dbTable.ToLower() + "/" + child.dbTable.ToLower() + "/{id}\")]");
                            w.WriteLine("\t\tpublic List<" + child.dbTable + "> Get" + child.dbTable + "(int id){");
                            w.WriteLine("\t\t\treturn _repository.Get" + child.dbTable + "Children(id);\n\t\t}\n");
                        }
                        w.WriteLine("\t}\n}"); //closing for namespace and class
                    }
                }
            }
            foreach (NNModel nnModel in model.nnRelations)
            {
                using (FileStream fs = new FileStream(path + "\\" + nnModel.nnTable + "sController.cs", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("using " + projectName + ".Server.DataAccess;\nusing " + projectName + ".Shared.Models;\nusing Microsoft.AspNetCore.Mvc;\n" +
                                    "using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Threading.Tasks;");
                        w.WriteLine();
                        w.WriteLine("namespace " + projectName + ".Server.Controllers \n{");
                        w.WriteLine("\tpublic class " + nnModel.nnTable + "Controller : Controller \n\t{");
                        w.WriteLine("\t\tRepo _repository=new Repo();\n\t\tRepo2 _repo2 = new Repo2();");
                        // get for first table
                        w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/" + nnModel.nnProps.table2.ToLower() + "/{id}\")]");
                        w.WriteLine("\t\tpublic List<" + nnModel.nnProps.table2 + "> GetFor" + nnModel.nnProps.table1 + " (int id){");
                        w.WriteLine("\t\t\treturn _repository.Get" + nnModel.nnProps.table2 + "(id);");
                        w.WriteLine("\t\t}\n");
                        // get for second table
                        w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/" + nnModel.nnProps.table1.ToLower() + "/{id}\")]");
                        w.WriteLine("\t\tpublic List<" + nnModel.nnProps.table1 + "> GetFor" + nnModel.nnProps.table2 + " (int id){");
                        w.WriteLine("\t\t\treturn _repository.Get" + nnModel.nnProps.table1 + "(id);");
                        w.WriteLine("\t\t}\n");
                        //get by ids
                        w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/{id1}/{id2}\")]");
                        w.WriteLine("\t\tpublic " + nnModel.nnTable + " GetById(int id1, int id2){");
                        w.WriteLine("\t\t\tif(id1==0 || id2==0){\n\t\t\t\tvar ret=new " + nnModel.nnTable + "();" +
                                    "\n\t\t\t\tret." + nnModel.nnProps.attr1 + "=id1;\n\t\t\t\tret." + nnModel.nnProps.attr2 + "=id2;\n\t\t\t\treturn ret;\n\t\t\t}\n\t\t\telse{");
                        w.WriteLine("\t\t\t\treturn _repository.GetById(id1,id2);\n\t\t\t}\n\t\t}\n");
                        // post nn
                        w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/create\")]");
                        w.WriteLine("\t\tpublic void Post([FromBody] " + nnModel.nnTable + " model){");
                        w.WriteLine("\t\t\tif (ModelState.IsValid){\n\t\t\t\t_repository.Add(model);");
                        w.WriteLine("\t\t\t}\n\t\t}");
                        //update nn
                        w.WriteLine("\t\t[HttpPost]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/edit\")]");
                        w.WriteLine("\t\tpublic void Update([FromBody] " + nnModel.nnTable + " model){");
                        w.WriteLine("\t\t\tif (ModelState.IsValid) {\n\t\t\t\t_repository.Update(model);");
                        w.WriteLine("\t\t\t}\n\t\t}");
                        //delete nn
                        w.WriteLine("\t\t[HttpDelete]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "/delete/{id1}/{id2}\")]");
                        w.WriteLine("\t\tpublic void Delete(int id1, int id2){");
                        w.WriteLine("\t\t\t_repository.Delete(id1, id2);");
                        w.WriteLine("\t\t}");

                        if (nnModel.atributes.Where(x => x.foreignKey == true).Count() > 0)
                        {
                            Dictionary <string, List <string> > tableValuePairs = new Dictionary <string, List <string> >();
                            foreach (var attr in nnModel.atributes.Where(x => x.foreignKey == true))
                            {
                                if (tableValuePairs.ContainsKey(attr.fkTable))
                                {
                                    tableValuePairs[attr.fkTable].Add(attr.fkValue);
                                }
                                else
                                {
                                    tableValuePairs[attr.fkTable] = new List <string>();
                                    tableValuePairs[attr.fkTable].Add(attr.fkValue);
                                }
                            }

                            foreach (var fkTable in tableValuePairs.Keys)
                            {
                                int i = 2;
                                foreach (var value in tableValuePairs[fkTable])
                                {
                                    w.WriteLine("\t\t[HttpGet]\n\t\t[Route(\"api/" + nnModel.nnTable.ToLower() + "s/" + fkTable.ToLower() + value.ToLower() + "\")]");
                                    w.WriteLine("\t\tpublic List<SelectListItem> Get" + fkTable + value + "SelectList(){");
                                    w.WriteLine("\t\t\tvar all=_repo" + i.ToString() + ".GetAll();\n\t\t\tList<SelectListItem> options = new List<SelectListItem>();");
                                    w.WriteLine("\t\t\tforeach(var option in all){\n\t\t\t\toptions.Add(new SelectListItem(option.Id, option." + value + "));\n\t\t\t}");
                                    w.WriteLine("\t\t\treturn options;\n\t\t}");
                                    i++;
                                }
                            }
                        }

                        w.WriteLine("\t}\n}"); //closing for namespace and class
                    }
                }
            }
        }
Пример #5
0
 public static void GenerateTableView(string viewsPath, TableModelCollection model, string projectName)
 {
     foreach (TableModel table in model.tableModels)
     {
         using (FileStream fs = new FileStream(viewsPath + "\\" + table.dbTable + "Table.cshtml", FileMode.Create))
         {
             using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
             {
                 w.WriteLine("@page \"/" + table.dbTable.ToLower() + "s\"");
                 w.WriteLine("@using " + projectName + ".Shared.Models");
                 w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                 w.WriteLine();
                 w.WriteLine("@if (models==null) { \n\t <p><em>Loading...</em></p> \n}\n else {");
                 if (model.authorization)
                 {
                     w.WriteLine("\t@if(AuthorizationStore.checkWritePermissions(\"" + table.dbTable.ToLower() + "\")){");
                     w.WriteLine("\t\t<button onclick=\"@Create\">Create</button>\n\t}");
                 }
                 else
                 {
                     w.WriteLine("\t<button onclick=\"@Create\">Create</button>");
                 }
                 w.WriteLine("<table class=\"table\">");
                 w.WriteLine("\t<thead>");
                 w.WriteLine("\t\t<tr>");
                 foreach (var attr in table.atributes)
                 {
                     w.WriteLine("\t\t\t<td" + (attr.hidden ? " hidden" : "") + "> " + attr.name + "</td>");
                 }
                 w.WriteLine("\t\t</tr>");
                 w.WriteLine("\t</thead>");
                 w.WriteLine("\t<tbody>");
                 w.WriteLine("\t@foreach(var entity in @models){");
                 w.WriteLine("\t\t<tr>");
                 foreach (var attr in table.atributes)
                 {
                     if (table.atributes.IndexOf(attr) != 1)
                     {
                         w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "> @entity." + attr.name + "</td>");
                     }
                     else
                     {
                         w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "><a href=\"/" + table.dbTable.ToLower() +
                                     "s/@entity.Id\">" + " @entity." + attr.name + "</a></td>");
                     }
                 }
                 if (model.authorization)
                 {
                     w.WriteLine("\t\t\t@if(AuthorizationStore.checkWritePermissions(\"" + table.dbTable.ToLower() + "\"))\n\t\t\t{");
                     w.WriteLine("\t\t\t\t<td><button onclick=\"@(e=>Edit(entity.Id))\">Edit</button> |<button onclick=\"@(e=>Delete(entity.Id))\">Delete</button></td>");
                     w.WriteLine("\t\t\t}");
                 }
                 else
                 {
                     w.WriteLine("\t\t\t<td><button onclick=\"@(e=>Edit(entity.Id))\">Edit</button> |<button onclick=\"@(e=>Delete(entity.Id))\">Delete</button></td>");
                 }
                 w.WriteLine("\t\t</tr>");
                 w.WriteLine("\t}");
                 w.WriteLine("\t</tbody>");
                 w.WriteLine("</table>");
                 w.WriteLine("}");
                 w.WriteLine("@functions{\n\n");
                 w.WriteLine("\tList<" + projectName + ".Shared.Models." + table.dbTable + "> models;");
                 w.WriteLine("\tprotected override async Task OnInitAsync()\n\t{ ");
                 if (model.authorization)
                 {
                     w.WriteLine("\t\tif (!AuthorizationStore.checkReadPermission(\"" + table.dbTable.ToLower() + "\")) uriHelper.NavigateTo(\" / \");");
                 }
                 w.WriteLine("\t\tmodels=await Http.GetJsonAsync<List<" + projectName + ".Shared.Models." + table.dbTable + ">>(\"/api/" + table.dbTable.ToLower() + "s\");");
                 w.WriteLine("\t}");
                 w.WriteLine("\tvoid Create(){\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "/0\");\n\t}");
                 w.WriteLine("\tvoid Edit(int id){\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "/\"+id);\n\t}");
                 w.WriteLine("\tvoid Delete(int id){\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "/delete/\"+id);\n\t}");
                 w.WriteLine();
                 w.WriteLine("}");
             }
         }
     }
 }
Пример #6
0
        public static void GenerateCreateUpdate(string viewsPath, TableModelCollection model, string projectName)
        {
            foreach (TableModel table in model.tableModels)
            {
                using (FileStream fs = new FileStream(viewsPath + "\\" + table.dbTable + "Create.cshtml", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("@page \"/" + table.dbTable.ToLower() + "/{id}\"");
                        w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                        w.WriteLine("<button onclick=\"@Back\">Back</button>");
                        w.WriteLine("<h1>Edit " + table.dbTable + "</h1>\n");
                        if (table.atributes.Where(x => x.foreignKey == true).Count() > 0)
                        {
                            w.WriteLine("<h6>@message</h6>");
                        }
                        w.WriteLine("<form onsubmit=\"@Post\">\n<table>\n\t<tbody>");
                        foreach (var attr in table.atributes)
                        {
                            if (!attr.foreignKey)
                            {
                                w.WriteLine("\t\t<tr>\n\t\t\t<td>");
                                if (!attr.hidden)
                                {
                                    w.WriteLine("\t\t\t\t<label>" + attr.name + "</label>");
                                }
                                w.WriteLine("\t\t\t\t<input type=\"" + attr.type + "\" bind=\"@model." + attr.name + "\"" +
                                            (attr.type == "date" ? " format-value=\"yyyy-MM-dd\"" : "") + " asp-for=\"" + attr.name + "\" " +
                                            (attr.hidden ? "hidden" : "") + "/>");
                                w.WriteLine("\t\t\t</td>\n\t\t</tr>");
                            }
                            else
                            {
                                w.WriteLine("\t\t<tr>\n\t\t\t<td>");
                                w.WriteLine("\t\t\t\t<label>" + attr.name + "</label>");
                                w.WriteLine("\t\t\t\t<select bind=\"@model." + attr.name + "\">\n\t\t\t\t\t<option value=\"\">Choose value</option>");
                                w.WriteLine("\t\t\t\t\t@foreach(var option in options" + attr.name.ToLower() + "){\n\t\t\t\t\t\t<option value=\"@option.Key\">@option.Value</option>");
                                w.WriteLine("\t\t\t\t\t}\n\t\t\t\t</select>\n\t\t\t</td>\n\t\t</tr>");
                            }
                        }
                        w.WriteLine("\t</tbody>\n</table>");
                        w.WriteLine("\t<button type=\"submit\" class=\"btn btn - success\">Save</button>\n</form>\n\n@functions{");
                        w.WriteLine("\t[Parameter]\n\tprivate string Id {get; set;}\n\n\t" + projectName + ".Shared.Models." + table.dbTable + " " +
                                    "model = new " + projectName + ".Shared.Models." + table.dbTable + "();");
                        foreach (var attr in table.atributes.Where(x => x.foreignKey == true))
                        {
                            w.WriteLine("\tList<" + projectName + ".Shared.Models.SelectListItem> options" + attr.name.ToLower() + " = new List<" + projectName + ".Shared.Models.SelectListItem>();");
                        }
                        w.WriteLine("\tstring message = \"\";");
                        w.WriteLine("\tprotected override async Task OnInitAsync(){");
                        if (model.authorization)
                        {
                            w.WriteLine("\t\tif (!AuthorizationStore.checkReadPermission(\"" + table.dbTable.ToLower() + "\")) uriHelper.NavigateTo(\" / \");");
                        }
                        w.WriteLine("\t\tmodel=await Http.GetJsonAsync<" + projectName + ".Shared.Models." + table.dbTable + ">(\"/api/" + table.dbTable.ToLower() + "/\"+Id);");
                        foreach (var attr in table.atributes.Where(x => x.foreignKey == true))
                        {
                            w.WriteLine("\t\toptions" + attr.name.ToLower() + " = await Http.GetJsonAsync<List<" + projectName + ".Shared.Models.SelectListItem>>(\"/api/" + table.dbTable.ToLower() + "s/" + attr.fkTable.ToLower() + attr.fkValue.ToLower() + "\");");
                        }
                        w.WriteLine("\t}");
                        w.WriteLine("\tvoid Back()\n\t{\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t}");
                        if (table.atributes.Any(x => x.foreignKey == true))
                        {
                            string condition = "";
                            foreach (var attr in table.atributes.Where(x => x.foreignKey == true))
                            {
                                condition += "model." + attr.name + " == 0 ||";
                            }
                            condition = condition.Substring(0, condition.Length - 2);
                            w.WriteLine("\tpublic async Task Post(){\n\t\ttry{\n\t\t\tif(" + condition + "){\n\t\t\t\tmessage=\"Please, fill all fields\";\n\t\t\t}");
                            w.WriteLine("\t\t\telse if(model.Id==0){\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + table.dbTable.ToLower() + "/create\", model);" +
                                        "\n\t\t\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t\t\t}");
                        }
                        else
                        {
                            w.WriteLine("\tpublic async Task Post(){\n\t\ttry{\n\t\t\tif(model.Id==0){\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + table.dbTable.ToLower() + "/create\",model);" +
                                        "\n\t\t\t\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t\t\t}");
                        }
                        w.WriteLine("\t\t\telse{\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + table.dbTable.ToLower() + "/edit\",model);" +
                                    "\n\t\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t\t\t}");
                        w.WriteLine("\t\t}\n\t\tcatch(Exception e){\n\t\t\tConsole.WriteLine(e.Message);\n\t\t\tthrow;\n\t\t}\n\t}\n}");
                    }
                }
            }

            foreach (NNModel nNModel in model.nnRelations)
            {
                using (FileStream fs = new FileStream(viewsPath + "\\" + nNModel.nnTable + "Create.cshtml", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("@page \"/" + nNModel.nnTable.ToLower() + "/{table}/{id1}/{id2}\"");
                        w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                        w.WriteLine("<h1>Edit " + nNModel.nnTable + "</h1>\n");
                        w.WriteLine("<h6>@message</h6>");
                        w.WriteLine("<form onsubmit=\"@Post\">\n<table>\n\t<tbody>");
                        foreach (var attr in nNModel.atributes)
                        {
                            if (!attr.foreignKey)
                            {
                                w.WriteLine("\t\t<tr>\n\t\t\t<td>");
                                if (!attr.hidden)
                                {
                                    w.WriteLine("\t\t\t\t<label>" + attr.name + "</label>");
                                }
                                w.WriteLine("\t\t\t\t<input type=\"" + attr.type + "\" bind=\"@model." + attr.name + "\"" +
                                            (attr.type == "date" ? " format-value=\"yyyy-MM-dd\"" : "") + " asp-for=\"" + attr.name + "\" " +
                                            (attr.hidden ? "hidden" : "") + "/>");
                                w.WriteLine("\t\t\t</td>\n\t\t</tr>");
                            }
                            else
                            {
                                w.WriteLine("\t\t<tr>\n\t\t\t<td>");
                                w.WriteLine("\t\t\t\t<label>" + attr.name + "</label>");
                                w.WriteLine("\t\t\t\t<select bind=\"@model." + attr.name + "\">\n\t\t\t\t\t<option value=\"\">Choose value</option>");
                                w.WriteLine("\t\t\t\t\t@foreach(var option in options" + attr.name.ToLower() + "){\n\t\t\t\t\t\t<option value=\"@option.Key\">@option.Value</option>");
                                w.WriteLine("\t\t\t\t\t}\n\t\t\t\t</select>\n\t\t\t</td>\n\t\t</tr>");
                            }
                        }
                        w.WriteLine("\t</tbody>\n</table>");
                        w.WriteLine("\t<button type=\"submit\" class=\"btn btn - success\">Save</button>\n</form>\n\n@functions{");
                        w.WriteLine("\t[Parameter]\n\tprivate string Id1 {get; set;} \n\t[Parameter]\n\tprivate string Id2 {get; set;}" +
                                    "\n\t[Parameter]\n\tprivate string Table{get; set;}" +
                                    "\n\n\t" + projectName + ".Shared.Models." + nNModel.nnTable + " " +
                                    "model = new " + projectName + ".Shared.Models." + nNModel.nnTable + "();");
                        foreach (var attr in nNModel.atributes.Where(x => x.foreignKey == true))
                        {
                            w.WriteLine("\tList<" + projectName + ".Shared.Models.SelectListItem> options" + attr.name.ToLower() + " = new List<" + projectName + ".Shared.Models.SelectListItem>();");
                        }
                        w.WriteLine("\tstring message = \"\";");
                        w.WriteLine("\tprotected override async Task OnInitAsync(){\n\t\tmodel=await Http.GetJsonAsync<" + projectName + ".Shared.Models." + nNModel.nnTable + ">(\"/api/" + nNModel.nnTable.ToLower() + "/\"+Id1+\"/\"+Id2);");
                        foreach (var attr in nNModel.atributes.Where(x => x.foreignKey == true))
                        {
                            w.WriteLine("\t\toptions" + attr.name.ToLower() + " = await Http.GetJsonAsync<List<" + projectName + ".Shared.Models.SelectListItem>>(\"/api/" + nNModel.nnTable.ToLower() + "s/" + attr.fkTable.ToLower() + attr.fkValue.ToLower() + "\");");
                        }
                        w.WriteLine("\t}");
                        if (nNModel.atributes.Any(x => x.foreignKey == true))
                        {
                            string condition = "";
                            foreach (var attr in nNModel.atributes.Where(x => x.foreignKey == true))
                            {
                                condition += "model." + attr.name + " == 0 ||";
                            }
                            condition = condition.Substring(0, condition.Length - 2);
                            w.WriteLine("\tpublic async Task Post(){\n\t\ttry{\n\t\t\tif(" + condition + "){\n\t\t\t\tmessage=\"Please, fill all fields\";\n\t\t\t}");
                            w.WriteLine("\t\t\telse if(model.Id==0){\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + nNModel.nnTable.ToLower() + "/create\", model);" +
                                        "\n\t\t\t\turiHelper.NavigateTo(\"/\"+Table+\"s\");\n\t\t\t}");
                        }
                        else
                        {
                            w.WriteLine("\tpublic async Task Post(){\n\t\ttry{\n\t\t\tif(model.Id==0){\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + nNModel.nnTable.ToLower() + "/create\",model);" +
                                        "\n\t\t\t\t\turiHelper.NavigateTo(\"/\"+Table+\"s\");\n\t\t\t}");
                        }
                        w.WriteLine("\t\t\telse{\n\t\t\t\tawait Http.SendJsonAsync(HttpMethod.Post, \"/api/" + nNModel.nnTable.ToLower() + "/edit\",model);" +
                                    "\n\t\t\turiHelper.NavigateTo(\"/\"+Table+\"s\");\n\t\t\t}");
                        w.WriteLine("\t\t}\n\t\tcatch(Exception e){\n\t\t\tConsole.WriteLine(e.Message);\n\t\t\tthrow;\n\t\t}\n\t}\n}");
                    }
                }
            }
        }
Пример #7
0
        public static void GenerateReadView(string viewsPath, TableModelCollection model, string projectName)
        {
            foreach (TableModel table in model.tableModels)
            {
                using (FileStream fs = new FileStream(viewsPath + "\\" + table.dbTable + ".cshtml", FileMode.Create))
                {
                    using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8))
                    {
                        w.WriteLine("@page \"/" + table.dbTable.ToLower() + "s/{id}\"");
                        w.WriteLine("@inject HttpClient Http\n@inject Microsoft.AspNetCore.Blazor.Services.IUriHelper uriHelper");
                        w.WriteLine("@*\n\tput routes for page on top with @page /{wishedRoute}\n*@");
                        w.WriteLine();
                        w.WriteLine("<button onclick=\"@Back\">Back</button>");
                        foreach (var attr in table.atributes)
                        {
                            w.WriteLine("<p " + (attr.hidden ? "hidden" : "") + "><span> " + attr.name + "</span> @model." + attr.name + "</p>");
                        }
                        foreach (var child in table.children)
                        {
                            w.WriteLine("\n<h3>" + child.dbTable + "s</h3>\n");
                            w.WriteLine("<table>\n\t<thead>\n\t\t<tr>");
                            foreach (var attr in child.atributes)
                            {
                                w.WriteLine("\t\t\t<td" + (attr.hidden ? " hidden" : "") + "> " + attr.name + "</td>");
                            }
                            w.WriteLine("\t\t</tr>\n\t</thead>");
                            w.WriteLine("\t<tbody>");
                            w.WriteLine("\t@foreach(var entity in @model." + child.dbTable + "){");
                            w.WriteLine("\t\t<tr>");
                            foreach (var attr in child.atributes)
                            {
                                if (table.atributes.IndexOf(attr) != 1)
                                {
                                    w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "> @entity." + attr.name + "</td>");
                                }
                                else
                                {
                                    w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "><a href=\"/" + child.dbTable.ToLower() +
                                                "s/@entity.Id\">" + " @entity." + attr.name + "</a></td>");
                                }
                            }
                            w.WriteLine("\t\t\t<td><button onclick=\"@(e=>Edit(entity.Id, \"" + child.dbTable + "\"))\">Edit</button> |<button onclick=\"@(e=>Delete(entity.Id, \"" + child.dbTable + "\"))\">Delete</button></td>");
                            w.WriteLine("\t\t</tr>");
                            w.WriteLine("\t}");
                            w.WriteLine("\t</tbody>\n</table>");
                        }
                        Dictionary <string, string> nnDict = new Dictionary <string, string>();

                        foreach (var nnRelation in table.nNRelations)
                        {
                            nnDict.Add(nnRelation.nnTable, model.nnRelations.Where(x => x.nnTable == nnRelation.nnTable).SingleOrDefault().nnProps.table1);

                            w.WriteLine("\n<h5>" + nnRelation.nnTable + "s</h5>\n");
                            if (nnDict[nnRelation.nnTable] == table.dbTable)
                            {
                                w.WriteLine("<button onclick=@( () => Createnn(\"" + nnRelation.nnTable.ToLower() + "\"))>Create</button>");
                            }
                            else
                            {
                                w.WriteLine("<button onclick=@( () => nnCreate(\"" + nnRelation.nnTable.ToLower() + "\"))>Create</button>");
                            }
                            w.WriteLine("<table>\n\t<thead>\n\t\t<tr>");
                            foreach (var attr in nnRelation.atributes)
                            {
                                w.WriteLine("\t\t\t<td" + (attr.hidden ? " hidden" : "") + "> " + attr.name + "</td>");
                            }
                            w.WriteLine("\t\t</tr>\n\t</thead>");
                            w.WriteLine("\t<tbody>");
                            w.WriteLine("\t@foreach(var entity in " + nnRelation.nnTable.ToLower() + "s){");
                            w.WriteLine("\t\t<tr>");
                            foreach (var attr in nnRelation.atributes)
                            {
                                if (table.atributes.IndexOf(attr) != 1)
                                {
                                    w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "> @entity." + attr.name + "</td>");
                                }
                                else
                                {
                                    w.WriteLine("\t\t\t<td " + (attr.hidden ? " hidden" : "") + "><a href=\"/" + nnRelation.nnTable.ToLower() +
                                                "s/@entity.Id\">" + " @entity." + attr.name + "</a></td>");
                                }
                            }
                            if (nnDict[nnRelation.nnTable] == table.dbTable)
                            {
                                w.WriteLine("\t\t\t<td><button onclick=\"@(e=>nnEdit(entity.Id, \"" + nnRelation.nnTable + "\"" +
                                            "))\">Edit</button> |<button onclick=\"@(e=>nnDelete(entity.Id, \"" + nnRelation.nnTable + "\"))\">Delete</button></td>");
                            }
                            else
                            {
                                w.WriteLine("\t\t\t<td><button onclick=\"@(e=>Editnn(entity.Id, \"" + nnRelation.nnTable + "\" " +
                                            "))\">Edit</button> |<button onclick=\"@(e=>Deletenn(entity.Id, \"" + nnRelation.nnTable + "\" ))\">Delete</button></td>");
                            }
                            w.WriteLine("\t\t</tr>");
                            w.WriteLine("\t}");
                            w.WriteLine("\t</tbody>\n</table>");
                        }
                        w.WriteLine();
                        w.WriteLine("@functions{\n\t[Parameter]\n\tprivate string Id {get; set;}\n\n\t" + projectName + ".Shared.Models." + table.dbTable + " " +
                                    "model = new " + projectName + ".Shared.Models." + table.dbTable + "();");
                        foreach (var nnRelation in table.nNRelations)
                        {
                            string thisTable       = table.dbTable;
                            string nnRelationTable = nnRelation.nnTable;
                            var    relationModel   = model.nnRelations.Where(x => x.nnTable == nnRelationTable).SingleOrDefault();
                            var    otherTable      = relationModel.nnProps.table1 == thisTable ? relationModel.nnProps.table2 : relationModel.nnProps.table1;
                            w.WriteLine("ICollection<" + projectName + ".Shared.Models." + otherTable + "> " + relationModel.nnTable.ToLower() + "s = new List<" + projectName + ".Shared.Models." + otherTable + ">();");
                        }

                        w.WriteLine("\tprotected override async Task OnInitAsync(){");
                        if (model.authorization)
                        {
                            w.WriteLine("\t\tif (!AuthorizationStore.checkReadPermission(\"" + table.dbTable.ToLower() + "\")) uriHelper.NavigateTo(\" / \");");
                        }
                        w.WriteLine("\t\tmodel=await Http.GetJsonAsync<" + projectName + ".Shared.Models." + table.dbTable + ">(\"/api/" + table.dbTable.ToLower() + "/\"+Id);");
                        foreach (var child in table.children)
                        {
                            w.WriteLine("\t\tmodel." + child.dbTable + " = await Http.GetJsonAsync<List<" + projectName + ".Shared.Models." + child.dbTable + ">>(\"/api/" + table.dbTable.ToLower() + "/" + child.dbTable.ToLower() + "/\"+Id);");
                        }

                        foreach (var nnRelation in table.nNRelations)
                        {
                            string thisTable       = table.dbTable;
                            string nnRelationTable = nnRelation.nnTable;
                            var    relationModel   = model.nnRelations.Where(x => x.nnTable == nnRelationTable).SingleOrDefault();
                            var    otherTable      = relationModel.nnProps.table1 == thisTable ? relationModel.nnProps.table2 : relationModel.nnProps.table1;
                            w.WriteLine("\t\t" + relationModel.nnTable.ToLower() + "s = await Http.GetJsonAsync<List<" + projectName + ".Shared.Models." + otherTable + ">>(\"/api/" + nnRelationTable.ToLower() + "/" + otherTable.ToLower() + "/\"+Id);");
                        }
                        w.WriteLine("\t}");
                        w.WriteLine("\tvoid Back()\n\t{\n\t\turiHelper.NavigateTo(\"/" + table.dbTable.ToLower() + "s\");\n\t}");
                        if (table.children.Count > 0)
                        {
                            w.WriteLine("\tvoid Edit(int id, string table){\n\t\turiHelper.NavigateTo(\"/\"+table.ToLower()+\"/\"+id);\n\t}");
                            w.WriteLine("\tvoid Delete(int id, string table){\n\t\turiHelper.NavigateTo(\"/\" + table.ToLower() + \"/delete/\"+id);\n\t}");
                        }
                        if (table.nNRelations.Count > 0)
                        {
                            w.WriteLine("\tvoid Editnn(int id, string table){\n\t\turiHelper.NavigateTo(\"/\"+table.ToLower()+\"/" + table.dbTable.ToLower() + "/\"+id+\"/\"+Id);\n\t}");
                            w.WriteLine("\tvoid Deletenn(int id, string table){\n\t\turiHelper.NavigateTo(\"/\" + table.ToLower() + \"/" + table.dbTable.ToLower() + "/delete/\"+id+\"/\"+Id);\n\t}");

                            w.WriteLine("\tvoid nnEdit(int id, string table){\n\t\turiHelper.NavigateTo(\"/\"+table.ToLower()+\"/" + table.dbTable.ToLower() + "/\"+Id+\"/\"+id);\n\t}");
                            w.WriteLine("\tvoid nnDelete(int id, string table){\n\t\turiHelper.NavigateTo(\"/\" + table.ToLower() + \"/" + table.dbTable.ToLower() + "/delete/\"+Id+\"/\"+id);\n\t}");

                            w.WriteLine("\tvoid Createnn(string nnTable){\n\t\turiHelper.NavigateTo(\"/\"+nnTable+\"/" + table.dbTable.ToLower() + "/\"+Id+\"/0\");\n\t}");
                            w.WriteLine("\tvoid nnCreate(string nnTable){\n\t\turiHelper.NavigateTo(\"/\"+nnTable+\"/" + table.dbTable.ToLower() + "/0/\"+Id);\n\t}");
                        }
                        w.WriteLine("}");
                    }
                }
            }
        }
Пример #8
0
        public static TableModelCollection ReadData(string path)
        {
            JObject jObject = new JObject();

            using (StreamReader s = File.OpenText(path))
            {
                using (JsonTextReader reader = new JsonTextReader(s))
                {
                    jObject = (JObject)JToken.ReadFrom(reader);
                }
            }
            var tableCollection = new TableModelCollection();

            foreach (var table in jObject)
            {
                if (table.Key == "validation")
                {
                    tableCollection.authorization = table.Value.ToObject <bool>();
                }
                else if (table.Key == "n-n")
                {
                    foreach (var obj in JObject.Parse(table.Value.ToString()))
                    {
                        var nnModel = new NNModel();
                        nnModel.nnTable = obj.Key;
                        foreach (var prop in JObject.Parse(obj.Value.ToString()))
                        {
                            if (prop.Key == "props")
                            {
                                nnModel.nnProps = prop.Value.ToObject <NNProps>();
                            }
                            else if (prop.Key == "attr")
                            {
                                foreach (var att in JObject.Parse(prop.Value.ToString()))
                                {
                                    nnModel.atributes.Add(att.Value.ToObject <AtributeModel>());
                                }
                            }
                        }
                        tableCollection.nnRelations.Add(nnModel);
                    }
                }
                else
                {
                    var tableModel = new TableModel();
                    tableModel.dbTable = table.Key;

                    foreach (var attr in JObject.Parse(table.Value.ToString()))
                    {
                        //so generator could generate authorization
                        if (attr.Key == "read")
                        {
                            tableModel.readPermissions = attr.Value.ToObject <int[]>();
                        }
                        else if (attr.Key == "write")
                        {
                            tableModel.writePermissions = attr.Value.ToObject <int[]>();
                        }
                        //get children
                        else if (attr.Key == "children")
                        {
                            foreach (var childTable in JObject.Parse(attr.Value.ToString()))
                            {
                                var child = new ChildModel();
                                child.dbTable = childTable.Key;
                                foreach (var childAttr in JObject.Parse(childTable.Value.ToString()))
                                {
                                    child.atributes.Add(childAttr.Value.ToObject <AtributeModel>());
                                }
                                tableModel.children.Add(child);
                            }
                        } //take n-n relations
                        else if (attr.Key == "n-n")
                        {
                            foreach (var nn in JObject.Parse(attr.Value.ToString()))
                            {
                                var nnModel = new NNRelationModel();
                                nnModel.nnTable = nn.Key;
                                foreach (var nnDataConn in JObject.Parse(nn.Value.ToString()))
                                {
                                    nnModel.atributes.Add(nnDataConn.Value.ToObject <AtributeModel>());
                                }
                                tableModel.nNRelations.Add(nnModel);
                            }
                        }  //take attributes
                        else if (attr.Key != "children")
                        {
                            tableModel.atributes.Add(attr.Value.ToObject <AtributeModel>());
                        }
                    }
                    tableCollection.tableModels.Add(tableModel);
                }
            }
            return(tableCollection);
        }