Example #1
0
// GetFenceHandler GET /fences/{fenceId}
func GetFenceHandler(w http.ResponseWriter, r *http.Request) {
	user, _ := auth.ValidateSession(r)

	vars := mux.Vars(r)

	fenceID, err := strconv.ParseInt(vars["fenceId"], 10, 8)
	if err != nil {
		http.Error(w, "Invalid Fence ID. "+err.Error(), http.StatusUnauthorized)
		return
	}

	fence, err, notFound := models.FindFenceByID(fenceID)

	if notFound {
		http.Error(w, "GeoFence Not Found.", http.StatusNotFound)
		return
	}

	var f fenceResponse
	f.ID = fence.ID
	f.Lat = fence.Lat
	f.Lon = fence.Lon
	f.Name = fence.Name
	f.Radius = fence.Radius
	f.Owner = fence.UserID
	f.DiesAt = util.Timestamp(fence.DiesAt)
	f.RentMultiplier = fence.RentMultiplier
	f.OwnerName = fence.User.Name
	if user != nil && fence.UserID == user.ID {
		f.Cost = fence.Cost
		f.TotalEarnings = fence.TotalEarnings
		f.TotalVisitors = fence.TotalVisitors
	}

	bytes, err := json.Marshal(&f)

	if err != nil {
		InternalServerError(err, w)
		return
	}

	w.Write(bytes)
}
Example #2
0
// GetFencesHandler GET /fences
func GetFencesHandler(w http.ResponseWriter, r *http.Request) {
	lat, err1 := strconv.ParseFloat(r.URL.Query().Get("latitude"), 64)
	lon, err2 := strconv.ParseFloat(r.URL.Query().Get("longitude"), 64)
	radius, err3 := strconv.ParseInt(r.URL.Query().Get("radius"), 10, 64)
	userID, err4 := strconv.ParseInt(r.URL.Query().Get("user"), 10, 8)
	excludeOwn, _ := strconv.ParseBool(r.URL.Query().Get("excludeOwn"))

	if err1 == nil && err2 == nil && err3 == nil {
		user, err := auth.ValidateSession(r)

		var result []models.Fence

		if err == nil && excludeOwn {
			result, err = search.FindGeoFencesExceptByUser(lat, lon, radius, user.ID)
		} else {
			result, err = search.FindGeoFences(lat, lon, radius)
		}

		if err != nil {
			InternalServerError(err, w)
			return
		}

		fences := make([]fenceResponse, len(result))
		for i := range result {
			f := result[i]
			fences[i].ID = f.ID
			fences[i].Lat = f.Lat
			fences[i].Lon = f.Lon
			fences[i].Name = f.Name
			fences[i].Radius = f.Radius
			fences[i].Owner = f.UserID
			fences[i].DiesAt = util.Timestamp(f.DiesAt)
			fences[i].RentMultiplier = f.RentMultiplier
			fences[i].OwnerName = f.User.Name
			if user != nil && f.UserID == user.ID {
				fences[i].Cost = f.Cost
				fences[i].TotalEarnings = f.TotalEarnings
				fences[i].TotalVisitors = f.TotalVisitors
			}
		}

		bytes, err := json.Marshal(&fences)

		if err != nil {
			InternalServerError(err, w)
			return
		}

		if user != nil {
			user.LastKnownGeoHash = geomodel.GeoCell(lat, lon, models.LastKnownGeoHashResolution)
			err = user.Save()
			if err != nil {
				InternalServerError(err, w)
				return
			}
		}

		w.Write(bytes)
		return
	}

	if err4 == nil {
		user, _ := auth.ValidateSession(r)

		fenceUser, errA := models.FindUserByID(int(userID))

		if errA != nil {
			InternalServerError(errA, w)
			return
		}

		result, errA := fenceUser.GetFences()
		if errA != nil {
			InternalServerError(errA, w)
			return
		}

		fences := make([]fenceResponse, len(result))
		for i := range result {
			f := result[i]
			fences[i].ID = f.ID
			fences[i].Lat = f.Lat
			fences[i].Lon = f.Lon
			fences[i].Name = f.Name
			fences[i].Radius = f.Radius
			fences[i].Owner = f.UserID
			fences[i].DiesAt = util.Timestamp(f.DiesAt)
			fences[i].RentMultiplier = f.RentMultiplier
			fences[i].OwnerName = fenceUser.Name
			if user != nil && f.UserID == user.ID {
				fences[i].Cost = f.Cost
				fences[i].TotalEarnings = f.TotalEarnings
				fences[i].TotalVisitors = f.TotalVisitors
			}
		}

		bytes, err := json.Marshal(&fences)

		if err != nil {
			InternalServerError(err, w)
			return
		}

		w.Write(bytes)
		return
	}

	err := err1
	if err == nil {
		err = err2
	}
	if err == nil {
		err = err3
	}
	if err == nil {
		err = err4
	}
	if err == nil {
		err = errors.New("Please specify valid query options.")
	}

	InternalServerError(err, w)
}