public RobotTemplate GetById(int id)
 {
     using (SqlConnection conn = new SqlConnection(this.ConnString))
     {
         using (SqlCommand command = new SqlCommand())
         {
             StringBuilder sqlCommand = new StringBuilder();
             sqlCommand.Append("SELECT * FROM robottemplates WHERE id=@id;");
             command.CommandText = sqlCommand.ToString();
             command.Parameters.AddWithValue("@id", id);
             command.Connection = conn;
             conn.Open();
             using (SqlDataReader reader = command.ExecuteReader())
             {
                 while (reader.Read())
                 {
                     this.id          = Convert.ToInt32(reader["id"]);
                     this.name        = Convert.ToString(reader["name"]);
                     this.description = Convert.ToString(reader["description"]);
                     this.note        = Convert.ToString(reader["note"]);
                     return(RobotTemplate.CreateFromRecord(this.id, this.name, this.description, this.note));
                 }
             }
         }
     }
     return(null);
 }
        public static string GetLookupStatement(RobotTemplate template)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("DECLARE @templateID int");
            sb.AppendLine("SET @templateID = (SELECT TOP 1 id from robottemplates WHERE [name] = '" + template.recordName + "' ORDER BY id DESC)");
            return(sb.ToString());
        }
        public static RobotTemplate CreateFromRecord(int id, string name, string descGenXY, string note)
        {
            Dictionary <string, object> dict = GenxyConverter.Deserialize(descGenXY);
            RobotTemplate botTemp            = RobotTemplate.GetRobotTemplateFromXY(dict);

            botTemp.recordNote        = note;
            botTemp.recordID          = id;
            botTemp.recordName        = name;
            botTemp.recordDescription = descGenXY;
            return(botTemp);
        }
        private static RobotTemplate GetRobotTemplateFromXY(IDictionary <string, object> d)
        {
            RobotTemplate robotTemplate = new RobotTemplate();
            bool          success       = d.TryGetValue("robot", out object rob);

            robotTemplate.robotID = Convert.ToInt32(rob);

            success = d.TryGetValue("head", out object heado) && success;
            robotTemplate.headID = Convert.ToInt32(heado);

            success = d.TryGetValue("chassis", out object chasso) && success;
            robotTemplate.chassisID = Convert.ToInt32(chasso);

            success             = d.TryGetValue("leg", out object lego) && success;
            robotTemplate.legID = Convert.ToInt32(lego);

            success = d.TryGetValue("container", out object cono) && success;
            robotTemplate.containerID = Convert.ToInt32(cono);

            success = d.TryGetValue("headModules", out object hmod) && success;
            Dictionary <string, object> headmods = (Dictionary <string, object>)hmod;

            robotTemplate.headModules = ModulesFromDictionary((IDictionary <string, object>)headmods);

            success = d.TryGetValue("chassisModules", out object cmod) && success;
            Dictionary <string, object> chassMods = (Dictionary <string, object>)cmod;

            robotTemplate.chassisModules = ModulesFromDictionary((IDictionary <string, object>)chassMods);

            success = d.TryGetValue("legModules", out object lmod) && success;
            Dictionary <string, object> legMods = (Dictionary <string, object>)lmod;

            robotTemplate.legModules = ModulesFromDictionary((IDictionary <string, object>)legMods);

            success = d.TryGetValue("items", out object imod) && success;
            Dictionary <string, object> items = (Dictionary <string, object>)imod;

            robotTemplate.items = ItemsFromDictionary((IDictionary <string, object>)items);

            return(robotTemplate);
        }
        public string SaveBotTemplate(RobotTemplate bot)
        {
            string query = string.Empty;

            using (SqlCommand command = new SqlCommand())
            {
                StringBuilder sqlCommand = new StringBuilder();
                sqlCommand.Append("UPDATE robottemplates SET name=@name, description=@description, note=@note WHERE id=@id;");

                command.CommandText = sqlCommand.ToString();

                command.Parameters.AddWithValue("@id", bot.recordID);
                command.Parameters.AddWithValue("@name", bot.recordName);
                command.Parameters.AddWithValue("@description", bot.ToGenXY());
                command.Parameters.AddWithValue("@note", bot.recordNote);

                SqlConnection conn = new SqlConnection(this.ConnString);
                conn.Open();
                command.Connection = conn;
                command.ExecuteNonQuery();
                conn.Close();
                query = command.CommandText;
                foreach (SqlParameter p in command.Parameters)
                {
                    if (SqlDbType.NVarChar.Equals(p.SqlDbType) || SqlDbType.VarChar.Equals(p.SqlDbType))
                    {
                        query = query.Replace(p.ParameterName, "'" + p.Value.ToString() + "'");
                    }
                    else
                    {
                        query = query.Replace(p.ParameterName, p.Value.ToString());
                    }
                }
            }
            return(query);
        }