Exemplo n.º 1
0
// Delete deletes a Note from this Notebook, removes it from any Tags that refer to it and deletes any Tags
// that no longer refer to any Notes
func (notebook *Notebook) Delete(id string, c appengine.Context) (bool, error) {
	err := datastore.RunInTransaction(c, func(tc appengine.Context) error {
		note := Note{ID: id}
		noteKey := note.Key(c)
		err := cachestore.Get(tc, noteKey, &note)
		if err != nil {
			c.Errorf("getting note: %s", err)
			return err
		}

		// remove note
		if Debug {
			tc.Debugf("deleting note: %#v", note)
		}
		err = cachestore.Delete(tc, noteKey)
		if err != nil {
			c.Errorf("deleting note: %s", err)
			return err
		}

		// remove note from tags
		err = notebook.updateTags(noteKey, &note, new(Note), tc)
		if err != nil {
			return err
		}

		// remove note from notebook
		notebook.NoteKeys = removeKey(notebook.NoteKeys, noteKey)
		return notebook.save(tc)
	}, &datastore.TransactionOptions{XG: true})
	if err != nil {
		memcache.Flush(c)
	}
	return err == nil, err
}
Exemplo n.º 2
0
// Note returns a note by its ID.
func (notebook *Notebook) Note(id string, c appengine.Context) (note Note, err error) {
	key, err := datastore.DecodeKey(id)
	if err == nil {
		if containsKey(notebook.NoteKeys, key) {
			err = cachestore.Get(c, key, &note)
		} else {
			err = errors.New("notebook does not contain note with ID: " + id)
		}
	}
	return note, err
}
Exemplo n.º 3
0
// newNoteKey returns a unique Key for a new Note. New Notes can be created with Keys generated outside of
// Tessernote. However if this Key is not unique (e.g. if it refers to a Note in another Notebook) then a new
// Key is generated.
func (notebook Notebook) newNoteKey(note *Note, c appengine.Context) *datastore.Key {
	if note.ID != "" {
		key, err := datastore.DecodeKey(note.ID)
		if err == nil {
			err = cachestore.Get(c, key, new(interface{}))
			if err == datastore.ErrNoSuchEntity {
				return key
			}
		}
		// note exists (probably in another notebook), use a different key
		note.ID = ""
	}
	return datastore.NewIncompleteKey(c, "Note", notebook.Key(c))
}
Exemplo n.º 4
0
// updateNote updates a Note in this Notebook, updating existing Tags to either start or stop pointing to it,
// cleaning up Tags that no longer point to any Note, and adding any new Tags.
func (notebook *Notebook) updateNote(note Note, c appengine.Context) (Note, error) {
	err := datastore.RunInTransaction(c, func(tc appengine.Context) error {
		// get old note
		var oldNote Note
		key := note.Key(tc)
		err := cachestore.Get(tc, key, &oldNote)
		if err != nil {
			tc.Errorf("getting old note: %s", err)
			return err
		}
		oldNote.ID = note.ID

		// add/update/delete tags
		err = notebook.updateTags(key, &oldNote, &note, tc)
		if err != nil {
			return err
		}

		// update note
		note.Created = oldNote.Created
		note.LastModified = time.Now()
		note.NotebookKeys = oldNote.NotebookKeys
		if Debug {
			tc.Debugf("updating note: %#v", note)
		}
		key, err = cachestore.Put(tc, key, &note)
		if err != nil {
			tc.Errorf("updating note: %s", err)
			return err
		}

		// update notebook
		return notebook.save(tc)
	}, &datastore.TransactionOptions{XG: true})
	if err != nil {
		memcache.Flush(c)
	}
	return note, err
}
Exemplo n.º 5
0
// CurrentNotebook returns the current user's Notebook
func CurrentNotebook(c appengine.Context) (*Notebook, error) {
	notebook := new(Notebook)
	u := user.Current(c)
	if u == nil {
		return notebook, errors.New("user is null")
	}
	notebook.ID = u.ID
	key := notebook.Key(c)
	err := cachestore.Get(c, key, notebook)
	if err != nil {
		if err != datastore.ErrNoSuchEntity {
			c.Warningf(err.Error())
		}
		// create new user
		if Debug {
			c.Debugf("adding new notebook for: %s", u.Email)
		}
		notebook.Name = u.Email
		notebook.Order = NewOrder()
		key, err = cachestore.Put(c, key, notebook)
	}
	return notebook, err
}