forked from vegax87/qstn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentry.go
40 lines (31 loc) · 744 Bytes
/
entry.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package main
import (
"github.com/daryl/qstn/lib/db"
"github.com/daryl/qstn/utils"
"gopkg.in/mgo.v2/bson"
)
type Entry struct {
Id bson.ObjectId `bson:"_id,omitempty" json:"-"`
Slug string `bson:"slug" json:"slug"`
Question string `bson:"question" json:"question"`
Options []*Option `bson:"options" json:"options"`
}
type Option struct {
Option string `bson:"option" json:"option"`
Votes int `bson:"votes" json:"votes"`
}
func (e *Entry) genSlug() {
e.Slug = genUniqSlug(8)
}
func genUniqSlug(n int) string {
var duff interface{}
coll := db.D.C("entries")
slug := utils.RandSeq(n)
coll.Find(bson.M{
slug: slug,
}).One(&duff)
if duff != nil {
return genUniqSlug(n)
}
return slug
}