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);
                }
            }
        }
예제 #2
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 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);
                }
            }
        }
 public static bool IsEmailAddressAvailable(string email)
 {
     using (var model = new fmDbDataModel())
     {
         var data = model.fm_Users.FirstOrDefault(user => user.Email.Equals(email));
         return(data == null ? false : true);
     }
 }
예제 #8
0
        public static double SumIncomesSavings()
        {
            using (var model = new fmDbDataModel())
            {
                var sum = model.fm_Incomes
                          .Where(i => i.UserId == UserSingleton.Instance.Id && i.Type == (int)IncomeType.Saving)
                          .Select(i => i.Amount ?? 0)
                          .DefaultIfEmpty()
                          .Sum();

                return(sum);
            }
        }
        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;
            }
        }
예제 #10
0
        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));
            }
        }
예제 #11
0
        public static List <Income> GetIncomes(string sOrderByColumn, SearchIncomeVModel searchIncomes)
        {
            using (var model = new fmDbDataModel())
            {
                try
                {
                    var           props  = typeof(Income).GetProperties();
                    List <string> queris = new List <string>();
                    var           allQ   = "SELECT * FROM fm_Incomes";

                    // SearchFields
                    if (searchIncomes != null)
                    {
                        PropertyInfo[] searchProps = typeof(SearchIncomeVModel).GetProperties();
                        if (searchProps != null)
                        {
                            foreach (var sProp in searchProps)
                            {
                                switch (sProp.Name)
                                {
                                case "Name":
                                {
                                    if (!string.IsNullOrWhiteSpace(searchIncomes.Name))
                                    {
                                        var q1 = string.Format("UPPER(Name) like '%{0}%'", searchIncomes.Name);
                                        queris.Add(q1);
                                    }
                                }
                                break;

                                case "Amount":
                                {
                                    if (searchIncomes.Amount != null)
                                    {
                                        var q1 = string.Format("Amount = {0}", searchIncomes.Amount);
                                        queris.Add(q1);
                                    }
                                }
                                break;

                                case "IncomeType":
                                {
                                    if (searchIncomes.IncomeType != 0)
                                    {
                                        var q1 = string.Format("Type = {0}", (int)searchIncomes.IncomeType);
                                        queris.Add(q1);
                                    }
                                }
                                break;

                                case "InsertTime":
                                {
                                    if (searchIncomes.InsertTime != null)
                                    {
                                        var q1 = string.Format("InsertTime >= '{0}'", searchIncomes.InsertTime);
                                        queris.Add(q1);
                                    }
                                }
                                break;
                                }
                            }

                            if (queris.Any())
                            {
                                allQ = allQ + " WHERE " + string.Join(" AND ", queris);
                            }
                        }
                    }


                    var incomesFetched =
                        model.Database.SqlQuery <fm_Incomes>(allQ)
                        .ToList()
                        .Where(income => income.UserId == UserSingleton.Instance.Id)
                        .AsEnumerable()
                        .Select(o => new Income()
                    {
                        Name       = o.Name,
                        Amount     = o.Amount,
                        InsertTime = o.InsertTime,
                        IncomeType =
                            ((IncomeType)o.Type).GetType()?
                            .GetMember(((IncomeType)o.Type).ToString())?
                            .First()?
                            .GetCustomAttribute <DisplayAttribute>()?
                            .Name
                    }).ToList();

                    if (incomesFetched == null)
                    {
                        return(new List <Income>());
                    }



                    // SortingReturned
                    string substr = "";

                    if (!string.IsNullOrEmpty(sOrderByColumn))
                    {
                        foreach (var prop in props)
                        {
                            var columnName = prop.Name;
                            if (sOrderByColumn.Length >= 4)
                            {
                                substr = sOrderByColumn.Substring(sOrderByColumn.Length - 4);
                                if (substr == "Desc")
                                {
                                    if ((columnName + "Desc").ToString() == sOrderByColumn)
                                    {
                                        incomesFetched = incomesFetched.OrderByDescending(m => prop.GetValue(m, null)).ToList();
                                        break;
                                    }
                                }
                                else
                                {
                                    if (sOrderByColumn == columnName)
                                    {
                                        incomesFetched = incomesFetched.OrderBy(m => prop.GetValue(m, null)).ToList();
                                        break;
                                    }
                                }
                            }
                        }
                    }

                    return(incomesFetched);
                }
                catch (Exception e)
                {
                    _log.ErrorFormat("There was an error with fetching the incomes. Message: {0}, Stacktrace: {1}", e.Message, e.StackTrace);
                    return(null);
                }
            }
        }