Beispiel #1
0
func RegisterDropbox(req *http.Request, render render.Render, registerDropboxForm RegisterDropboxForm, account *models.Account, logger *middlewares.Logger, ds *appx.Datastore) {
	logger.Infof("You are in register dropbox")

	authorization := &models.ExternalServiceAuthorization{
		AuthorizationType: models.DropBox,
		AccessToken:       registerDropboxForm.AccessToken,
		UserId:            models.DropBox.String() + "-" + registerDropboxForm.UserId,
	}

	authorization.SetParentKey(account.Key())

	err := ds.Load(authorization)
	if err != nil {
		println("I failed you becasue: %v", err.Error())
	}
	exists := err == nil

	if err := ds.Save(authorization); err != nil {
		logger.Errorf("Unable to register for dropbox %v", err)
		render.JSON(http.StatusInternalServerError, "Unable to register dropbox")
		return
	}

	if exists {
		DropboxDelta(req, ds, account, authorization)
	} else {
		DropboxInit(req, ds, account, authorization)
	}

	render.Status(http.StatusOK)
}
Beispiel #2
0
func (trail *Trail) storeTags(context appengine.Context, db *appx.Datastore) {
	extractTags := func(data stream.T) stream.T {
		trail := data.(*Trail)
		return trail.AllTags()
	}

	err := rivers.FromData(trail).FlatMap(extractTags).Each(func(data stream.T) {
		tag := data.(*Tag)
		tag.SetParentKey(trail.ParentKey())
		tag.ImagePath = trail.Path
		tag.ImageProvider = DropBox
		datastore.RunInTransaction(context, func(c appengine.Context) error {
			if err := db.Load(tag); err == nil {
				tag.LikenessCount++
			} else {
				tag.LikenessCount = 1
			}

			return db.Save(tag)
		}, nil)
	}).Drain()
	if err != nil {
		println(">>>>Screwed", err.Error())
	}
}
Beispiel #3
0
func saveBatch(ds *appx.Datastore) stream.EachFn {
	return func(data stream.T) {
		trails := data.(*appx.DatastoreBatch)

		if err := ds.Save(trails.Items...); err != nil {
			panic(err)
		}
	}
}
Beispiel #4
0
func UpdateAccount(render render.Render, accountUpdateForm AccountUpdateForm, account *models.Account, db *appx.Datastore) {
	account.FirstName = accountUpdateForm.FirstName
	account.LastName = accountUpdateForm.LastName
	account.Email = accountUpdateForm.Email

	if err := db.Save(account); err != nil {
		render.JSON(http.StatusInternalServerError, "Unable to register dropbox")
	}

	render.Status(http.StatusOK)
}
func LoginHandler(r render.Render, user model.User, appx *appx.Datastore) {
	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	isValid, validationErr := user.IsValid()

	if !isValid {
		response.ErrorCode = http.StatusBadRequest
		response.Message = validationErr
	} else {
		err := appx.Save(&user)

		if err != nil {
			log.Printf("Error: %v", err)
			response.ErrorCode = http.StatusInternalServerError
			response.Message = append(response.Message, err.Error())
		} else {
			response.Data = user
		}
	}

	r.JSON(200, response)

}
func CreatePostHandler(r render.Render, post model.Post, appx *appx.Datastore, location middleware.RequestLocation) {

	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	post.CarPlate = strings.ToUpper(post.CarPlate)
	post.Country = location.Country
	post.CreatedAt = time.Now()

	isValid, validationErr := post.IsValid(location.Country)

	if !isValid {
		response.ErrorCode = http.StatusBadRequest
		response.Message = validationErr
	} else {
		err := appx.Save(&post)

		if err != nil {
			log.Printf("Error: %+v", err)
			response.ErrorCode = http.StatusInternalServerError
			response.Message = append(response.Message, err.Error())
		} else {
			postResource := &resources.PostResource{}
			postResource.From(post)
			response.Data = postResource
		}
	}

	r.JSON(200, response)
}
Beispiel #7
0
func Login(render render.Render, loginForm LoginForm, logger *middlewares.Logger, db *appx.Datastore, request *http.Request) {
	existingAccount := &models.Account{Id: loginForm.Id}
	if err := db.Load(existingAccount); err == nil {
		render.JSON(http.StatusOK, existingAccount)
		return
	}

	account := models.Accounts.New(loginForm.Id)
	if err := db.Save(account); err != nil {
		logger.Errorf("Error while trying to login for account by id: %v", err.Error())
		render.JSON(http.StatusBadRequest, err.Error())
		return
	}

	render.JSON(http.StatusCreated, account)
}
Beispiel #8
0
func DropboxDelta(req *http.Request, ds *appx.Datastore, authorization *models.ExternalServiceAuthorization, existingItem stream.PredicateFn) {
	dropboxClient := dropboxClient(newappengine.NewContext(req), authorization.AccessToken)
	builder := DropboxDeltaProducerBuilder{Client: dropboxClient, CurrentCursor: authorization.LastCursor}

	deltaStream, debugStream := rivers.
		From(builder.Build()).
		Drop(nonMediaFiles).
		Map(toTrailForAuthorization(authorization)).
		Drop(existingItem).
		Split()

	println("About to split data")

	err := deltaStream.BatchBy(&appx.DatastoreBatch{Size: 500}).
		Each(saveBatch(ds)).
		Drain()

	println(fmt.Sprintf("Error while batch saving the data: %v", err))

	total, errTotal := debugStream.Reduce(0, func(sum, item stream.T) stream.T {
		return sum.(int) + 1
	}).CollectFirst()

	if errTotal != nil {
		println(fmt.Sprintf("########## Guaging error %v", err))
		return
	}
	println(fmt.Sprintf("Total entries: %v", total.(int)))

	authorization.LastCursor = builder.CurrentCursor
	ds.Save(authorization)

	if err != nil {
		println(fmt.Sprintf("########## Delta error %v", err))
	}

}
Beispiel #9
0
func likeness(trailId string, likeness LikenessType, db *appx.Datastore, context appengine.Context) error {
	trail := Trail{}
	trail.SetEncodedKey(trailId)

	if err := db.Load(&trail); err != nil {
		println("The error: ", err.Error())
		return err
	}

	trail.Likeness = likeness
	trail.EvaluatedOn = time.Now()

	if trail.Likeness == LikedIt {
		trail.Tags = fetchLatLngFromGoogle(trail, context)
		trail.storeTags(context, db)
	}

	if err := db.Save(&trail); err != nil {
		println("The error: ", err.Error())
		return err
	}

	return nil
}
func FlagPostHandler(r render.Render, params martini.Params, appx *appx.Datastore) {
	postId := params["post_id"]

	response := model.Response{
		ErrorCode: http.StatusOK,
		Message:   []string{},
		Data:      nil,
	}

	post := model.Post{}
	post.SetEncodedKey(postId)
	err := appx.Load(&post)

	fmt.Print("aqui")
	if err != nil {
		fmt.Print("not found")
		fmt.Print(err)
		response.Message = []string{err.Error()}
		response.ErrorCode = http.StatusBadRequest
	} else {
		fmt.Print("found")
		fmt.Print(err)
		post.Flagged = true
		err = appx.Save(&post)

		if err != nil {
			fmt.Print("save problem")
			fmt.Print(err)
			response.Message = []string{err.Error()}
			response.ErrorCode = http.StatusInternalServerError
		} else {
			postResource := &resources.PostResource{}
			postResource.From(post)
			response.Data = postResource
		}
	}

	r.JSON(200, response)
}