Commit 7918ac43 authored by Ikram Haq's avatar Ikram Haq
Browse files

Implement PUT method for Zone Event and Status Subsciptions

parent 8bfbe2c6
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -168,6 +168,9 @@ func ZoneSubGET(w http.ResponseWriter, r *http.Request) {
	zoneSubGET(w, r)
}

func ZoneSubPUT(w http.ResponseWriter, r *http.Request) {
	zoneSubPUT(w, r)
}
func ZoneSubDELETE(w http.ResponseWriter, r *http.Request) {
	zoneSubDELETE(w, r)
}
+196 −0
Original line number Diff line number Diff line
@@ -3542,7 +3542,42 @@ func userTrackingSubPost(w http.ResponseWriter, r *http.Request) {
	w.WriteHeader(http.StatusCreated)
	fmt.Fprint(w, string(jsonResponse))
}
func zoneSubPUT(w http.ResponseWriter, r *http.Request) {
	var requestBody []map[string]interface{}
	vars := mux.Vars(r)
	decoder := json.NewDecoder(r.Body)
	if err := decoder.Decode(&requestBody); err != nil {
		log.Error(err.Error())
		errHandlerProblemDetails(w, "Invalid request body format", http.StatusBadRequest)
		return
	}

	hasZoneSubscription := false
	hasStatusSubscription := false

	for _, body := range requestBody {
		if _, ok := body["zoneLocationEventSubscription"]; ok {
			hasZoneSubscription = true
		} else if _, ok := body["zoneStatusSubscription"]; ok {
			hasStatusSubscription = true
		}
	}

	// Check if both types of subscriptions are present
	if hasZoneSubscription && hasStatusSubscription {
		errHandlerProblemDetails(w, "Please send only one type of subscription at a time", http.StatusBadRequest)
		return
	}

	if hasZoneSubscription {
		handlezoneLocationEventSubscriptionPut(w, requestBody, vars["subscriptionId"])
	} else if hasStatusSubscription {
		handlezoneStatusSubscriptionPut(w, requestBody, vars["subscriptionId"])
	} else {
		errHandlerProblemDetails(w, "No valid subscription found in the request body", http.StatusBadRequest)
		return
	}
}
func userSubPUT(w http.ResponseWriter, r *http.Request) {
	var requestBody []map[string]interface{}
	vars := mux.Vars(r)
@@ -3579,7 +3614,168 @@ func userSubPUT(w http.ResponseWriter, r *http.Request) {
		return
	}
}
func handlezoneLocationEventSubscriptionPut(w http.ResponseWriter, requestBody []map[string]interface{}, subscriptionID string) {
	for _, body := range requestBody {
		if zoneSubscription, ok := body["zoneLocationEventSubscription"]; ok {
			// Convert the event subscription to a map
			zoneSubscriptionMap := zoneSubscription.(map[string]interface{})
			// Decode the event subscription map into a struct
			var userSubBody ZoneLocationEventSubscription
			err := mapstructure.Decode(zoneSubscriptionMap, &userSubBody)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}

			//checking for mandatory properties
			if userSubBody.CallbackReference == nil || userSubBody.CallbackReference.NotifyURL == "" {
				log.Error("Mandatory CallbackReference parameter not present")
				errHandlerProblemDetails(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
				return
			}
			if userSubBody.ZoneId == "" {
				log.Error("Mandatory ZoneId parameter not present")
				errHandlerProblemDetails(w, "Mandatory ZoneId parameter not present", http.StatusBadRequest)
				return
			}
			if userSubBody.ResourceURL == "" {
				log.Error("Mandatory ResourceURL parameter not present")
				errHandlerProblemDetails(w, "Mandatory ResourceURL parameter not present", http.StatusBadRequest)
				return
			}
			subsIdParamStr := subscriptionID
			selfUrl := strings.Split(userSubBody.ResourceURL, "/")
			subsIdStr := selfUrl[len(selfUrl)-1]

			//body content not matching parameters
			if subsIdStr != subsIdParamStr {
				log.Error("SubscriptionId in endpoint and in body not matching")
				errHandlerProblemDetails(w, "SubscriptionId in endpoint and in body not matching", http.StatusBadRequest)
				return
			}

			userSubBody.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr

			subsId, err := strconv.Atoi(subsIdStr)
			if err != nil {
				log.Error(err)
				w.WriteHeader(http.StatusBadRequest)
				return
			}

			if zonalSubscriptionMap[subsId] == "" {
				w.WriteHeader(http.StatusNotFound)
				return
			}

			_ = rc.JSONSetEntry(baseKey+typeZonalSubscription+":"+subsIdStr, ".", convertZonalSubscriptionToJson1(&userSubBody))

			deregisterZonal(subsIdStr)

			registerZonal1(userSubBody.ZoneId, userSubBody.LocationEventCriteria, subsIdStr)
			var response InlineZoneLocationEventSubscription
			response.ZoneLocationEventSubscription = &userSubBody

			// Marshal response
			jsonResponse, err := json.Marshal(response)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}

			// Write response
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))
			return

		}
	}
}
func handlezoneStatusSubscriptionPut(w http.ResponseWriter, requestBody []map[string]interface{}, subscriptionID string) {
	for _, body := range requestBody {
		if statusSubscription, ok := body["zoneStatusSubscription"]; ok {
			// Convert the event subscription to a map
			statusSubscriptionMap := statusSubscription.(map[string]interface{})
			// Decode the event subscription map into a struct
			var zoneStatusSub ZoneStatusSubscription
			err := mapstructure.Decode(statusSubscriptionMap, &zoneStatusSub)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}

			//checking for mandatory properties
			if zoneStatusSub.CallbackReference == nil || zoneStatusSub.CallbackReference.NotifyURL == "" {
				log.Error("Mandatory CallbackReference parameter not present")
				errHandlerProblemDetails(w, "Mandatory CallbackReference parameter not present", http.StatusBadRequest)
				return
			}
			if zoneStatusSub.ZoneId == "" {
				log.Error("Mandatory ZoneId parameter not present")
				errHandlerProblemDetails(w, "Mandatory ZoneId parameter not present", http.StatusBadRequest)
				return
			}
			if zoneStatusSub.ResourceURL == "" {
				log.Error("Mandatory ResourceURL parameter not present")
				errHandlerProblemDetails(w, "Mandatory ResourceURL parameter not present", http.StatusBadRequest)
				return
			}

			subsIdParamStr := subscriptionID
			selfUrl := strings.Split(zoneStatusSub.ResourceURL, "/")
			subsIdStr := selfUrl[len(selfUrl)-1]

			//body content not matching parameters
			if subsIdStr != subsIdParamStr {
				log.Error("SubscriptionId in endpoint and in body not matching")
				errHandlerProblemDetails(w, "SubscriptionId in endpoint and in body not matching", http.StatusBadRequest)
				return
			}

			zoneStatusSub.ResourceURL = hostUrl.String() + basePath + "subscriptions/zones/" + subsIdStr

			subsId, err := strconv.Atoi(subsIdStr)
			if err != nil {
				log.Error(err)
				w.WriteHeader(http.StatusBadRequest)
				return
			}

			if zoneStatusSubscriptionMap[subsId] == nil {
				w.WriteHeader(http.StatusNotFound)
				return
			}

			_ = rc.JSONSetEntry(baseKey+typeZoneStatusSubscription+":"+subsIdStr, ".", convertZoneStatusSubscriptionToJson(&zoneStatusSub))

			deregisterZoneStatus(subsIdStr)

			registerZoneStatus(zoneStatusSub.ZoneId, zoneStatusSub.UpperNumberOfUsersZoneThreshold, zoneStatusSub.UpperNumberOfUsersAPThreshold,
				zoneStatusSub.OperationStatus, subsIdStr, zoneStatusSub.LowerNumberOfUsersZoneThreshold, zoneStatusSub.LowerNumberOfUsersAPThreshold)
			var response InlineZoneStatusSubscription
			response.ZoneStatusSubscription = &zoneStatusSub

			// Marshal response
			jsonResponse, err := json.Marshal(response)
			if err != nil {
				log.Error(err.Error())
				errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
				return
			}

			// Write response
			w.Header().Set("Content-Type", "application/json; charset=UTF-8")
			w.WriteHeader(http.StatusCreated)
			fmt.Fprint(w, string(jsonResponse))
			return

		}
	}
}
func handleUserLocationEventSubscriptionPut(w http.ResponseWriter, requestBody []map[string]interface{}, subscriptionID string) {
	for _, body := range requestBody {
		if eventSubscription, ok := body["userLocationEventSubscription"]; ok {
+6 −0
Original line number Diff line number Diff line
@@ -343,6 +343,12 @@ var routes = Routes{
		"/location/v2/subscriptions/zones/{subscriptionId}",
		ZoneSubDELETE,
	},
	Route{
		"zoneSubPUT",
		strings.ToUpper("Put"),
		"/location/v2/subscriptions/zones/{subscriptionId}",
		ZoneSubPUT,
	},

	Route{
		"ZonalTrafficSubPOST",