Exemplo n.º 1
0
func (c *C) Update(a iface.Filter, data map[string]interface{}) error {
	upd := map[string]interface{}{
		"$set": data,
	}
	has_files := len(c.fileBiz) > 0
	if has_files {
		ids, err := a.Ids()
		if err != nil {
			return err
		}
		err = c.moveFiles(a.Subject(), ids[0].String())
		if err != nil {
			return err
		}
		upd["$addToSet"] = eachIfNeeded(fileNames(c.fileBiz))
	}
	err := a.Update(upd)
	if err != nil {
		return err
	}
	if c.Hooks != nil {
		c.Hooks.Select("Updated").Fire(a)
		c.Hooks.Select(a.Subject() + "Updated").Fire(a)
	}
	return nil
}
Exemplo n.º 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)
}
Exemplo n.º 3
0
// Returns the closest interval on the same day to the given interval.
func (e *Entries) GetClosest(a iface.Filter, data map[string]interface{}) (*evenday.Interval, error) {
	e.getOptions(a.Subject())
	prof, err := e.db.ToId(data["professional"].(string))
	if err != nil {
		return nil, err
	}
	from := data["from"].(int64)
	length := data["length"].(int64)
	err = e.intervalIsValid(data, prof, length)
	if err != nil {
		return nil, err
	}
	tt, err := e.getTimeTable(prof)
	if err != nil {
		return nil, err
	}
	day := evenday.DateToDayName(from)
	open := tt[day]
	taken, err := e.getTaken(a, from)
	if err != nil {
		return nil, err
	}
	adv := evenday.NewAdvisor(open, taken)
	to := from + length*60
	adv.Amount(1)
	interv, err := evenday.NewInterval(evenday.DateToMinute(from), evenday.DateToMinute(to))
	if err != nil {
		return nil, err
	}
	ret := adv.Advise(interv)
	if len(ret) == 0 {
		return nil, fmt.Errorf("Can't advise you, all day is taken.")
	}
	return ret[0], nil
}
Exemplo n.º 4
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)
}
Exemplo n.º 5
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)
}
Exemplo n.º 6
0
func (a *C) Login(f iface.Filter, data map[string]interface{}) error {
	name := data["name"].(string)
	pass := data["password"].(string)
	// Maybe there could be a check here to not log in somebody who is already logged in.
	if user, err := user_model.FindLogin(f, name, pass); err == nil {
		a.client.StoreEncrypted("user", f.Subject()+"|"+user.Id().String())
	} else {
		return err
	}
	return nil
}
Exemplo n.º 7
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.Hooks != nil {
		b.Hooks.Select("Updated").Fire(a)
		b.Hooks.Select(a.Subject() + "Updated").Fire(a)
	}
	return nil
}
Exemplo n.º 8
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
}
Exemplo n.º 9
0
func (c *C) Insert(a iface.Filter, data map[string]interface{}) (iface.Id, error) {
	has_files := len(c.fileBiz) > 0
	if has_files {
		merge(data, fileNames(c.fileBiz))
	}
	id, err := c.Basics.Insert(a, data)
	if err != nil {
		return id, err
	}
	if has_files {
		err := c.moveFiles(a.Subject(), id.String())
		if err != nil {
			return nil, err
		}
	}
	return id, nil
}
Exemplo n.º 10
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
}