public static bool SaveOutcome(CreateOutcomeVModel Outcome) { try { using (var model = new fmDbDataModel()) { fm_Outcomes newOutcome = new fm_Outcomes() { UserId = UserSingleton.Instance.Id, Name = Outcome.Name, Type = (int)Enum.Parse(typeof(OutcomeType), Outcome.OutcomeType.ToString()), Amount = Outcome.Amount, //Currency = (int)Enum.Parse(typeof(CurrencyType), Outcome.Currency.ToString()), InsertTime = DateTime.Now }; model.fm_Outcomes.Add(newOutcome); model.SaveChanges(); } return(true); } catch (Exception e) { DataOperationManager.VerifyResult(new Func <CreateOutcomeVModel, bool>(SaveOutcome), new object[] { Outcome }, MethodReturnStatus.Error); _log.ErrorFormat("There was an error with saving the outcome. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace); return(false); } }
public static void SwitchCurrency(CurrencyType?currency) { if (currency == null) { return; } using (var model = new fmDbDataModel()) { try { if (HttpContext.Current.Session["USER_ID"] != null) { long userId = Convert.ToInt64(HttpContext.Current.Session["USER_ID"].ToString()); var user = model.fm_Users.FirstOrDefault(u => u.Id == userId); if (user != null) { UserSingleton.Instance.DefaultCurrency = (int)currency; user.DefaultCurrency = (int)currency; model.SaveChanges(); DataOperationManager.Synch(new Action <CurrencyType?>(SwitchCurrency), new object[] { currency }); } else { DataOperationManager.VerifyResult(new Action <CurrencyType?>(SwitchCurrency), new object[] { currency }, MethodReturnStatus.Null); } } } catch (Exception e) { DataOperationManager.VerifyResult(new Action <CurrencyType?>(SwitchCurrency), new object[] { currency }, MethodReturnStatus.Error); _log.ErrorFormat("There was an error with inserting user to db. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace); } } }
public static bool InsertUser(RegisterVModel userRegister) { using (var model = new fmDbDataModel()) { try { string hashedPassword = SecurityManager.CalculateHash(userRegister.Password); fm_Users newUser = new fm_Users() { Name = userRegister.Name, Email = userRegister.Email, Password = hashedPassword, Salt = SecurityManager.GetSalt(), // later will be taken from database and fetched with new password to compare with the old one InsertTime = DateTime.Now, AccountStatus = (int)AccountStatus.Active }; // Adding the user to the currently connected database and now performing the same operation on the mirror ones. model.fm_Users.Add(newUser); model.SaveChanges(); DataOperationManager.Synch(new Func <RegisterVModel, bool>(InsertUser), new object[] { userRegister }); return(true); } catch (Exception e) { DataOperationManager.VerifyResult(new Func <RegisterVModel, bool>(InsertUser), new object[] { userRegister }, MethodReturnStatus.Error); _log.ErrorFormat("There was an error with inserting user to db. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace); return(false); } } }
public static double SumOutcomes() { try { using (var model = new fmDbDataModel()) { var sum = model.fm_Outcomes .Where(o => o.UserId == UserSingleton.Instance.Id) .Select(o => o.Amount ?? 0) .DefaultIfEmpty() .Sum(); return(sum); } } catch (Exception) { DataOperationManager.VerifyResult(new Func <double>(SumOutcomes), null, MethodReturnStatus.Error); throw; } }
public static LoginVModel UserSignIn(LoginVModel UserLogin) { fm_Users userExists = null; try { using (var model = new fmDbDataModel()) { userExists = model.fm_Users.FirstOrDefault(user => user.Email.Equals(UserLogin.Email)); if (userExists == null) { return((LoginVModel)DataOperationManager.VerifyResult(new Func <LoginVModel, LoginVModel>(UserSignIn), new object[] { UserLogin }, HostCommunication.HostModels.MethodReturnStatus.Null)); } string hashedPassword = SecurityManager.CalculateHash(UserLogin.Password, userExists.Salt); if (SecurityManager.ComparePasswords(userExists.Password, hashedPassword)) { bool isInitialized = UserManager.InitializeUserLogin(userExists); if (isInitialized) { return(new LoginVModel() { Email = UserLogin.Email, Password = hashedPassword }); } } _log.InfoFormat("User with {0} email could not login.", UserLogin.Email); return(null); } } catch (Exception e) { return((LoginVModel)DataOperationManager.VerifyResult(new Func <LoginVModel, LoginVModel>(UserSignIn), new object[] { UserLogin }, HostCommunication.HostModels.MethodReturnStatus.Error)); } }