コード例 #1
0
ファイル: CassandraUserRepo.go プロジェクト: somanole/gaitapi
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
}
コード例 #2
0
ファイル: UserService.go プロジェクト: somanole/gaitapi
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
}