public static string get_box() { string json = null; string message2 = null; try { json = null; var Profile = new ProfileLocal(); var Profile_1 = new ProfileLocal(); var Box_Local = new BoxLocal(); using (var conn = new SQLite.SQLiteConnection(App.root_db)) { Profile_1 = conn.Table <ProfileLocal>().FirstOrDefault(); Box_Local = conn.Table <BoxLocal>().FirstOrDefault(); int coun = conn.Table <ProfileLocal>().Count(); string json_header = null; string json_body = null; string json_value = null; string json_fantasma = null; //11/30/2020 6:09:07 p. m. json_header = "Box recibida correctamente!\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n" + "¡"; json_value = "{" + @"""BoxId"":""" + Box_Local.BoxId + @""", ""Name"":""" + Box_Local.Name + @""", ""BoxDefault"":""" + Box_Local.BoxDefault + @""", ""UserId"":""" + Box_Local.UserId + @""", ""Time"":""" + Box_Local.Time + @""", ""ImagePath"":""" + Box_Local.ImagePath + @""", ""UserTypeId"":""" + Box_Local.UserTypeId + @""", ""FirstName"":""" + Box_Local.FirstName + @""", ""LastName"":""" + Box_Local.LastName + @""", ""ImageFullPath"":""" + Box_Local.ImageFullPath + @""", ""FullName"":""" + Box_Local.FullName + @""", ""ProfileLocalId"":""" + Profile_1.ProfileLocalId + @""", ""IdBox"":""" + Profile_1.IdBox + @""", ""UserId_p"":""" + Profile_1.UserId + @""", ""ProfileName"":""" + Profile_1.ProfileName + @""", ""value"":""" + Profile_1.value + @""", ""ProfileType"":""" + Profile_1.ProfileType + @""" }"; message2 = Box_Local.UserId.ToString(); //quitar //name bxl //BoxDefault //time //UserTypeId //ImageFullPath //FullName json_fantasma = "{" + @"""BoxId"":""-"", ""Name"":""" + Box_Local.Name + @""", ""BoxDefault"":""" + Box_Local.BoxDefault + @""", ""UserId"":""" + Box_Local.UserId + @""", ""Time"":""" + Box_Local.Time + @""", ""ImagePath"":""" + Box_Local.ImagePath + @""", ""UserTypeId"":""" + Box_Local.UserTypeId + @""", ""FirstName"":""" + Box_Local.FirstName + @""", ""LastName"":""" + Box_Local.LastName + @""", ""ImageFullPath"":""" + Box_Local.ImageFullPath + @""", ""FullName"":""" + Box_Local.FullName + @""", ""ProfileLocalId"":""" + Profile_1.ProfileLocalId + @""", ""IdBox"":""" + Profile_1.IdBox + @""", ""UserId_p"":""" + Profile_1.UserId + @""", ""ProfileName"":""" + Profile_1.ProfileName + @""", ""value"":""" + Profile_1.value + @""", ""ProfileType"":""" + Profile_1.ProfileType + @""" }"; if (coun > 1) { for (int i = 1; i < coun; i++) { Profile = conn.Table <ProfileLocal>().ElementAt(i); json_body = "{" + @"""BoxId"":""" + Box_Local.BoxId + @""", ""Name"":""" + Box_Local.Name + @""", ""BoxDefault"":""" + Box_Local.BoxDefault + @""", ""UserId"":""" + Box_Local.UserId + @""", ""Time"":""" + Box_Local.Time + @""", ""ImagePath"":""" + Box_Local.ImagePath + @""", ""UserTypeId"":""" + Box_Local.UserTypeId + @""", ""FirstName"":""" + Box_Local.FirstName + @""", ""LastName"":""" + Box_Local.LastName + @""", ""ImageFullPath"":""" + Box_Local.ImageFullPath + @""", ""FullName"":""" + Box_Local.FullName + @""", ""ProfileLocalId"":""" + Profile.ProfileLocalId + @""", ""IdBox"":""" + Profile.IdBox + @""", ""UserId_p"":""" + Profile.UserId + @""", ""ProfileName"":""" + Profile.ProfileName + @""", ""value"":""" + Profile.value + @""", ""ProfileType"":""" + Profile.ProfileType + @""" }"; json_value = json_value + ",\n" + json_body; } json_value = "[" + json_value + "]"; } else { json_value = "[" + json_value + ",\n" + json_fantasma + "]"; } json = "¬" + json_value; } } catch (Exception exx) { Console.Write(exx); json = null; } var message = json; return(message2); }
async void deleteBox(object sender, EventArgs e, int _BoxId, int _UserId, bool _BoxDefault) { string sqlDeleteEmails = "delete from dbo.Box_ProfileEmail where dbo.Box_ProfileEmail.BoxId = " + _BoxId, sqlDeletePhones = "delete from dbo.Box_ProfilePhone where dbo.Box_ProfilePhone.BoxId = " + _BoxId, sqlDeleteSMProfiles = "delete from dbo.Box_ProfileSM where dbo.Box_ProfileSM.BoxId = " + _BoxId, sqlDeleteWhatsappProfiles = "delete from dbo.Box_ProfileWhatsapp where dbo.Box_ProfileWhatsapp.BoxId = " + _BoxId, sqlDeleteBox = "delete from dbo.Boxes where dbo.boxes.BoxId = " + _BoxId; string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; //string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; StringBuilder sb; string sql; bool answer = await DisplayAlert(Resource.Warning, Resource.DeleteBoxNotification, Resource.Yes, Resource.No); #region If if (answer == true) { using (SqlConnection connection = new SqlConnection(cadenaConexion)) { //Borrar emails de la box sb = new System.Text.StringBuilder(); sb.Append(sqlDeleteEmails); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } //Borrar teléfonos de la box sb = new System.Text.StringBuilder(); sb.Append(sqlDeletePhones); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } //Borrar perfiles de redes sociales de la box sb = new System.Text.StringBuilder(); sb.Append(sqlDeleteSMProfiles); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } //Borrar perfiles de whatsapp de la box sb = new System.Text.StringBuilder(); sb.Append(sqlDeleteWhatsappProfiles); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } //Borrar box sb = new System.Text.StringBuilder(); sb.Append(sqlDeleteBox); sql = sb.ToString(); var apiSecurity = Application.Current.Resources["APISecurity"].ToString(); var Box = await this.apiService.GetBox( apiSecurity, "/api", "/Boxes", _BoxId); MainViewModel.GetInstance().Home.RemoveList(Box); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } //Si la box era predeterminada if (_BoxDefault == true) { //Borrar box predeterminada anterior using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.DeleteAll <BoxLocal>(); } //Borrar perfiles de box predeterminada anteriores using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.DeleteAll <ProfileLocal>(); } string sqlUpdateBoxDefault = "update top (1) dbo.Boxes set BoxDefault = 1 where dbo.Boxes.UserId = " + _UserId; BoxLocal boxLocal; bool boxLocalExists = false; int boxIdLocal = 0; //Definir nueva box default sb = new System.Text.StringBuilder(); sb.Append(sqlUpdateBoxDefault); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } string sqlGetNewDefault = "select * from dbo.Boxes " + "where dbo.Boxes.UserId = " + _UserId + "and dbo.Boxes.BoxDefault = 1"; //Definir nueva box default sb = new System.Text.StringBuilder(); sb.Append(sqlGetNewDefault); sql = sb.ToString(); //Creación de nueva box local using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { boxLocal = new BoxLocal { BoxId = (int)reader["BoxId"], BoxDefault = true, Name = (String)reader["Name"], UserId = MainViewModel.GetInstance().User.UserId, Time = (DateTime)reader["Time"], FirstName = MainViewModel.GetInstance().User.FirstName, LastName = MainViewModel.GetInstance().User.LastName, ImagePath = MainViewModel.GetInstance().User.ImagePath, UserTypeId = MainViewModel.GetInstance().User.UserTypeId }; //Crear box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.CreateTable <BoxLocal>(); conn.Insert(boxLocal); } //Crear tabla de perfiles de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.CreateTable <ProfileLocal>(); } boxLocalExists = true; boxIdLocal = (int)reader["BoxId"]; } } connection.Close(); } //Si se creo la box local, procedo a crear los perfiles locales if (boxLocalExists == true) { //Creación de perfiles locales de box local string queryGetBoxEmail = "select * from dbo.ProfileEmails " + "join dbo.Box_ProfileEmail on" + "(dbo.ProfileEmails.ProfileEmailId = dbo.Box_ProfileEmail.ProfileEmailId) " + "where dbo.Box_ProfileEmail.BoxId = " + boxIdLocal; string queryGetBoxPhone = "select * from dbo.ProfilePhones " + "join dbo.Box_ProfilePhone on" + "(dbo.ProfilePhones.ProfilePhoneId = dbo.Box_ProfilePhone.ProfilePhoneId) " + "where dbo.Box_ProfilePhone.BoxId = " + boxIdLocal; string queryGetBoxSMProfiles = "select * from dbo.ProfileSMs " + "join dbo.Box_ProfileSM on" + "(dbo.ProfileSMs.ProfileMSId = dbo.Box_ProfileSM.ProfileMSId) " + "join dbo.RedSocials on(dbo.ProfileSMs.RedSocialId = dbo.RedSocials.RedSocialId) " + "where dbo.Box_ProfileSM.BoxId = " + boxIdLocal; string queryGetBoxWhatsappProfiles = "select * from dbo.ProfileWhatsapps join dbo.Box_ProfileWhatsapp on " + "(dbo.ProfileWhatsapps.ProfileWhatsappId = dbo.Box_ProfileWhatsapp.ProfileWhatsappId) " + "where dbo.Box_ProfileWhatsapp.BoxId = " + boxIdLocal; //Consulta para obtener perfiles email using (SqlConnection conn = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxEmail); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn)) { conn.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal emailProfile = new ProfileLocal { IdBox = boxIdLocal, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Email"], ProfileType = "Email" }; //Crear perfil de correo de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(emailProfile); } } } conn.Close(); } } //Consulta para obtener perfiles teléfono using (SqlConnection conn = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxPhone); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn)) { conn.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal phoneProfile = new ProfileLocal { IdBox = boxIdLocal, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Phone" }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(phoneProfile); } } } conn.Close(); } } //Consulta para obtener perfiles de redes sociales using (SqlConnection conn = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxSMProfiles); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn)) { conn.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal smProfile = new ProfileLocal { IdBox = boxIdLocal, UserId = (int)reader["UserId"], ProfileName = (string)reader["ProfileName"], value = (string)reader["link"], ProfileType = (string)reader["Name"] }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(smProfile); } } } conn.Close(); } } //Consulta para obtener perfiles whatsapp using (SqlConnection conn = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxWhatsappProfiles); sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn)) { conn.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal whatsappProfile = new ProfileLocal { IdBox = boxIdLocal, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Whatsapp" }; //Crear perfil de whatsapp de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(whatsappProfile); } } } conn.Close(); } } } } } //Regresar a home MainViewModel.GetInstance().Home = new HomeViewModel(); Application.Current.MainPage = new MasterPage(); await PopupNavigation.Instance.PopAllAsync(); } #endregion //if(answer == true) //{ // MainViewModel.GetInstance().DetailsBoxEdith.DeleteBox(_BoxId); //} //else //{ // return; //} }
private void CheckLocalBox() { BoxLocal boxLocal = new BoxLocal(); bool valBoxLocal = false; bool valProfileLocal = false; using (var conn = new SQLite.SQLiteConnection(App.root_db)) { string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; //string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; string queryToGetBoxDefault = "select * from dbo.Boxes where dbo.boxes.UserId = " + MainViewModel.GetInstance().User.UserId + " and dbo.Boxes.BoxDefault = 1"; StringBuilder sb; var resultBoxLocal = conn.GetTableInfo("BoxLocal"); var resulForeingBox = conn.GetTableInfo("ForeingBox"); var resultForeingProfiles = conn.GetTableInfo("ForeingProfile"); if (resulForeingBox.Count == 0) { conn.CreateTable <ForeingBox>(); if (resultForeingProfiles.Count == 0) { conn.CreateTable <ForeingProfile>(); } } //Si no existe la tabla de las boxes locales... if (resultBoxLocal.Count == 0) { //Validamos si existe la tabla de perfiles locales var resultProfileLocal = conn.GetTableInfo("ProfileLocal"); //Crear tabla de box local conn.CreateTable <BoxLocal>(); //Si no existe la tabla de perfiles... if (resultProfileLocal.Count == 0) { //Creamos la tabla de perfiles local conn.CreateTable <ProfileLocal>(); } else { //Eliminamos los datos de la tabla de perfiles locales conn.DeleteAll <ProfileLocal>(); } //Buscar registros, y si existen, replicarlos a la box local using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryToGetBoxDefault); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { boxLocal = new BoxLocal { BoxId = (int)reader["BoxId"], BoxDefault = true, Name = (string)reader["Name"], UserId = MainViewModel.GetInstance().User.UserId, Time = (DateTime)reader["Time"], FirstName = MainViewModel.GetInstance().User.FirstName, LastName = MainViewModel.GetInstance().User.LastName, ImagePath = MainViewModel.GetInstance().User.ImagePath, UserTypeId = MainViewModel.GetInstance().User.UserTypeId }; conn.Insert(boxLocal); valBoxLocal = true; } } connection.Close(); } } //Si existe la box en la nube if (boxLocal.BoxId != 0) { //Creación de perfiles locales de box local string queryGetBoxEmail = "select * from dbo.ProfileEmails " + "join dbo.Box_ProfileEmail on" + "(dbo.ProfileEmails.ProfileEmailId = dbo.Box_ProfileEmail.ProfileEmailId) " + "where dbo.Box_ProfileEmail.BoxId = " + boxLocal.BoxId; string queryGetBoxPhone = "select * from dbo.ProfilePhones " + "join dbo.Box_ProfilePhone on" + "(dbo.ProfilePhones.ProfilePhoneId = dbo.Box_ProfilePhone.ProfilePhoneId) " + "where dbo.Box_ProfilePhone.BoxId = " + boxLocal.BoxId; string queryGetBoxSMProfiles = "select * from dbo.ProfileSMs " + "join dbo.Box_ProfileSM on" + "(dbo.ProfileSMs.ProfileMSId = dbo.Box_ProfileSM.ProfileMSId) " + "join dbo.RedSocials on(dbo.ProfileSMs.RedSocialId = dbo.RedSocials.RedSocialId) " + "where dbo.Box_ProfileSM.BoxId = " + boxLocal.BoxId; //Consulta para obtener perfiles email using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxEmail); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal emailProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Email"], ProfileType = "Email" }; //Crear perfil de correo de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(emailProfile); } } } conn1.Close(); } } //Consulta para obtener perfiles teléfono using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxPhone); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal phoneProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Phone" }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(phoneProfile); } } } conn1.Close(); } } //Consulta para obtener perfiles de redes sociales using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxSMProfiles); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal smProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["ProfileName"], value = (string)reader["link"], ProfileType = (string)reader["Name"] }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(smProfile); } } } conn1.Close(); } } //Validamos que se haya insertado al menos un perfil if (conn.Table <ProfileLocal>().Count() > 0) { valProfileLocal = true; } } if (valBoxLocal == true && valProfileLocal == true) { //this.get_box(); } } else { //********************************************* //Si la tabla de box local si existe //La vacíamos para colocar los nuevos valores conn.DeleteAll <BoxLocal>(); conn.DeleteAll <ProfileLocal>(); //Validamos que esté vacía int a = conn.Table <BoxLocal>().Count(); //Buscar registros, y si existen, replicarlos a la box local using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryToGetBoxDefault); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { boxLocal = new BoxLocal { BoxId = (int)reader["BoxId"], BoxDefault = true, Name = (string)reader["Name"], UserId = MainViewModel.GetInstance().User.UserId, Time = (DateTime)reader["Time"], FirstName = MainViewModel.GetInstance().User.FirstName, LastName = MainViewModel.GetInstance().User.LastName, ImagePath = MainViewModel.GetInstance().User.ImagePath, UserTypeId = MainViewModel.GetInstance().User.UserTypeId }; conn.Insert(boxLocal); valBoxLocal = true; } } connection.Close(); } } a = conn.Table <BoxLocal>().Count(); //Validamos que exista una box if (boxLocal.BoxId != 0) { //Creación de perfiles locales de box local string queryGetBoxEmail = "select * from dbo.ProfileEmails " + "join dbo.Box_ProfileEmail on" + "(dbo.ProfileEmails.ProfileEmailId = dbo.Box_ProfileEmail.ProfileEmailId) " + "where dbo.Box_ProfileEmail.BoxId = " + boxLocal.BoxId; string queryGetBoxPhone = "select * from dbo.ProfilePhones " + "join dbo.Box_ProfilePhone on" + "(dbo.ProfilePhones.ProfilePhoneId = dbo.Box_ProfilePhone.ProfilePhoneId) " + "where dbo.Box_ProfilePhone.BoxId = " + boxLocal.BoxId; string queryGetBoxSMProfiles = "select * from dbo.ProfileSMs " + "join dbo.Box_ProfileSM on" + "(dbo.ProfileSMs.ProfileMSId = dbo.Box_ProfileSM.ProfileMSId) " + "join dbo.RedSocials on(dbo.ProfileSMs.RedSocialId = dbo.RedSocials.RedSocialId) " + "where dbo.Box_ProfileSM.BoxId = " + boxLocal.BoxId; //Consulta para obtener perfiles email using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxEmail); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal emailProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Email"], ProfileType = "Email" }; //Crear perfil de correo de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(emailProfile); } } } conn1.Close(); } } //Consulta para obtener perfiles teléfono using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxPhone); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal phoneProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Phone" }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(phoneProfile); } } } conn1.Close(); } } //Consulta para obtener perfiles de redes sociales using (SqlConnection conn1 = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxSMProfiles); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, conn1)) { conn1.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal smProfile = new ProfileLocal { IdBox = boxLocal.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["ProfileName"], value = (string)reader["link"], ProfileType = (string)reader["Name"] }; //Crear perfil de teléfono de box local predeterminada using (var connSQLite = new SQLite.SQLiteConnection(App.root_db)) { connSQLite.Insert(smProfile); } } } conn1.Close(); } } //Validamos que se haya insertado al menos un perfil if (conn.Table <ProfileLocal>().Count() > 0) { valProfileLocal = true; } if (valBoxLocal == true && valProfileLocal == true) { //this.get_box(); } } } } }
public DetailBoxPopUpPage(Box _Box) { InitializeComponent(); apiService = new ApiService(); NavigationPage.SetHasNavigationBar(this, false); OSAppTheme currentTheme = Application.Current.RequestedTheme; FrameB.CloseWhenBackgroundIsClicked = true; #region Lastcode int BoxId = _Box.BoxId; var boxLocal = new BoxLocal(); int UserID = MainViewModel.GetInstance().User.UserId; string consultaDefault; string queryLastBoxCreated = "SELECT TOP 1 * FROM dbo.Boxes where dbo.Boxes.UserId = " + UserID + " ORDER BY BoxId DESC"; string queryUpdatesetDefault; string queryUpdateTakeOffDefault; string queryGetPhones; string queryGetEmails; string queryGetSMProfiles; string queryGetWhatsapp; string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; //string cadenaConexion = @"data source=serverappmynfo.database.windows.net;initial catalog=mynfo;user id=adminmynfo;password=4dmiNFC*Atx2020;Connect Timeout=60"; System.Text.StringBuilder sb; BoxName = ""; bool BoxDefault = _Box.BoxDefault; int UserId = 0; DateTime boxcreation = DateTime.Now; var BxSaveName = new ImageButton(); var BxBtnDelete = new ImageButton(); var bxBtnHome = new ImageButton(); var BxDefaultCheckBox = new CheckBox(); int listProfileNum = 0; //Asignación de querys consultaDefault = "select * from dbo.Boxes where dbo.Boxes.BoxId = " + BoxId; queryUpdatesetDefault = "update dbo.Boxes set BoxDefault = 1 where dbo.Boxes.UserId =" + UserID + " and dbo.Boxes.BoxId =" + BoxId; queryUpdateTakeOffDefault = "update dbo.Boxes set BoxDefault = 0 where dbo.Boxes.UserId =" + UserID + " and dbo.Boxes.BoxDefault = 1 and dbo.Boxes.BoxId !=" + BoxId; queryGetPhones = "select dbo.Boxes.BoxId, dbo.ProfilePhones.ProfilePhoneId, dbo.ProfilePhones.Name, " + "dbo.ProfilePhones.Number from dbo.Box_ProfilePhone Join dbo.Boxes " + "on(dbo.Boxes.BoxId = dbo.Box_ProfilePhone.BoxId) " + "Join dbo.ProfilePhones on(dbo.ProfilePhones.ProfilePhoneId = dbo.Box_ProfilePhone.ProfilePhoneId) " + "where dbo.Boxes.BoxId = " + BoxId; queryGetEmails = "select dbo.Boxes.BoxId, dbo.ProfileEmails.ProfileEmailId, dbo.ProfileEmails.Name, " + "dbo.ProfileEmails.Email from dbo.Box_ProfileEmail " + "Join dbo.Boxes on(dbo.Boxes.BoxId = dbo.Box_ProfileEmail.BoxId) " + "Join dbo.ProfileEmails on(dbo.ProfileEmails.ProfileEmailId = dbo.Box_ProfileEmail.ProfileEmailId) " + "where dbo.Boxes.BoxId = " + BoxId; queryGetSMProfiles = "select * from dbo.Box_ProfileSM " + "join dbo.ProfileSMs on(dbo.ProfileSMs.ProfileMSId = dbo.Box_ProfileSM.ProfileMSId) " + "join dbo.RedSocials on(dbo.ProfileSMs.RedSocialId = dbo.RedSocials.RedSocialId) " + "where dbo.Box_ProfileSM.BoxId = " + _Box.BoxId; queryGetWhatsapp = "select dbo.Boxes.BoxId, dbo.ProfileWhatsapps.ProfileWhatsappId, dbo.ProfileWhatsapps.Name, " + "dbo.ProfileWhatsapps.Number from dbo.Box_ProfileWhatsapp Join dbo.Boxes " + "on(dbo.Boxes.BoxId = dbo.Box_ProfileWhatsapp.BoxId) " + "Join dbo.ProfileWhatsapps on(dbo.ProfileWhatsapps.ProfileWhatsappId = dbo.Box_ProfileWhatsapp.ProfileWhatsappId) " + "where dbo.Boxes.BoxId =" + _Box.BoxId; ////Consulta para obtener Box //using (SqlConnection connection = new SqlConnection(cadenaConexion)) //{ // sb = new System.Text.StringBuilder(); // sb.Append(consultaDefault); // string sql = sb.ToString(); // using (SqlCommand command = new SqlCommand(sql, connection)) // { // connection.Open(); // using (SqlDataReader reader = command.ExecuteReader()) // { // while (reader.Read()) // { // BoxName = (string)reader["Name"]; // BoxDefault = (bool)reader["BoxDefault"]; // UserId = (int)reader["UserId"]; // boxcreation = (DateTime)reader["Time"]; // } // } // connection.Close(); // } //} //boxLocal.BoxId = _Box.BoxId; //boxLocal.Name = BoxName; //boxLocal.BoxDefault = BoxDefault; //boxLocal.UserId = UserId; //boxLocal.Time = boxcreation; //boxLocal.FirstName = MainViewModel.GetInstance().User.FirstName; //boxLocal.LastName = MainViewModel.GetInstance().User.LastName; //boxLocal.ImagePath = MainViewModel.GetInstance().User.ImagePath; //boxLocal.UserTypeId = MainViewModel.GetInstance().User.UserTypeId; //Definir color de fondo con respecto a si la box es predeterminada if (currentTheme == OSAppTheme.Light) { if (_Box.ColorBox != null) { FrameB.BackgroundColor = Color.FromHex(_Box.ColorBox); } else { FrameB.BackgroundColor = Color.FromHex("#c6c6c6"); } BackG.BackgroundColor = Color.FromHex("#FFFFFF"); bxBtnHome.BackgroundColor = Color.FromHex("#FFFFFF"); BxSaveName.BackgroundColor = Color.FromHex("#FFFFFF"); BxBtnDelete.BackgroundColor = Color.FromHex("#FFFFFF"); } else { if (_Box.ColorBox != null) { FrameB.BackgroundColor = Color.FromHex(_Box.ColorBox); } else { FrameB.BackgroundColor = Color.FromHex("#a4a4a4"); } BackG.BackgroundColor = Color.FromHex("#222b3a"); bxBtnHome.BackgroundColor = Color.FromHex("#222b3a"); BxSaveName.BackgroundColor = Color.FromHex("#222b3a"); BxBtnDelete.BackgroundColor = Color.FromHex("#222b3a"); } //Tamaño de etiqueta de box predeterminada if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.iOS) { defaultLabel.FontSize = 13; } //Navegación a ventana de perfiles BoxProfiles.Clicked += new EventHandler((sender, e) => BoxDetails_Clicked(sender, e, BoxId, BoxDefault, BoxName)); //Botón de Editar EdithButton.Clicked += new EventHandler((sender, e) => edithBox(sender, e, _Box, UserID, BoxDefault)); //Creación del checkbox de box predeterminada BxDefaultCheckBox.IsChecked = BoxDefault; BxDefaultCheckBox.VerticalOptions = LayoutOptions.Start; if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.iOS) { Thickness thick = new Thickness(20, -6, 0, 0); BoxDefaultCheckBox.Margin = thick; } else if (Xamarin.Forms.Device.RuntimePlatform == Xamarin.Forms.Device.Android) { Thickness thick = new Thickness(20, 0, 0, 0); BoxDefaultCheckBox.Margin = thick; } if (BoxDefault == true) { BxDefaultCheckBox.IsEnabled = false; if (currentTheme == OSAppTheme.Dark) { BxDefaultCheckBox.Color = Color.White; EdithButton.Source = "edit3"; BoxProfiles.Source = "plusb"; } else { BxDefaultCheckBox.Color = Color.FromHex("FF5521"); EdithButton.Source = "edit2"; BoxProfiles.Source = "Plus"; } } else { if (currentTheme == OSAppTheme.Dark) { BxDefaultCheckBox.IsEnabled = true; BxDefaultCheckBox.Color = Color.FromHex("FFFFFF"); EdithButton.Source = "edit3"; BoxProfiles.Source = "plusb"; } else { BxDefaultCheckBox.IsEnabled = true; BxDefaultCheckBox.Color = Color.FromHex("FF5521"); EdithButton.Source = "edit2"; BoxProfiles.Source = "Plus"; } } BxDefaultCheckBox.CheckedChanged += CheckDefaultBox; BoxDefaultCheckBox.Children.Add(BxDefaultCheckBox); //Marcar o desmarcar la box predeterminada void CheckDefaultBox(object sender, EventArgs e) { string queryGetBoxEmail = "select * from dbo.ProfileEmails " + "join dbo.Box_ProfileEmail on" + "(dbo.ProfileEmails.ProfileEmailId = dbo.Box_ProfileEmail.ProfileEmailId) " + "where dbo.Box_ProfileEmail.BoxId = " + _Box.BoxId; string queryGetBoxPhone = "select * from dbo.ProfilePhones " + "join dbo.Box_ProfilePhone on" + "(dbo.ProfilePhones.ProfilePhoneId = dbo.Box_ProfilePhone.ProfilePhoneId) " + "where dbo.Box_ProfilePhone.BoxId = " + _Box.BoxId; string queryGetBoxSMProfiles = "select * from dbo.ProfileSMs " + "join dbo.Box_ProfileSM on" + "(dbo.ProfileSMs.ProfileMSId = dbo.Box_ProfileSM.ProfileMSId) " + "join dbo.RedSocials on(dbo.ProfileSMs.RedSocialId = dbo.RedSocials.RedSocialId) " + "where dbo.Box_ProfileSM.BoxId = " + _Box.BoxId; string queryGetBoxWhatsapp = "select * from dbo.ProfileWhatsapps join dbo.Box_ProfileWhatsapp on " + "(dbo.ProfileWhatsapps.ProfileWhatsappId = dbo.Box_ProfileWhatsapp.ProfileWhatsappId) " + "where dbo.Box_ProfileWhatsapp.BoxId = " + _Box.BoxId; //Borrar box predeterminada anterior using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.DeleteAll <BoxLocal>(); } //Borrar perfiles de box predeterminada anteriores using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.DeleteAll <ProfileLocal>(); } //Crear box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.CreateTable <BoxLocal>(); conn.Insert(boxLocal); } //Crear tabla de perfiles de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.CreateTable <ProfileLocal>(); } //Consulta para obtener perfiles email using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxEmail); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal emailProfile = new ProfileLocal { IdBox = _Box.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Email"], ProfileType = "Email" }; //Crear perfil de correo de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.Insert(emailProfile); } } } connection.Close(); } } //Consulta para obtener perfiles teléfono using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxPhone); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal phoneProfile = new ProfileLocal { IdBox = _Box.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Phone" }; //Crear perfil de teléfono de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.Insert(phoneProfile); } } } connection.Close(); } } //Consulta para obtener perfiles de redes sociales using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxSMProfiles); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal smProfile = new ProfileLocal { IdBox = _Box.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["ProfileName"], value = (string)reader["link"], ProfileType = (string)reader["Name"] }; //Crear perfil de teléfono de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.Insert(smProfile); } } } connection.Close(); } } //Consulta para obtener perfiles de whatsapp using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryGetBoxWhatsapp); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); using (SqlDataReader reader = command.ExecuteReader()) { while (reader.Read()) { ProfileLocal whatsappProfile = new ProfileLocal { IdBox = _Box.BoxId, UserId = (int)reader["UserId"], ProfileName = (string)reader["Name"], value = (string)reader["Number"], ProfileType = "Whatsapp" }; //Crear perfil de teléfono de box local predeterminada using (var conn = new SQLite.SQLiteConnection(App.root_db)) { conn.Insert(whatsappProfile); } } } connection.Close(); } } //Consulta para predeterminar la box actual using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryUpdatesetDefault); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } //Consulta para quitar predeterminado de la box default anterior using (SqlConnection connection = new SqlConnection(cadenaConexion)) { sb = new System.Text.StringBuilder(); sb.Append(queryUpdateTakeOffDefault); string sql = sb.ToString(); using (SqlCommand command = new SqlCommand(sql, connection)) { connection.Open(); command.ExecuteNonQuery(); connection.Close(); } } MainViewModel.GetInstance().Home.GetBoxDefault(); BxDefaultCheckBox.IsEnabled = false; MainViewModel.GetInstance().Home.GetBoxNoDefault(); } #endregion }