Beispiel #1
0
func (c *C) RegenerateFulltext(a iface.Filter) error {
	cb := func(doc map[string]interface{}, g iface.Grabbed) error {
		upd := c.updateFromDoc(doc)
		return g.Update(upd)
	}
	return a.Iterate(cb)
}
Beispiel #2
0
func (c *C) New(a iface.Filter) ([]map[string]interface{}, error) {
	scheme, err := c.getScheme(a.Subject(), "Insert")
	if err != nil {
		return nil, err
	}
	return convert.SchemeToFields(scheme, nil)
}
Beispiel #3
0
func (b *Basics) UpdateAll(a iface.Filter, data map[string]interface{}) error {
	upd := map[string]interface{}{
		"$set": data,
	}
	_, err := a.UpdateAll(upd)
	return err
}
Beispiel #4
0
func (c *C) Insert(a iface.Filter, data map[string]interface{}) error {
	m, err := c.decrypt(data)
	if err != nil {
		return err
	}
	m["created"] = time.Now().UnixNano() // Should include user too maybe.
	return a.Insert(m)
}
Beispiel #5
0
func (c *C) Update(a iface.Filter, data map[string]interface{}) error {
	m, err := c.decrypt(data)
	if err != nil {
		return err
	}
	m["modified"] = time.Now().UnixNano()
	return a.Update(m)
}
Beispiel #6
0
func (c *C) DeleteFile(a iface.Filter, data map[string]interface{}) error {
	upd := map[string]interface{}{
		"$pull": map[string]interface{}{
			data["key"].(string): data["file"],
		},
	}
	return a.Update(upd)
}
Beispiel #7
0
func (c *C) SaveFulltext(a iface.Filter) error {
	doc, err := a.FindOne()
	if err != nil {
		return err
	}
	upd := c.updateFromDoc(doc)
	return a.Update(upd)
}
Beispiel #8
0
// Finds a user by id.
func FindUser(a iface.Filter, id bson.ObjectId) (map[string]interface{}, error) {
	q := m{"_id": id}
	v, err := a.AddQuery(q).FindOne()
	if err != nil {
		return nil, err
	}
	delete(v, "password")
	return v, nil
}
Beispiel #9
0
// Finds he user by name password equality.
func namePass(a iface.Filter, name, encoded_pass string) (map[string]interface{}, error) {
	q := bson.M{
		"name":     name,
		"password": encoded_pass,
	}
	doc, err := a.AddQuery(q).FindOne()
	if err != nil {
		return nil, err
	}
	return convert.Clean(doc).(map[string]interface{}), nil
}
Beispiel #10
0
func (c *C) Edit(a iface.Filter) ([]map[string]interface{}, error) {
	doc, err := a.FindOne()
	if err != nil {
		return nil, err
	}
	scheme, err := c.getScheme(a.Subject(), "Update")
	if err != nil {
		return nil, err
	}
	return convert.SchemeToFields(scheme, doc)
}
Beispiel #11
0
// Returns true if the username is still available.
func NameAvailable(a iface.Filter, name string) (bool, error) {
	q := bson.M{"slug": slugify.S(name)}
	users, err := a.AddQuery(q).Find()
	if err != nil {
		return false, err
	}
	if len(users) > 0 {
		return false, nil
	}
	return true, nil
}
Beispiel #12
0
func RegisterUser(a iface.Filter, user map[string]interface{}) (bson.ObjectId, error) {
	user["password"] = hashPass(user["password"].(string))
	if _, has := user["level"]; !has {
		user["level"] = 100
	}
	user_id := bson.NewObjectId()
	user["_id"] = user_id
	err := a.Insert(user)
	if err != nil {
		return "", fmt.Errorf("Name is not unique.")
	}
	return user_id, nil
}
Beispiel #13
0
func (c *C) Update(a iface.Filter, data map[string]interface{}) error {
	files_map, has := data["_files"].(map[string]interface{})
	upd := map[string]interface{}{
		"$set": data,
	}
	if has {
		ids, err := a.Ids()
		if err != nil {
			return err
		}
		err = c.moveFiles(a.Subject(), ids[0].Hex(), files_map)
		if err != nil {
			return err
		}
		delete(data, "_files")
		upd["$addToSet"] = eachIfNeeded(fileheadersToFilenames(files_map))
	}
	err := a.Update(upd)
	if err != nil {
		return err
	}
	if c.Ev != nil {
		c.Ev.Fire("Updated", a)
		c.Ev.Fire(a.Subject()+"Updated", a)
	}
	return nil
}
Beispiel #14
0
func (c *C) Edit(a iface.Filter) (string, error) {
	doc, err := a.FindOne()
	if err != nil {
		return "", err
	}
	for _, v := range ignore {
		delete(doc, v)
	}
	marsh, err := json.MarshalIndent(doc, "", "\t")
	if err != nil {
		return "", err
	}
	return string(marsh), nil
}
Beispiel #15
0
func (b *Basics) Update(a iface.Filter, data map[string]interface{}) error {
	upd := map[string]interface{}{
		"$set": data,
	}
	err := a.Update(upd)
	if err != nil {
		return err
	}
	if b.Ev != nil {
		b.Ev.Fire("Updated", a)
		b.Ev.Fire(a.Subject()+"Updated", a)
	}
	return nil
}
Beispiel #16
0
func hasAdmin(f iface.Filter) error {
	q := map[string]interface{}{
		"level": 300,
	}
	f.AddQuery(q)
	c, err := f.Count()
	if err != nil {
		return err
	}
	if c > 0 {
		return fmt.Errorf("Site already has an admin.")
	}
	return nil
}
Beispiel #17
0
func (f *Filter) Reduce(a ...iface.Filter) (iface.Filter, error) {
	l := len(a)
	if l == 0 {
		return &Filter{}, fmt.Errorf("Nothing to reduce.")
	}
	var prev iface.Filter
	prev = f
	for _, v := range a {
		ids, err := prev.Ids()
		if err != nil {
			return &Filter{}, err
		}
		v.AddParents("_"+prev.Subject(), ids)
		prev = v
	}
	return prev, nil
}
Beispiel #18
0
func (c *C) Insert(a iface.Filter, data map[string]interface{}) (bson.ObjectId, error) {
	files_map, has_files := data["_files"].(map[string]interface{})
	if has_files {
		delete(data, "_files")
		merge(data, fileheadersToFilenames(files_map))
	}
	id, err := c.Basics.Insert(a, data)
	if err != nil {
		return id, err
	}
	if has_files {
		err := c.moveFiles(a.Subject(), id.Hex(), files_map)
		if err != nil {
			return "", err
		}
	}
	return id, nil
}
Beispiel #19
0
func (b *Basics) Get(a iface.Filter) ([]interface{}, *QueryInfo, error) {
	list, err := a.Find()
	if err != nil {
		return nil, nil, err
	}
	count, err := a.Count()
	if err != nil {
		return nil, nil, err
	}
	return list, &QueryInfo{
		count, a.Modifiers().Skip(),
		a.Modifiers().Limit(),
		a.Modifiers().Sort(),
	}, nil
}
Beispiel #20
0
func (b *Basics) Insert(a iface.Filter, data map[string]interface{}) (bson.ObjectId, error) {
	id := bson.NewObjectId()
	data["_id"] = id
	err := a.Insert(data)
	if err != nil {
		return "", err
	}
	if b.Ev != nil {
		q := map[string]interface{}{
			"_id": id,
		}
		filt := a.Clone().AddQuery(q)
		b.Ev.Fire("Inserted", filt)
		b.Ev.Fire(a.Subject()+"Inserted", filt)
	}
	return id, nil
}
Beispiel #21
0
func (b *Basics) RemoveAll(a iface.Filter) error {
	_, err := a.RemoveAll()
	return err
}
Beispiel #22
0
func (c *C) DeleteAllFiles(a iface.Filter, data map[string]interface{}) error {
	upd := map[string]interface{}{
		"$unset": data["key"].(string), // We don't care about the files themselves.
	}
	return a.Update(upd)
}
Beispiel #23
0
func (b *Basics) GetSingle(a iface.Filter) (map[string]interface{}, error) {
	return a.FindOne()
}
Beispiel #24
0
func (b *Basics) Remove(a iface.Filter) error {
	return a.Remove()
}