/// <summary> /// Inserts an e-mail into the database. In case sending the e-mail didn't work, it is still registered in the db /// </summary> /// <param name="uniqueString">The user's unique string</param> /// <param name="emailMessage">The e-mail message</param> /// <param name="emailSubject">The e-mail subject</param> /// <param name="eMailAddress">The users e-mail address. This is optional</param> public static void InsertEmailAttempt(string uniqueString, string emailMessage, string emailSubject, string eMailAddress = "") { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } EmailAttempts ea = new EmailAttempts(); ea.Username = Environment.UserName; ea.UserId = uniqueString; ea.Message = emailMessage; ea.Subject = emailSubject; if (!string.IsNullOrWhiteSpace(eMailAddress)) { ea.E_mail = eMailAddress; } db.EmailAttempts.Add(ea); db.SaveChanges(); } }
public static void TransformUniqueString(string uniqueStringOld, string uniqueStringNew, string remindMeVersion) { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } Users usr; //If the user doesn't exist in the db yet... if (db.Users.Where(u => u.UniqueString == uniqueStringOld).Count() > 0) { //Update the LastOnline attribute usr = db.Users.Where(u => u.UniqueString == uniqueStringOld).SingleOrDefault(); usr.UniqueString = uniqueStringNew; usr.LastOnline = DateTime.UtcNow; usr.SignIns++; usr.RemindMeVersion = remindMeVersion; db.SaveChanges(); } } }
/// <summary> /// Inserts a user into the database to keep track of how many users RemindMe has (after version 2.6.02) /// </summary> /// <param name="uniqueString"></param> public static void InsertOrUpdateUser(string uniqueString, string remindMeVersion) { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } Users usr; //If the user doesn't exist in the db yet... if (db.Users.Where(u => u.UniqueString == uniqueString).Count() == 0) { usr = new Users(); usr.Username = Environment.UserName; usr.UniqueString = uniqueString; usr.LastOnline = DateTime.UtcNow; usr.RemindMeVersion = remindMeVersion; usr.Material = 1; usr.AutoUpdate = 1; usr.ActiveReminders = 0; usr.DisabledReminders = 0; usr.DeletedReminders = 0; usr.ArchivedReminders = 0; usr.TotalReminders = 0; db.Users.Add(usr); } else { //Update the LastOnline attribute usr = db.Users.Where(u => u.UniqueString == uniqueString).SingleOrDefault(); usr.UniqueString = uniqueString; usr.LastOnline = DateTime.UtcNow; usr.SignIns++; usr.RemindMeVersion = remindMeVersion; usr.Material = (int)DLLocalDatabase.Setting.Settings.MaterialDesign.Value; usr.AutoUpdate = (int)DLLocalDatabase.Setting.Settings.AutoUpdate; } usr.ActiveReminders = DLReminders.GetReminders().Where(r => r.Enabled == 1).Count(); usr.DisabledReminders = DLReminders.GetReminders().Where(r => r.Enabled == 0).Count(); usr.DeletedReminders = DLReminders.GetDeletedReminders().Count; usr.ArchivedReminders = DLReminders.GetArchivedReminders().Count; usr.TotalReminders = DLReminders.GetAllReminders().Count; db.SaveChanges(); } }
public static void InsertTheme(Database.Entity.Themes theme) { try { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } UserThemes usrTheme = new UserThemes(); usrTheme.NormalPrimary = theme.Primary; usrTheme.DarkPrimary = theme.DarkPrimary; usrTheme.LightPrimary = theme.LightPrimary; usrTheme.Accent = theme.Accent; usrTheme.TextShade = theme.TextShade; usrTheme.Mode = theme.Mode.Value; usrTheme.ThemeName = theme.ThemeName; db.UserThemes.Add(usrTheme); db.SaveChanges(); } } catch (System.Data.Entity.Validation.DbEntityValidationException dbEx) { Exception raise = dbEx; foreach (var validationErrors in dbEx.EntityValidationErrors) { foreach (var validationError in validationErrors.ValidationErrors) { string message = string.Format("{0}:{1}", validationErrors.Entry.Entity.ToString(), validationError.ErrorMessage); // raise a new exception nesting // the current instance as InnerException raise = new InvalidOperationException(message, raise); } } throw raise; } }
/// <summary> /// Adds another count to the amount of times a message is read /// </summary> /// <param name="id"></param> public static void UpdateRemindMeMessageCount(int id) { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } RemindMeMessages mess = db.RemindMeMessages.Where(m => m.Id == id).FirstOrDefault(); mess.ReadByAmountOfUsers++; db.SaveChanges(); } }
public static bool IsUniqueString(string uniqueString) { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } return(db.Users.Where(u => u.UniqueString == uniqueString).SingleOrDefault() == null); } }
/// <summary> /// Logs an exception to the online database /// </summary> /// <param name="ex">The exception that is going to be logged</param> /// <param name="exceptionDate">The date the exception is logged at</param> public static void AddException(Exception ex, DateTime exceptionDate, string pathToSystemLog, string customMessage) { if (exceptionInserts >= MAX_EXCEPTION_INSERTS) { return; } int attemptCount = 0; using (remindmesqldbEntities db = new remindmesqldbEntities()) { while (!terminateDatabaseAccess && !terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } ExceptionLog log = new ExceptionLog(); log.ExceptionDate = exceptionDate; log.ExceptionMessage = ex.Message; log.CustomMessage = customMessage; if (pathToSystemLog != null && File.Exists(pathToSystemLog)) { log.SystemLog = File.ReadAllText(pathToSystemLog).Replace(Environment.NewLine, "¤");//so we can do a find and replace ¤ to \r\n in notepad++ to make it more readable } log.ExceptionStackTrace = ex.ToString(); log.ExceptionType = ex.GetType().ToString(); log.Username = Environment.UserName; db.ExceptionLog.Add(log); db.SaveChanges(); exceptionInserts++; } }
/// <summary> /// Checks wether RemindMe can make a connection to the online SQL Database. /// </summary> /// <param name="db">The entities object representing the database</param> /// <returns>True if a connection can be made, false if not</returns> private static bool CanConnect(remindmesqldbEntities db) { if (terminateDatabaseAccess) { return(false); } try { terminateDatabaseAccess = !db.Database.Exists(); if (terminateDatabaseAccess) { return(false); } } catch (Exception ex) { return(false); } //TODO: this might increase cpu usage a bit if connecting to the database fails return(true); }
/// <summary> /// Gets the RemindMe messages from the database, sent by the creator of RemindMe /// </summary> public static RemindMeMessages GetRemindMeMessageById(int id) { try { using (remindmesqldbEntities db = new remindmesqldbEntities()) { int attemptCount = 0; while (!terminateDatabaseAccess && !CanConnect(db)) { Thread.Sleep(500); if (attemptCount > MAX_ATTEMPTS) { break; } } return(db.RemindMeMessages.Where(m => m.Id == id).FirstOrDefault()); } } catch (DbUpdateException) { return(null); } catch { return(null); } }
private static bool CanConnect(remindmesqldbEntities db) { if (terminateDatabaseAccess) { return(false); } try { terminateDatabaseAccess = !db.Database.Exists(); if (terminateDatabaseAccess) { return(false); } db.Database.Connection.Open(); db.Database.Connection.Close(); } catch { return(false); } return(true); }