Esempio n. 1
0
func (repo *CassandraUserRepo) GetUser(userId string) (types.User, error) {
	// get user by id
	var user types.User
	var user_id string
	var err error
	err = nil

	sql := fmt.Sprintf(`SELECT user_id, username, facebook_access_token, 
	twitter_access_token, google_access_token, push_token, 
	device_type, email, password, is_test, is_anonymous, gender_preference, 
	timestamp FROM users WHERE user_id = %v LIMIT 1`, userId)

	log.Printf(sql)

	if err = session.Query(sql).Scan(&user_id,
		&user.Username, &user.FacebookAccessToken, &user.TwitterAccessToken, &user.GoogleAccessToken,
		&user.PushToken, &user.DeviceType, &user.Email,
		&user.Password, &user.IsTest, &user.IsAnonymous, &user.GenderPreference, &user.Timestamp); err != nil {
		log.Printf(fmt.Sprintf("CassandraUserRepo.GetUser() - Error: %v", err.Error()))
	} else {
		user.UserId = uuid.Parse(user_id)
	}

	return user, err
}
Esempio n. 2
0
func UpdateUser(userId string, u types.UserUpdateRequest) (types.CreateUserResponse, error) {
	var err error
	var response types.CreateUserResponse
	err = nil

	if err = utilsservice.CheckIfUUID(userId); err == nil {
		var user types.User

		if user, err = GetUser(userId); err == nil {
			user.Timestamp = int64(time.Now().UTC().Unix())
			user.IsTest = u.IsTest

			if u.FacebookAccessToken != "" {
				user.FacebookAccessToken = u.FacebookAccessToken
			}
			if u.DeviceType != "" {
				user.DeviceType = u.DeviceType
			}
			if u.GenderPreference != "" {
				user.GenderPreference = u.GenderPreference
			}
			if u.GoogleAccessToken != "" {
				user.GoogleAccessToken = u.GoogleAccessToken
			}
			if u.Password != "" {
				user.Password = u.Password
			}
			if u.PushToken != "" {
				user.PushToken = u.PushToken
			}
			if u.TwitterAccessToken != "" {
				user.TwitterAccessToken = u.TwitterAccessToken
			}

			response, err = userRepo.UpdateUser(user)
		}
	}

	return response, err
}
Esempio n. 3
0
func CreateUser(ur types.UserRequest) (types.CreateUserResponse, error) {
	var err error
	var u types.User
	var userByEmail types.UserByEmail
	var response types.CreateUserResponse
	err = nil

	if !ur.IsAnonymous {
		userByEmail, err = GetUserByEmail(ur.Email)
	}

	if ur.IsAnonymous || (!ur.IsAnonymous && err != nil && err.Error() == "not found") {
		err = nil

		var username string
		wordnikResponse, err := utilsservice.GenerateRandomUsername()
		if err != nil {
			username = "******"
			err = nil
		} else if len(wordnikResponse) >= 2 {
			username = fmt.Sprintf("%v %v", wordnikResponse[0].Word, wordnikResponse[1].Word)
		} else {
			username = "******"
		}

		u.UserId = uuid.NewRandom()
		u.Username = username
		u.Timestamp = int64(time.Now().UTC().Unix())
		u.DeviceType = ur.DeviceType
		u.Email = ur.Email
		u.FacebookAccessToken = ur.FacebookAccessToken
		u.GenderPreference = ur.GenderPreference
		u.GoogleAccessToken = ur.GoogleAccessToken
		u.IsAnonymous = ur.IsAnonymous
		u.IsTest = false
		u.Password = ur.Password
		u.PushToken = ur.PushToken
		u.TwitterAccessToken = ur.TwitterAccessToken

		response, err = userRepo.CreateUser(u)
	} else if userByEmail.Email != "" {
		err = errors.New("email already registered")
	}

	return response, err
}