Commit dde89b2f authored by Ikram Haq's avatar Ikram Haq
Browse files

Update the Get zone endpoint

parent 12484795
Loading
Loading
Loading
Loading
+81 −18
Original line number Diff line number Diff line
@@ -1835,11 +1835,45 @@ func apByIdGet(w http.ResponseWriter, r *http.Request) {
func zonesGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

	// Check if zoneId query parameter is provided
	zoneID := r.URL.Query().Get("zoneId")
	if zoneID != "" {
		// If zoneId is provided, retrieve information for that specific zone
		var zoneInfo ZoneInfo

		// Retrieve zone information from the DB
		jsonZoneInfo, _ := rc.JSONGetEntry(baseKey+typeZone+":"+zoneID, ".")
		if jsonZoneInfo == "" {
			w.WriteHeader(http.StatusNotFound)
			return
		}

		// Unmarshal the retrieved JSON into zoneInfo struct
		err := json.Unmarshal([]byte(jsonZoneInfo), &zoneInfo)
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}

		// Marshal the zoneInfo into JSON response
		jsonResponse, err := json.Marshal(InlineZoneInfo{ZoneInfo: &zoneInfo})
		if err != nil {
			log.Error(err.Error())
			errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
			return
		}

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, string(jsonResponse))
	} else {
		// If zoneId is not provided, retrieve information for all zones
		var response InlineZoneList
		var zoneList ZoneList
		zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
		response.ZoneList = &zoneList

		// Retrieve zone list information from the DB
		keyName := baseKey + typeZone + ":*"
		err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
		if err != nil {
@@ -1848,15 +1882,44 @@ func zonesGet(w http.ResponseWriter, r *http.Request) {
			return
		}

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

		w.WriteHeader(http.StatusOK)
		fmt.Fprint(w, string(jsonResponse))
	}
}

// func zonesGet(w http.ResponseWriter, r *http.Request) {
// 	w.Header().Set("Content-Type", "application/json; charset=UTF-8")

// 	var response InlineZoneList
// 	var zoneList ZoneList
// 	zoneList.ResourceURL = hostUrl.String() + basePath + "queries/zones"
// 	response.ZoneList = &zoneList

// 	keyName := baseKey + typeZone + ":*"
// 	err := rc.ForEachJSONEntry(keyName, populateZoneList, &zoneList)
// 	if err != nil {
// 		log.Error(err.Error())
// 		errHandlerProblemDetails(w, err.Error(), http.StatusInternalServerError)
// 		return
// 	}

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

func zonesByIdGet(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json; charset=UTF-8")