Ejemplo n.º 1
0
func deleteCheckin(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	r.ParseForm()
	if r.Form["id"] == nil {
		app.ServeError(c, w, errors.New("Checkin not deleted. ID expected"))
		return
	}

	id, err := strconv.ParseInt(r.Form["id"][0], 10, 64)
	if err != nil {
		app.ServeError(c, w, errors.New("Checkin not deleted. Bad ID"))
		return
	}

	k := datastore.NewKey(c, "checkins", "", id, nil)

	// Elimino punto
	err = datastore.Delete(c, k)
	if err != nil {
		app.ServeError(c, w, errors.New("Checkin not deleted. Entity not found"))
		return
	}
}
Ejemplo n.º 2
0
func deleteImage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	key := appengine.BlobKey(r.FormValue("blobKey"))
	err := blobstore.Delete(c, key)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}
}
Ejemplo n.º 3
0
func getPoint(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_VIEW); err != nil {
		return
	}

	r.ParseForm()

	if r.Form["id"] != nil {
		id, err := strconv.ParseInt(r.Form["id"][0], 10, 64)
		if err != nil {
			app.ServeError(c, w, errors.New("User not found. Bad ID"))
			return
		}

		var p Point

		// Busco información del usuario en la base de datos
		// y relleno el objeto User para enviar

		k := datastore.NewKey(c, "points", "", id, nil)
		datastore.Get(c, k, &p)

		p.Id = k.IntID()
		msg, _ := json.Marshal(p)
		fmt.Fprintf(w, "%s", msg)
	}

	if r.Form["userId"] != nil {
		// Busco puntos de un usuario
		uid, err := strconv.ParseInt(r.Form["userId"][0], 10, 64)
		if err != nil {
			app.ServeError(c, w, errors.New("User not found. Bad ID"))
			return
		}

		var points []Point

		q := datastore.NewQuery("points").Filter("UserId =", uid)
		keys, _ := q.GetAll(c, &points)
		for i, key := range keys {
			points[i].Id = key.IntID()
		}

		msg, _ := json.Marshal(points)
		fmt.Fprintf(w, "%s", msg)
	}
}
Ejemplo n.º 4
0
func newImage(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	url, err := blobstore.UploadURL(c, "/images/upload", nil)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	fmt.Fprintf(w, "%s", url)
}
Ejemplo n.º 5
0
func newPoint(w http.ResponseWriter, r *http.Request) {

	var err error

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	var p Point

	decoder := json.NewDecoder(r.Body)
	err = decoder.Decode(&p)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	if err = p.IsValid(); err != nil {
		app.ServeError(c, w, err)
		return
	}

	/*

	 - From API reference in google cloud API:
	 NewKey creates a new key. kind cannot be empty. Either one or
	 both of stringID and intID must be zero. If both are zero,
	 the key returned is incomplete. parent must either be a
	 complete key or nil.

	*/

	key := datastore.NewKey(c, "points", "", p.Id, nil)
	key, err = datastore.Put(c, key, &p)
	p.Id = key.IntID()
	c.Infof("%d", key.IntID())

	jbody, err := json.Marshal(p)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	fmt.Fprintf(w, "%s", string(jbody[:len(jbody)]))
}
Ejemplo n.º 6
0
func storeImage(w http.ResponseWriter, r *http.Request) {
	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	blobs, _, err := blobstore.ParseUpload(r)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	file := blobs["img"]
	if len(file) == 0 {
		app.ServeError(c, w, errors.New(ERR_FILENOTSTORED))
		return
	}

	key := file[0].BlobKey

	info, err := blobstore.Stat(c, key)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	if info.Size > MAXSZIMAGE {
		blobstore.Delete(c, key)
		app.ServeError(c, w, errors.New(ERR_FILENOTVALID))
		return
	}

	/*

		key,err=resizeImage(c,key)
		if err != nil {
	                app.ServeError(c, w, errors.New(ERR_FILENOTVALID))
	                return
	        }

	*/

	fmt.Fprintf(w, "%s", string(key))
}
Ejemplo n.º 7
0
func newCheckin(w http.ResponseWriter, r *http.Request) {

	var err error

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	var ck Checkin

	decoder := json.NewDecoder(r.Body)
	err = decoder.Decode(&ck)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	if err = ck.IsValid(); err != nil {
		app.ServeError(c, w, err)
		return
	}

	// Paso del objeto TimeStamp que me llega por JSON a
	// un time.Time que es el unico compatible con el Datastore
	ck.DBStamp = time.Time(ck.Stamp)

	key := datastore.NewKey(c, "checkins", "", 0, nil)
	key, err = datastore.Put(c, key, &ck)
	ck.Id = key.IntID()

	jbody, err := json.Marshal(ck)
	if err != nil {
		app.ServeError(c, w, err)
		return
	}

	fmt.Fprintf(w, "%s", string(jbody[:len(jbody)]))
}
Ejemplo n.º 8
0
func deletePoint(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_UPDATE); err != nil {
		return
	}

	r.ParseForm()
	if r.Form["id"] == nil {
		app.ServeError(c, w, errors.New("Point not deleted. ID expected"))
		return
	}

	id, err := strconv.ParseInt(r.Form["id"][0], 10, 64)
	if err != nil {
		app.ServeError(c, w, errors.New("Point not deleted. Bad ID"))
		return
	}

	k := datastore.NewKey(c, "points", "", id, nil)

	// Eliminar imagen asociada a el del blobStore
	var p Point
	datastore.Get(c, k, &p)
	if p.ImageKey != "" {
		blobstore.Delete(c, appengine.BlobKey(p.ImageKey))
	}

	// Elimino punto
	err = datastore.Delete(c, k)
	if err != nil {
		app.ServeError(c, w, errors.New("Point not deleted. Entity not found"))
		return
	}

}
Ejemplo n.º 9
0
func getCheckin(w http.ResponseWriter, r *http.Request) {

	c := appengine.NewContext(r)
	if err := users.CheckPerm(w, r, users.OP_VIEW); err != nil {
		return
	}

	r.ParseForm()

	if r.Form["id"] != nil {
		id, err := strconv.ParseInt(r.Form["id"][0], 10, 64)
		if err != nil {
			app.ServeError(c, w, errors.New("Checkin not found. Bad ID"))
			return
		}

		var ck Checkin

		// Busco un checkin concreto
		k := datastore.NewKey(c, "checkins", "", id, nil)
		datastore.Get(c, k, &ck)

		msg, _ := json.Marshal(ck)
		fmt.Fprintf(w, "%s", msg)

		return
	}

	var checkins []Checkin

	if r.Form["userId"] != nil {
		// Busco checkins de un usuario
		uid, err := strconv.ParseInt(r.Form["userId"][0], 10, 64)
		if err != nil {
			app.ServeError(c, w, errors.New("User not found. Bad ID"))
			return
		}

		var nu users.NUser
		k := datastore.NewKey(c, "users", "", uid, nil)
		datastore.Get(c, k, &nu)

		q := datastore.NewQuery("checkins").Filter("UserId =", uid).Order("-DBStamp")
		keys, _ := q.GetAll(c, &checkins)

		// Relleno claves y fechas en formato TimeStamp para aplanarlas
		for i, key := range keys {
			checkins[i].Id = key.IntID()
			checkins[i].Stamp = TimeStamp(checkins[i].DBStamp)
			checkins[i].Username = nu.Name
		}
	}

	if r.Form["pointId"] != nil {
		// Busco checkins de un punto
		pid, err := strconv.ParseInt(r.Form["pointId"][0], 10, 64)
		if err != nil {
			app.ServeError(c, w, errors.New("Point not found. Bad ID"))
			return
		}

		q := datastore.NewQuery("checkins").Filter("PointId =", pid)
		keys, _ := q.GetAll(c, &checkins)
		for i, key := range keys {
			checkins[i].Id = key.IntID()
			checkins[i].Stamp = TimeStamp(checkins[i].DBStamp)

			var nu users.NUser
			k := datastore.NewKey(c, "users", "", checkins[i].UserId, nil)
			datastore.Get(c, k, &nu)
			checkins[i].Username = nu.Name
		}
	}

	msg, _ := json.Marshal(checkins)
	fmt.Fprintf(w, "%s", msg)
}
Ejemplo n.º 10
0
func handleServe(w http.ResponseWriter, r *http.Request) {
	if err := users.CheckPerm(w, r, users.OP_VIEW); err != nil {
		return
	}
	blobstore.Send(w, appengine.BlobKey(r.FormValue("blobKey")))
}