public UserErrorModel RegisterUser(RegisterModel model)
        {
            var errors = new UserErrorModel();
            if (model == null)
            {
                errors.Messages.Add("Invalid data.");
                return errors;
            }

            if (model.UserInfo == null)
            {
                errors.Messages.Add("Invalid user data.");
                return errors;
            }

            if (
                DbContext.GetUsers()
                    .Any(
                        u =>
                            string.Equals(u.Email, model.UserInfo.Email, StringComparison.OrdinalIgnoreCase) ||
                            string.Equals(u.PhoneNumber, model.UserInfo.PhoneNumber, StringComparison.OrdinalIgnoreCase)))
            {
                errors.Messages.Add("The email address or phone number you are registering is not available. Please choose another email and phone number.");
                return errors;
            }

            var newUser = model.UserInfo.ToEntity();
            newUser.UserStatus = UserStatus.PendingVerification;

            var newProvider = default(ProviderEntity);
            if (model.ProviderInfo != null)
            {
                newProvider = model.ProviderInfo.ToEntity();
                var coordinates = GeoServiceProvider.FindGeoLocationByAddress(model.ProviderInfo.Address.StateOrProvince, model.ProviderInfo.Address.ZipCode, model.ProviderInfo.Address.City,
                    model.ProviderInfo.Address.FullAddressLine);

                if (coordinates == null || coordinates.Length != 2)
                {
                    errors.Messages.Add("Cannot verify the provider address");
                    newProvider.ProviderStatus = ProviderStatus.FailedOnVerifyAddress;
                }
                else
                {
                    newProvider.GeoLatitude = coordinates[0];
                    newProvider.GeoLongitude = coordinates[1];
                    newProvider.ProviderStatus = ProviderStatus.Verified;
                }
            }

            try
            {
                DbContext.SaveUsers(new List<UserEntity> { newUser });
                errors.UserId = DbContext.GetUsers().Single(u => string.Equals(u.Email, newUser.Email, StringComparison.OrdinalIgnoreCase)).UserId;
                if (newProvider != null)
                {
                    newProvider.ProviderId = errors.UserId;
                    DbContext.SaveProviders(new List<ProviderEntity> { newProvider });
                    errors.IsProvider = true;
                }

                return errors;
            }
            catch (Exception ex)
            {
                return new UserErrorModel { Messages = new List<string> { ex.Message } };
            }
        }
 public UserErrorModel LoginUser(LoginModel login)
 {
     var errors = new UserErrorModel();
     if (login == null)
     {
         errors.Messages.Add("Invalid data.");
         return errors;
     }
     var email = login.Username;
     var password = login.Password;
     if (string.IsNullOrWhiteSpace(email) || string.IsNullOrWhiteSpace(password))
     {
         errors.Messages.Add("Invalid username/password.");
         return errors;
     }
     var user =
         DbContext.GetUsers()
             .SingleOrDefault(u => string.Equals(email, u.Email, StringComparison.OrdinalIgnoreCase));
     if (user != null && string.Equals(user.Password, password, StringComparison.Ordinal))
     {
         errors.UserId = user.UserId;
         errors.IsProvider = DbContext.GetProviders().Any(p => p.ProviderId == user.UserId);
     }
     else
     {
         errors.Messages.Add("Invalid username/password.");
     }
     return errors;
 }