Example #1
0
// DeleteAll deletes all Notes and Tags from this Notebook.
func (notebook *Notebook) DeleteAll(c appengine.Context) error {
	err := datastore.RunInTransaction(c, func(tc appengine.Context) error {
		err := cachestore.DeleteMulti(tc, notebook.NoteKeys)
		if err != nil {
			return err
		}
		return cachestore.DeleteMulti(tc, notebook.TagKeys)
	}, &datastore.TransactionOptions{XG: true})
	if err != nil {
		memcache.Flush(c)
	}
	return nil
}
Example #2
0
// updateTags calculates the Tag difference between oldNote and note. It adds missing Tags to the datastore,
// removes note from Tags that refered to oldNote but not note, and cleans up Tags that no longer refer to any Notes.
func (notebook *Notebook) updateTags(key *datastore.Key, oldNote, note *Note, c appengine.Context) error {
	tagKeys, tags, count, deleted, err := notebook.updateTagKeys(oldNote, note, c)
	if err != nil {
		return err
	}
	if len(tagKeys) > 0 {
		if Debug {
			c.Debugf("adding/updating tags: %#v", tags)
		}
		tagKeys, err := cachestore.PutMulti(c, tagKeys, tags)
		if err != nil {
			c.Errorf("adding/updating tags: %s", err)
			return err
		}
		// update note tags
		note.TagKeys = tagKeys[:count]
	}
	if len(deleted) > 0 {
		if Debug {
			c.Debugf("deleting empty tags: %#v", deleted)
		}
		err = cachestore.DeleteMulti(c, deleted)
		if err != nil {
			c.Errorf("deleting empty tags: %s", err)
		}
	}
	// update notebook tags
	if len(oldNote.TagKeys) > 0 && len(note.TagKeys) > 0 {
		notebook.removeTagKeys(deleted)
		notebook.addTagKeys(note.TagKeys)
	} else if len(oldNote.TagKeys) == 0 && len(note.TagKeys) > 0 {
		notebook.UntaggedNoteKeys = removeKey(notebook.UntaggedNoteKeys, key)
		notebook.addTagKeys(note.TagKeys)
	} else if len(oldNote.TagKeys) > 0 && len(note.TagKeys) == 0 {
		notebook.removeTagKeys(deleted)
		if note.ID != "" {
			notebook.UntaggedNoteKeys = append(notebook.UntaggedNoteKeys, key)
		}
	} else if note.ID == "" {
		notebook.UntaggedNoteKeys = removeKey(notebook.UntaggedNoteKeys, key)
	}
	return err
}