Пример #1
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
}
Пример #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
}