public int UpdateUser(VMUserData changedData, string userMail)
        {
            int result = -1;

            using (var db = new LocalDbEntities2())
            {
                User dbUser = db.User.Where(u => u.email == userMail && u.active == true).FirstOrDefault();

                if (!string.IsNullOrEmpty(changedData.FirstName))
                {
                    dbUser.firstname = changedData.FirstName;
                }
                if (!string.IsNullOrEmpty(changedData.LastName))
                {
                    dbUser.lastname = changedData.LastName;
                }
                if (!string.IsNullOrEmpty(changedData.NewPassword))
                {
                    string salt = "";
                    dbUser.password = HashingHelper.SaltAndHashPassword(changedData.NewPassword, out salt);
                    dbUser.salt     = salt;
                }
                db.SaveChanges();
            }
            return(result);
        }
        public static VMAddress GetAddress(string email)
        {
            VMAddress result  = new VMAddress();
            Address   address = new Address();

            //City city = new City();
            //Country country = new Country();
            using (var db = new LocalDbEntities2())
            {
                address = db.Address.Where(a => a.User.email == email).FirstOrDefault();
                if (address != null)
                {
                    result = new VMAddress
                    {
                        CityName    = address.City.city1,
                        CountryIso  = address.City.Country.iso,
                        CountryName = address.City.Country.name,
                        Numbers     = address.numbers,
                        Street      = address.street,
                        Zip         = address.City.zip
                    };
                }
            }
            return(result);
        }
        //TODO: if existing city, country querys
        public static int EditOrUpdateAddress(VMAddress addressInfo)
        {
            int result = -1;

            using (var db = new LocalDbEntities2())
            {
                User user = db.User.Where(u => u.email == addressInfo.UserEmail).FirstOrDefault();

                Country country = db.Country.Where(c => c.name == addressInfo.CountryName).FirstOrDefault();
                if (country == null)
                {
                    country = new Country
                    {
                        iso     = addressInfo.CountryIso,
                        name    = addressInfo.CountryName,
                        created = DateTime.Now
                    };
                }
                City city = db.City.Where(c => c.city1 == addressInfo.CityName).FirstOrDefault();
                if (city == null)
                {
                    city = new City
                    {
                        city1   = addressInfo.CityName,
                        zip     = addressInfo.Zip,
                        Country = country,
                        created = DateTime.Now
                    };
                }
                Address address = db.Address.Where(a => a.User.email == user.email).FirstOrDefault();
                if (address == null)
                {
                    address = new Address
                    {
                        street  = addressInfo.Street,
                        numbers = addressInfo.Numbers,
                        created = DateTime.Now,
                        user_id = user.id,
                        city_id = city.id,
                        User    = user,
                        City    = city
                    };
                    db.Address.Add(address);
                }
                else
                {
                    address.street  = addressInfo.Street;
                    address.numbers = addressInfo.Numbers;
                    address.user_id = user.id;
                    address.city_id = city.id;
                    address.User    = user;
                    address.City    = city;
                }
                db.SaveChanges();
                result = 0;
            }
            return(result);
        }
        public VMUserData GetUserData(string email)
        {
            VMUserData result = null;

            using (var db = new LocalDbEntities2())
            {
                User dbUser = db.User.Where(u => u.email == email && u.active == true).FirstOrDefault();
                result = new VMUserData
                {
                    FirstName = dbUser.firstname,
                    LastName  = dbUser.lastname
                };
            }
            return(result);
        }
        public static List <VMCity> GetCities(string searchTerm)
        {
            List <VMCity> result = new List <VMCity>();

            using (var db = new LocalDbEntities2())
            {
                var dbCities = db.City.Where(c => c.city1.ToLower().StartsWith(searchTerm.ToLower())).ToList();

                foreach (var dbCity in dbCities)
                {
                    result.Add(new VMCity
                    {
                        CityId   = dbCity.id,
                        CityName = dbCity.city1
                    });
                }
            }
            return(result);
        }
        public static List <VMCountry> GetCountries(string searchTerm)
        {
            List <VMCountry> result = new List <VMCountry>();

            using (var db = new LocalDbEntities2())
            {
                var dbCountries = db.Country.Where(c => c.name.ToLower().StartsWith(searchTerm.ToLower())).ToList();

                foreach (var dbCountry in dbCountries)
                {
                    result.Add(new VMCountry
                    {
                        CountryId   = dbCountry.id,
                        CountryName = dbCountry.name
                    });
                }
            }
            return(result);
        }
        public bool CheckLogin(VMLogin loginInfo)
        {
            bool result = false;

            using (var db = new LocalDbEntities2())
            {
                User dbUser = db.User.Where(u => u.email == loginInfo.Email && u.active == true).FirstOrDefault();
                if (dbUser != null)
                {
                    loginInfo.Password += dbUser.salt;
                    loginInfo.Password  = HashingHelper.HashBerechnen(loginInfo.Password);
                    if (loginInfo.Password == dbUser.password)
                    {
                        result = true;
                    }
                }
            }
            return(result);
        }
        public VMAddress GetAddress(string email)
        {
            VMAddress result = null;

            using (var db = new LocalDbEntities2())
            {
                var dbAddress = db.Address.Where(a => a.User.email == email).FirstOrDefault();
                result = new VMAddress
                {
                    CityName    = dbAddress.City.city1,
                    CountryIso  = dbAddress.City.Country.iso,
                    CountryName = dbAddress.City.Country.name,
                    Numbers     = dbAddress.numbers,
                    Street      = dbAddress.street,
                    Zip         = dbAddress.City.zip
                };
            }
            return(result);
        }
        /// Per default ADMIN ROLE will be signed to the user where
        /// EMAIL="*****@*****.**"
        /// TODO: Think about a better solution...

        public int CreateUser(VMRegister registerInfo)
        {
            int    result         = (int)RegisterType.Error;
            string hashedSaltedPw = "";
            string salt           = "";

            using (var db = new LocalDbEntities2())
            {
                User dbUser = db.User.Where(u => u.email == registerInfo.Email).FirstOrDefault();
                if (dbUser == null)
                {
                    hashedSaltedPw = HashingHelper.SaltAndHashPassword(registerInfo.Password, out salt);
                    int userRole = (int)UserRoles.User;

                    if (registerInfo.Email == "*****@*****.**")
                    {
                        userRole = (int)UserRoles.Admin;
                    }
                    dbUser = new User
                    {
                        salt      = salt,
                        password  = hashedSaltedPw,
                        email     = registerInfo.Email,
                        lastname  = registerInfo.LastName,
                        firstname = registerInfo.FirstName,
                        role      = userRole,
                        active    = true,
                        status    = (int)UserState.NotVerified,
                        created   = DateTime.Now
                    };
                    db.User.Add(dbUser);
                    db.SaveChanges();
                    result = (int)RegisterType.Success;
                }
                else
                {
                    result = (int)RegisterType.UserExisting;
                }
            }
            return(result);
        }
        public List <string> GetRoles(string email)
        {
            List <string> result = new List <string>();

            using (var db = new LocalDbEntities2())
            {
                User dbUser = db.User.Where(u => u.email == email && u.active == true).FirstOrDefault();
                switch ((int)dbUser.role)
                {
                case (int)UserRoles.Admin:
                    result = new List <string> {
                        UserRoles.Admin.ToString()
                    };
                    break;

                case (int)UserRoles.User:
                    result = new List <string> {
                        UserRoles.User.ToString()
                    };
                    break;
                }
            }
            return(result);
        }