Пример #1
0
        public static bool SaveIncome(CreateIncomeVModel Income)
        {
            try
            {
                using (var model = new fmDbDataModel())
                {
                    fm_Incomes newIncome = new fm_Incomes()
                    {
                        UserId = UserSingleton.Instance.Id,
                        Name   = Income.Name,
                        Type   = (int)Enum.Parse(typeof(IncomeType), Income.IncomeType.ToString()),
                        Amount = Income.Amount,
                        //Currency = (int)Enum.Parse(typeof(CurrencyType), Income.Currency.ToString()),
                        InsertTime = DateTime.Now
                    };

                    model.fm_Incomes.Add(newIncome);
                    model.SaveChanges();
                }

                return(true);
            }
            catch (Exception e)
            {
                _log.ErrorFormat("There was an error with saving the income. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace);
                return(false);
            }
        }
        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 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 bool ResetUserOnLogout()
        {
            try
            {
                using (var model = new fmDbDataModel())
                {
                    int userId = Convert.ToInt32(HttpContext.Current.Session["USER_ID"]);

                    var userToLogout = model.fm_Users.FirstOrDefault(
                        user => user.Id == UserSingleton.Instance.Id
                        &&
                        user.Email == UserSingleton.Instance.Email);

                    userToLogout.LastSuccessfullLogin          = DateTime.Now;
                    userToLogout.IsOnline                      = (int)UserCurrentStatus.Offine;
                    model.Entry <fm_Users>(userToLogout).State = System.Data.Entity.EntityState.Modified;
                    model.SaveChanges();

                    UserSingleton.Reset();

                    HttpContext.Current.Session["USER_ID"]    = null;
                    HttpContext.Current.Session["USER_EMAIL"] = null;
                }

                return(true);
            }
            catch (Exception e)
            {
                _log.ErrorFormat("There was an error with resetting user data while loggin out. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace);
                return(false);
            }
        }
        public static bool InitializeUserLogin(fm_Users User)
        {
            try
            {
                using (var model = new fmDbDataModel())
                {
                    UserSingleton.CreateUserSingleton(User);

                    HttpContext.Current.Session["USER_ID"]    = User.Id;
                    HttpContext.Current.Session["USER_EMAIL"] = UserSingleton.Instance.Email;

                    User.LastSuccessfullLogin          = DateTime.Now;
                    User.IsOnline                      = (int)UserCurrentStatus.Online;
                    model.Entry <fm_Users>(User).State = System.Data.Entity.EntityState.Modified;
                    model.SaveChanges();
                }

                return(true);
            }
            catch (Exception e)
            {
                _log.ErrorFormat("There was an error with initializing user login. 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);
                }
            }
        }