Commit 7f7abe20 authored by Ikram Haq's avatar Ikram Haq
Browse files

Implement address query parameter in GET method of users subscriptions

parent 3b421356
Loading
Loading
Loading
Loading
+30 −34
Original line number Diff line number Diff line
@@ -3728,24 +3728,20 @@ func userSubListGET(w http.ResponseWriter, r *http.Request) {
	switch subscriptionType {
	case "periodic":
		keyName := baseKey + typePeriodicSubscription + "*"
		// If address parameter is provided, filter subscriptions by address
		if address != "" {
			keyName += ":" + address
		}
		err = rc.ForEachJSONEntry(keyName, populateUserSubList, &subscriptions)
		err = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
			return populateUserSubList([]byte(jsonInfo), address, &subscriptions)
		}, nil)
	case "event":
		keyName := baseKey + typeUserSubscription + "*"
		// If address parameter is provided, filter subscriptions by address
		if address != "" {
			keyName += ":" + address
		}
		err = rc.ForEachJSONEntry(keyName, populateUserSubList1, &subscriptions)
		err = rc.ForEachJSONEntry(keyName, func(key string, jsonInfo string, userData interface{}) error {
			return populateUserSubList1([]byte(jsonInfo), address, &subscriptions)
		}, nil)
	default:
		// If no subscription_type is provided, return both responses
		var userSubListPeriodic []Subscription
		keyNamePeriodic := baseKey + typePeriodicSubscription + "*"
		// If address parameter is provided, filter subscriptions by address
		errPeriodic := rc.ForEachJSONEntry(keyNamePeriodic, populateUserSubList, &userSubListPeriodic)
		errPeriodic := rc.ForEachJSONEntry(keyNamePeriodic, func(key string, jsonInfo string, userData interface{}) error {
			return populateUserSubList([]byte(jsonInfo), address, &userSubListPeriodic)
		}, nil)
		if errPeriodic != nil {
			log.Error(errPeriodic.Error())
			errHandlerProblemDetails(w, errPeriodic.Error(), http.StatusInternalServerError)
@@ -3754,9 +3750,9 @@ func userSubListGET(w http.ResponseWriter, r *http.Request) {

		var userSubListEvent []Subscription
		keyNameEvent := baseKey + typeUserSubscription + "*"
		// If address parameter is provided, filter subscriptions by address

		errEvent := rc.ForEachJSONEntry(keyNameEvent, populateUserSubList1, &userSubListEvent)
		errEvent := rc.ForEachJSONEntry(keyNameEvent, func(key string, jsonInfo string, userData interface{}) error {
			return populateUserSubList1([]byte(jsonInfo), address, &userSubListEvent)
		}, nil)
		if errEvent != nil {
			log.Error(errEvent.Error())
			errHandlerProblemDetails(w, errEvent.Error(), http.StatusInternalServerError)
@@ -3766,7 +3762,6 @@ func userSubListGET(w http.ResponseWriter, r *http.Request) {
		subscriptions = append(subscriptions, userSubListPeriodic...)
		subscriptions = append(subscriptions, userSubListEvent...)

		// No error since we're combining the lists
		err = nil
	}

@@ -4953,16 +4948,17 @@ func userTrackingSubPut(w http.ResponseWriter, r *http.Request) {
	fmt.Fprint(w, string(jsonResponse))
}

func populateUserSubList1(key string, jsonInfo string, userData interface{}) error {

	userList := userData.(*[]Subscription)
func populateUserSubList1(data []byte, address string, subscriptions *[]Subscription) error {
	var userInfo UserLocationEventSubscription

	// Format response
	err := json.Unmarshal([]byte(jsonInfo), &userInfo)
	if err != nil {
	if err := json.Unmarshal(data, &userInfo); err != nil {
		return err
	}

	// Filter subscriptions by address
	if address != "" && userInfo.Address != address {
		return nil
	}

	href := ""
	if userInfo.Links != nil && userInfo.Links.Self != nil {
		href = userInfo.Links.Self.Href
@@ -4971,25 +4967,25 @@ func populateUserSubList1(key string, jsonInfo string, userData interface{}) err
		Href:             href,
		SubscriptionType: userInfo.SubscriptionType,
	}
	*userList = append(*userList, sub)
	*subscriptions = append(*subscriptions, sub)
	return nil
}

func populateUserSubList(key string, jsonInfo string, userData interface{}) error {

	subscriptions := userData.(*[]Subscription)
	var userInfo UserLocationPeriodicSubscription

	// Format response
	err := json.Unmarshal([]byte(jsonInfo), &userInfo)
	if err != nil {
func populateUserSubList(data []byte, address string, subscriptions *[]Subscription) error {
	var userInfo UserLocationEventSubscription
	if err := json.Unmarshal(data, &userInfo); err != nil {
		return err
	}

	// Filter subscriptions by address
	if address != "" && userInfo.Address != address {
		return nil
	}

	href := ""
	if userInfo.Links != nil && userInfo.Links.Self != nil {
		href = userInfo.Links.Self.Href
	}
	// Create a Subscription instance
	sub := Subscription{
		Href:             href,
		SubscriptionType: userInfo.SubscriptionType,