Example #1
0
func checkFenceOverlap(fence *models.Fence) (bool, error) {
	result, err := search.FindGeoFences(fence.Lat, fence.Lon, int64(fence.Radius+models.FenceMaxRadius))

	if err != nil {
		return false, err
	}

	if err != nil {
		return false, err
	}

	for i := range result {
		fenceB := result[i]
		distance := maths.Distance(fence.Lat, fence.Lon, fenceB.Lat, fenceB.Lon)
		if distance < float64(fence.Radius+fenceB.Radius) {
			return true, nil
		}
	}
	return false, nil
}
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)
}