Ejemplo n.º 1
0
func (e *Entries) Insert(a iface.Filter, data map[string]interface{}) error {
	e.getOptions(a.Subject())
	from := data["from"].(int64)
	length := data["length"].(int64)
	prof, err := e.db.ToId(data["professional"].(string))
	if err != nil {
		return err
	}
	err = e.intervalIsValid(data, prof, length)
	if err != nil {
		return err
	}
	to := from + length*60
	err = e.okAccordingToTimeTable(data, from, to)
	if err != nil {
		return err
	}
	err = e.othersAlreadyTook(a, from, to)
	if err != nil {
		return err
	}
	i := map[string]interface{}{
		"createdBy":       e.userId,
		"from":            from,
		"to":              to,
		"length":          length,
		"day":             dateToString(from),
		"forProfessional": prof,
	}
	return a.Insert(i)
}
Ejemplo n.º 2
0
func (tt *TimeTable) Save(a iface.Filter, data map[string]interface{}) error {
	if !tt.userIsProfessional {
		return fmt.Errorf("Only professionals can save timetables.")
	}
	ssl := toSS(data["timeTable"].([]interface{}))
	timeTable, err := evenday.StringsToTimeTable(ssl)
	if err != nil {
		return err
	}
	count, err := a.Count()
	if err != nil {
		return err
	}
	m := map[string]interface{}{}
	m["createdBy"] = tt.userId
	m["timeTable"] = timeTable
	if count == 0 {
		return a.Insert(m)
	} else if count == 1 {
		doc, err := a.SelectOne()
		if err != nil {
			return err
		}
		return doc.Update(m)
	}
	_, err = a.RemoveAll()
	if err != nil {
		return err
	}
	return a.Insert(m)
}
Ejemplo n.º 3
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)
}
Ejemplo n.º 4
0
func RegisterUser(a iface.Filter, db iface.Db, user map[string]interface{}) (iface.Id, error) {
	user["password"] = hashPass(user["password"].(string))
	if _, has := user["level"]; !has {
		user["level"] = 100
	}
	user_id := db.NewId()
	user["_id"] = user_id
	err := a.Insert(user)
	if err != nil {
		return nil, fmt.Errorf("Name is not unique.")
	}
	return user_id, nil
}
Ejemplo n.º 5
0
func (b *Basics) Insert(a iface.Filter, data map[string]interface{}) (iface.Id, error) {
	id := b.Db.NewId()
	data["_id"] = id
	err := a.Insert(data)
	if err != nil {
		return nil, err
	}
	if b.Hooks != nil {
		q := map[string]interface{}{
			"_id": id,
		}
		filt := a.Clone().AddQuery(q)
		b.Hooks.Select("Inserted").Fire(filt)
		b.Hooks.Select(a.Subject() + "Inserted").Fire(filt)
	}
	return id, nil
}