Пример #1
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
}
Пример #2
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
}
Пример #3
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)
}
Пример #4
0
func (e *Entries) othersAlreadyTook(a iface.Filter, from, to int64) error {
	entryQ := map[string]interface{}{
		"$or": []interface{}{
			map[string]interface{}{
				"from": map[string]interface{}{
					"$gt": from,
					"$lt": to,
				},
			},
			map[string]interface{}{
				"to": map[string]interface{}{
					"$gt": from,
					"$lt": to,
				},
			},
		},
	}
	a.AddQuery(entryQ)
	eC, err := a.Count()
	if err != nil {
		return err
	}
	if eC > 0 {
		return fmt.Errorf("That time is already taken.")
	}
	return nil
}
Пример #5
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)
}
Пример #6
0
func _new(db iface.Db, f iface.Filter, hooks iface.Hooks, client iface.Client) (*User, error) {
	uidI, err := client.GetDecrypted("user")
	if err != nil {
		return nil, err
	}
	id, err := db.ToId(uidI.(string))
	if err != nil {
		return nil, err
	}
	q := map[string]interface{}{
		"_id": id,
	}
	f.AddQuery(q)
	userDoc, err := f.SelectOne()
	if err != nil {
		return nil, err
	}
	var langs []string
	if langz, ok := userDoc.Data()["languages"].([]interface{}); ok {
		for _, v := range langz {
			langs = append(langs, v.(string))
		}
	} else if client.Languages() != nil {
		langs = client.Languages()
	} else {
		langs = []string{"en"}
	}
	return &User{
		userDoc,
		numcon.IntP(userDoc.Data()["level"]),
		langs,
	}, nil
}
Пример #7
0
func (c *C) RegenerateFulltext(a iface.Filter) error {
	cb := func(g iface.Document) error {
		upd := c.updateFromDoc(g.Data())
		return g.Update(upd)
	}
	return a.Iterate(cb)
}
Пример #8
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)
}
Пример #9
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)
}
Пример #10
0
func FindLogin(a iface.Filter, name, password string) (iface.Document, error) {
	encoded_pass := hashPass(password)
	q := map[string]interface{}{
		"name":     name,
		"password": encoded_pass,
	}
	return a.AddQuery(q).SelectOne()
}
Пример #11
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)
}
Пример #12
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)
}
Пример #13
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)
}
Пример #14
0
// This way professionals will only see entries posted for them,
// and users will only see entries posted by them.
// However, a professional will not be able to act as a user: he wont see entries posted by him, posted for other professionals.
func (e *Entries) TopModFilter(a iface.Filter) {
	if e.userIsProfessional {
		a.AddQuery(map[string]interface{}{
			"forProfessional": e.userId,
		})
	} else {
		a.AddQuery(map[string]interface{}{
			"createdBy": e.userId,
		})
	}
}
Пример #15
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)
}
Пример #16
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
}
Пример #17
0
func (tt *TimeTable) TopModFilter(a iface.Filter) {
	if tt.userIsProfessional {
		a.AddQuery(map[string]interface{}{
			"createdBy": tt.userId,
		})
	} else {
		a.AddQuery(map[string]interface{}{
			// Hehe.
			"clientsShouldNotViewTimeTables": true,
		})
	}
}
Пример #18
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
}
Пример #19
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
}
Пример #20
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
}
Пример #21
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
}
Пример #22
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
}
Пример #23
0
func (e *Entries) getTaken(a iface.Filter, from int64) (evenday.DaySchedule, error) {
	b := a.Clone()
	q := map[string]interface{}{
		"day": dateToString(from),
	}
	b.AddQuery(q)
	res, err := b.Find()
	if err != nil {
		return evenday.DaySchedule{}, err
	}
	gen := []interface{}{}
	for _, v := range res {
		gen = append(gen, v)
	}
	return evenday.GenericToDaySchedule(gen)
}
Пример #24
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
}
Пример #25
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
}
Пример #26
0
func (b *Basics) Get(a iface.Filter) ([]map[string]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
}
Пример #27
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
}
Пример #28
0
func (b *Basics) RemoveAll(a iface.Filter) error {
	_, err := a.RemoveAll()
	return err
}
Пример #29
0
func (b *Basics) Remove(a iface.Filter) error {
	return a.Remove()
}
Пример #30
0
func (b *Basics) GetSingle(a iface.Filter) (map[string]interface{}, error) {
	return a.FindOne()
}