Commit 04c28b4a authored by Muhammad Umair Khan's avatar Muhammad Umair Khan
Browse files

add query paramets fields and exclude_fields to MEC 021 Mobility Service GET request

parent 7f7a8871
Loading
Loading
Loading
Loading
+133 −23
Original line number Diff line number Diff line
@@ -1495,7 +1495,7 @@ func appMobilityServiceGET(w http.ResponseWriter, r *http.Request) {
	// Validate query parameters
	u, _ := url.Parse(r.URL.String())
	q := u.Query()
	validParams := []string{"filter", "All_fields", "Fields", "Exclude_fields", "Exclude_default"}
	validParams := []string{"filter", "all_fields", "fields", "exclude_fields", "exclude_default"}
	err := validateQueryParams(q, validParams)
	if err != nil {
		print("Query Parameter error")
@@ -1504,17 +1504,18 @@ func appMobilityServiceGET(w http.ResponseWriter, r *http.Request) {
	}

	// Parse query parameters
	urlFilter := q.Get("filter")
	// allFields := q.Get("all_fields")
	// field := q["fields"]
	// excludeFields := q["exclude_fields"]
	// excludeDefault := q.Get("exclude_default")
	if urlFilter != "" {
		fmt.Printf("filter: %s", urlFilter)
	}
	urlFilter := q.Get("Filter")
	urlAllFields := q.Get("all_fields")
	urlfields := q.Get("fields")
	urlExcludeFields := q.Get("exclude_fields")
	urlExcludeDefault := q.Get("exclude_default")
	regInfoList := &RegisterationInfoList{
		Filters: &FilterParameters{
			filter:          urlFilter,
			all_fields:      urlAllFields,
			fields:          urlfields,
			exclude_fields:  urlExcludeFields,
			exclude_default: urlExcludeDefault,
		},
		Registrations: make([]RegistrationInfo, 0),
	}
@@ -1565,19 +1566,7 @@ func populateRegInfoList(key string, jsonEntry string, response interface{}) err
			filterField := data.Filters.filter
			fmt.Printf("Filter Field: %s", filterField)
			// Split filterField into operator, attribute, and value
			operator, attribute, value, _ := parseFilter(string(filterField))

			fmt.Printf("operator: %s", operator)
			fmt.Printf("attribute: %s", attribute)
			fmt.Printf("value: %s", value)
			// filterField = strings.Trim(filterField, "()")
			// filterParts := strings.SplitN(filterField, ",", 3)
			// if len(filterParts) != 3 {
			// 	return nil
			// }
			// operator := filterParts[0]
			// attribute := filterParts[1]
			// value := filterParts[2]
			operator, attribute, value, _ := parseFilter(filterField)

			// Apply filters based on attribute
			switch attribute {
@@ -1608,6 +1597,18 @@ func populateRegInfoList(key string, jsonEntry string, response interface{}) err
					return nil
				}

			case "deviceInformation/appMobilityServiceLevel":
				matched := false
				for _, deviceInfo := range regInfo.DeviceInformation {
					if applyStringFilter(operator, string(*deviceInfo.AppMobilityServiceLevel), value) {
						matched = true
						break
					}
				}
				if !matched {
					return nil
				}

			case "deviceInformation/contextTransferState":
				matched := false
				for _, deviceInfo := range regInfo.DeviceInformation {
@@ -1635,7 +1636,116 @@ func populateRegInfoList(key string, jsonEntry string, response interface{}) err

		}

		// Handle Fields Parameter
		if data.Filters.fields != "" {
			fields := strings.Split(data.Filters.fields, ",")
			filteredRegInfo := RegistrationInfo{}

			for _, field := range fields {
				switch field {
				case "appMobilityServiceId":
					filteredRegInfo.AppMobilityServiceId = regInfo.AppMobilityServiceId

				case "serviceConsumerId/appInstanceId":
					if filteredRegInfo.ServiceConsumerId == nil {
						filteredRegInfo.ServiceConsumerId = &RegistrationInfoServiceConsumerId{}
					}
					if regInfo.ServiceConsumerId.AppInstanceId != "" {
						filteredRegInfo.ServiceConsumerId.AppInstanceId = regInfo.ServiceConsumerId.AppInstanceId
					}

				case "serviceConsumerId/mepId":
					if filteredRegInfo.ServiceConsumerId == nil {
						filteredRegInfo.ServiceConsumerId = &RegistrationInfoServiceConsumerId{}
					}
					if regInfo.ServiceConsumerId.MepId != "" {
						filteredRegInfo.ServiceConsumerId.MepId = regInfo.ServiceConsumerId.MepId
					}

				case "deviceInformation/associateId":
					for _, deviceInfo := range regInfo.DeviceInformation {
						if deviceInfo.AssociateId.Value != "" {
							filteredDeviceInfo := RegistrationInfoDeviceInformation{
								AssociateId: deviceInfo.AssociateId,
							}
							filteredRegInfo.DeviceInformation = append(filteredRegInfo.DeviceInformation, filteredDeviceInfo)
						}
					}

				case "deviceInformation/appMobilityServiceLevel":
					for _, deviceInfo := range regInfo.DeviceInformation {
						if *deviceInfo.AppMobilityServiceLevel != "" {
							filteredDeviceInfo := RegistrationInfoDeviceInformation{
								AppMobilityServiceLevel: deviceInfo.AppMobilityServiceLevel,
							}
							filteredRegInfo.DeviceInformation = append(filteredRegInfo.DeviceInformation, filteredDeviceInfo)
						}
					}

				case "deviceInformation/contextTransferState":
					for _, deviceInfo := range regInfo.DeviceInformation {
						if *deviceInfo.ContextTransferState != "" {
							filteredDeviceInfo := RegistrationInfoDeviceInformation{
								ContextTransferState: deviceInfo.ContextTransferState,
							}
							filteredRegInfo.DeviceInformation = append(filteredRegInfo.DeviceInformation, filteredDeviceInfo)
						}
					}

				case "expiryTime":
					//Logic
					if string(regInfo.ExpiryTime) != "" {
						filteredRegInfo.ExpiryTime = regInfo.ExpiryTime
					}

				}
				// Replace regInfo with the filtered version

			}
			regInfo = filteredRegInfo
		}

		// Handle Exclude Fields Parameter (Exclude specified fields)
		if data.Filters.exclude_fields != "" {
			excludeFields := strings.Split(data.Filters.exclude_fields, ",")
			filteredRegInfo := regInfo

			// Exclude the listed fields
			for _, field := range excludeFields {
				switch field {
				case "appMobilityServiceId":
					filteredRegInfo.AppMobilityServiceId = "" // Exclude this field
				case "serviceConsumerId/appInstanceId":
					if filteredRegInfo.ServiceConsumerId != nil {
						filteredRegInfo.ServiceConsumerId.AppInstanceId = "" // Exclude this field
					}
				case "serviceConsumerId/mepId":
					if filteredRegInfo.ServiceConsumerId != nil {
						filteredRegInfo.ServiceConsumerId.MepId = "" // Exclude this field
					}
				case "deviceInformation/associateId":
					for i := range filteredRegInfo.DeviceInformation {
						filteredRegInfo.DeviceInformation[i].AssociateId = nil // Exclude this field
					}
				case "deviceInformation/appMobilityServiceLevel":
					for i := range filteredRegInfo.DeviceInformation {
						filteredRegInfo.DeviceInformation[i].AppMobilityServiceLevel = nil // Exclude this field
					}
				case "deviceInformation/contextTransferState":
					for i := range filteredRegInfo.DeviceInformation {
						filteredRegInfo.DeviceInformation[i].ContextTransferState = nil // Exclude this field
					}
				case "expiryTime":
					filteredRegInfo.ExpiryTime = 0 // Exclude this field
				}
			}

			// Replace regInfo with the filtered version based on exclude_fields parameter
			regInfo = filteredRegInfo
		}

	}
	// Returning Data
	data.Registrations = append(data.Registrations, regInfo)
	return nil
}