Exemplo n.º 1
0
        private string Create_Controller_Code_Update(string template)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// Update a(n) " + entity_name + " details");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <returns></returns>");
            sb.AppendLine("        [Route(\"api/" + entity_sub_namespace + "/Put\")]");
            sb.AppendLine("        [PermissionVerify(new CommonEnum.Operations[] { CommonEnum.Operations.Setup })]");
            sb.AppendLine("        [HttpPut]");
            sb.AppendLine("        public IHttpActionResult Put(" + NameHelper.Get_ViewModel_UpdateModel(entity_name) + " model)");
            sb.AppendLine("        {");
            sb.AppendLine("            " + NameHelper.Get_Identities_Name(entity_name) + " " + entity_id_properties.Name.ToString() + " = new " + NameHelper.Get_Identities_Name(entity_name) + "(model.id);");
            sb.AppendLine("            var oldValue = QueryService." + NameHelper.Get_MethodName_QueryService_GetDetails_Name(entity_name) + "(" + entity_id_properties.Name.ToString() + ");");
            sb.AppendLine("            var command = new UpdateCommand()");
            sb.AppendLine("            {");
            foreach (var p in entity_properties)
            {
                if (NameHelper.Get_Property_Name_For_Command(p.Name).Equals("Created_Time") ||
                    NameHelper.Get_Property_Name_For_Command(p.Name).Equals("Created_User_Id") ||
                    NameHelper.Get_Property_Name_For_Command(p.Name).Equals("Last_Updated_Time") ||
                    NameHelper.Get_Property_Name_For_Command(p.Name).Equals("Last_Updated_User_Id"))
                {
                    break;
                }

                if (NameHelper.Get_Property_Name_For_Command(p.Name).Equals("Organization_Id"))
                {
                    sb.AppendLine("                " + NameHelper.Get_Property_Name_For_Command(p.Name) + " = oldValue." + NameHelper.Get_Property_Name_For_ViewModel(p.Name) + ",");
                }
                else
                {
                    sb.AppendLine("                " + NameHelper.Get_Property_Name_For_Command(p.Name) + " = model." + NameHelper.Get_Property_Name_For_ViewModel(p.Name) + ",");
                }
            }
            sb.AppendLine("            };");
            //sb.AppendLine("");
            sb.AppendLine("            command.Created_Time = oldValue.created_time;");
            sb.AppendLine("            command.Created_User_Id = oldValue.created_user_id;");
            sb.AppendLine("            command.Last_Updated_Time = DateTime.Now;");
            sb.AppendLine("            command.Last_Updated_User_Id = WebData.LoginedUser.UserId;");
            //sb.AppendLine("");
            sb.AppendLine("            CommandBus.Send(command);");
            //sb.AppendLine("");
            sb.AppendLine("            var properties = string.Empty;");
            foreach (var p in entity_properties)
            {
                sb.AppendLine("            if(oldValue." + NameHelper.Get_Property_Name_For_ViewModel(p.Name) + " != model." + NameHelper.Get_Property_Name_For_ViewModel(p.Name) + ")");
                sb.AppendLine("            {");
                sb.AppendLine("                 properties += \"" + NameHelper.Get_Property_Name_For_ViewModel(p.Name) + ";\";");
                sb.AppendLine("            }");
            }

            //sb.AppendLine("");
            sb.AppendLine("            if (!properties.IsNullOrEmpty()) { properties.Substring(0, properties.Length - 1); }");
            //sb.AppendLine("");
            sb.AppendLine("            if (NeedRecordSystemLog(Operations.Update)) ");
            sb.AppendLine("            {");
            sb.AppendLine("                 RecordSystemLog(JsonConvert.SerializeObject(command), null, properties, Operations.Delete);");
            sb.AppendLine("            }");

            sb.AppendLine("            return CommandResult(command.ExecuteResult);");
            sb.Append("        }");

            template = template.Replace("{{Update-Actions}}", sb.ToString());
            return(template);
        }