예제 #1
0
// AddTask adds a task with the given description to the datastore,
// returning the key of the newly created entity.
func AddTask(ctx context.Context, client *datastore.Client, desc string) (*datastore.Key, error) {
	task := &Task{
		Desc:    desc,
		Created: time.Now(),
	}
	key := datastore.NewIncompleteKey(ctx, "Task", nil)
	return client.Put(ctx, key, task)
}
예제 #2
0
// newDatastoreDB creates a new BookDatabase backed by Cloud Datastore.
// See the cloud and google packages for details on creating a suitable context:
// https://godoc.org/google.golang.org/cloud
// https://godoc.org/golang.org/x/oauth2/google
func newDatastoreDB(client *datastore.Client) (BookDatabase, error) {
	ctx := context.Background()
	// Verify that we can communicate and authenticate with the datastore service.
	t, err := client.NewTransaction(ctx)
	if err != nil {
		return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
	}
	if err := t.Rollback(); err != nil {
		return nil, fmt.Errorf("datastoredb: could not connect: %v", err)
	}
	return &datastoreDB{
		client: client,
	}, nil
}
예제 #3
0
// [START update_entity]
// MarkDone marks the task done with the given ID.
func MarkDone(ctx context.Context, client *datastore.Client, taskID int64) error {
	// Create a key using the given integer ID.
	key := datastore.NewKey(ctx, "Task", "", taskID, nil)

	// In a transaction load each task, set done to true and store.
	_, err := client.RunInTransaction(ctx, func(tx *datastore.Transaction) error {
		var task Task
		if err := tx.Get(key, &task); err != nil {
			return err
		}
		task.Done = true
		_, err := tx.Put(key, &task)
		return err
	})
	return err
}
예제 #4
0
// [START retrieve_entities]
// ListTasks returns all the tasks in ascending order of creation time.
func ListTasks(ctx context.Context, client *datastore.Client) ([]*Task, error) {
	var tasks []*Task

	// Create a query to fetch all queries, ordered by "created".
	query := datastore.NewQuery("Task").Order("created")
	keys, err := client.GetAll(ctx, query, &tasks)
	if err != nil {
		return nil, err
	}

	// Set the id field on each Task from the corresponding key.
	for i, key := range keys {
		tasks[i].id = key.ID()
	}

	return tasks, nil
}
예제 #5
0
func (fe *FeServiceImpl) isValidNewConfig(ctx context.Context, dsClient *datastore.Client, config *api.BiosphereCreationConfig) (bool, error) {
	if config == nil {
		return false, nil
	}
	if config.Name == "" || config.Nx <= 0 || config.Ny <= 0 {
		return false, nil
	}
	// Name must be unique.
	qSameName := datastore.NewQuery("BiosphereMeta").Filter("Name =", config.Name)
	numSameName, err := dsClient.Count(ctx, qSameName)
	if err != nil {
		return false, err
	}
	if numSameName > 0 {
		return false, nil
	}
	return true, nil
}
예제 #6
0
// [START delete_entity]
// DeleteTask deletes the task with the given ID.
func DeleteTask(ctx context.Context, client *datastore.Client, taskID int64) error {
	return client.Delete(ctx, datastore.NewKey(ctx, "Task", "", taskID, nil))
}