Example #1
0
File: pkg.go Project: wuxu92/snp
// get pkg's groups list as a map, using grp id as key
func (this *Pkg) GetGroups() map[string]Group {
	grpLen := len(this.Groups)
	if grpLen == 0 {
		return nil
	}
	grps := make(map[string]Group, grpLen)
	mgc := utils.GetMgc()
	c := mgc.GetDB().C("grp")
	//	for _, id := range this.Groups {
	//		grp := Group{}
	//		err := c.FindId(id).One(&grp)
	//		utils.ErrChk(err)
	//
	//		grps[id.Hex()] = grp
	//	}

	// replace find sites methods, using $in operator instead
	var grpArr []Group
	c.Find(bson.M{
		"_id": bson.M{
			"$in": this.Groups,
		},
	}).All(&grpArr)

	for _, grp := range grpArr {
		grps[grp.Id.Hex()] = grp
	}

	return grps
}
Example #2
0
File: site.go Project: wuxu92/snp
func GetSiteById(id bson.ObjectId) Site {
	c := utils.GetMgc().GetDB().C("site")
	site := Site{}
	err := c.FindId(id).One(&site)
	utils.ErrChk(err)
	return site
}
Example #3
0
File: pkg.go Project: wuxu92/snp
func (this *Pkg) Copy(newName string) (Pkg, error) {
	pkg := Pkg{
		bson.NewObjectId(),
		time.Now().Format(time.RFC3339),
		this.Title,
		this.Owner,
		newName,
		"123456",
		true,
		nil,
		"",
		"",
		1,
	}
	// copy groups
	newGrps := make([]bson.ObjectId, len(this.Groups))
	for idx, grpIdx := range this.Groups {
		old := GetGroupById(grpIdx)
		tmpGrp := old.Copy()
		newGrps[idx] = tmpGrp.Id
	}
	pkg.Groups = newGrps

	// save pkg to mongo
	c := utils.GetMgc().GetDB().C("pkg")
	err := c.Insert(&pkg)
	if err != nil {
		return pkg, err
	}
	return pkg, nil
}
Example #4
0
File: grp.go Project: wuxu92/snp
func (this *Group) Copy() Group {
	grp := Group{
		bson.NewObjectId(),
		time.Now().Format(time.RFC3339),
		this.Title,
		this.Owner,
		this.GroupLink,
		true,
		true,
		this.Id.Hex(),
		this.AdditionalStyle,
		nil,
		1,
	}
	sites := make([]bson.ObjectId, len(this.Sites))
	for idx, siteId := range this.Sites {
		old := GetSiteById(siteId)
		tmpSite := old.Copy()
		sites[idx] = tmpSite.Id
	}
	grp.Sites = sites
	// save group to mongo
	c := utils.GetMgc().GetDB().C("grp")
	err := c.Insert(&grp)
	utils.ErrChk(err)
	return grp
}
Example #5
0
File: grp.go Project: wuxu92/snp
func IsGroupExist(id bson.ObjectId) bool {
	c := utils.GetMgc().GetDB().C("grp")
	count, err := c.FindId(id).Count()
	if err != nil {
		return false
	}
	return count > 0
}
Example #6
0
File: grp.go Project: wuxu92/snp
func GetGroupById(id bson.ObjectId) Group {
	// db := utils.GetMgc().GetDB()
	c := utils.GetMgc().GetDB().C("grp")
	grp := Group{}
	err := c.FindId(id).One(&grp)
	utils.ErrChk(err)
	return grp
}
Example #7
0
File: site.go Project: wuxu92/snp
func IsSiteExist(id bson.ObjectId) bool {
	c := utils.GetMgc().GetDB().C("site")
	n, err := c.FindId(id).Count()
	if err != nil {
		return false
	} else {
		return n > 0
	}
}
Example #8
0
File: site.go Project: wuxu92/snp
func getSite(id string) models.Site {
	c := utils.GetMgc().GetDB().C("site")
	site := models.Site{}
	err := c.FindId(bson.ObjectIdHex(id)).One(&site)
	if err != nil {
		fmt.Println("not site find for: ", id, err.Error())
	}
	return site
}
Example #9
0
File: pkg.go Project: wuxu92/snp
/**
 * check if a name already exist
 */
func CheckPkgName(name string) bool {
	mgc := utils.GetMgc()
	c := mgc.GetDB().C("pkg")

	n, err := c.Find(bson.M{"name": name}).Count()
	utils.ErrChk(err)
	if n > 0 {
		return true
	} else {
		return false
	}
}
Example #10
0
File: site.go Project: wuxu92/snp
func (this *Site) Copy() Site {
	site := Site{
		bson.NewObjectId(),
		this.Title,
		this.Url,
		time.Now().Format(time.RFC3339),
		true,
	}
	c := utils.GetMgc().GetDB().C("site")
	err := c.Insert(&site)
	utils.ErrChk(err)
	return site
}
Example #11
0
File: pkg.go Project: wuxu92/snp
func GetPkgByName(name string) (Pkg, error) {
	mgc := utils.GetMgc()
	db := mgc.GetDB()
	c := db.C("pkg")

	var pkg Pkg
	err := c.Find(bson.M{"name": name}).One(&pkg)

	if err != nil {
		return pkg, errors.New("pkg not exist")
	}
	return pkg, nil
}
Example #12
0
File: grp.go Project: wuxu92/snp
// add a site to this group
// add a site to mgo first, and then add id to
// this.sites
func (this *Group) AddSite(title, url string) (Site, error) {
	site := Site{
		bson.NewObjectId(),
		title,
		url,
		time.Now().Format(time.RFC3339),
		true,
	}
	err := utils.GetMgc().GetDB().C("site").Insert(site)
	if err != nil {
		return Site{}, err
	}
	utils.GetLogger().Info("insert site: %s", site.Id.Hex())
	this.Sites = append(this.Sites, site.Id)
	this.Update()
	return site, nil
}
Example #13
0
File: grp.go Project: wuxu92/snp
func (this *Group) GetSites() map[string]Site {
	if len(this.Sites) == 0 {
		return nil
	}
	sites := make(map[string]Site)
	c := utils.GetMgc().GetDB().C("site")

	var siteArr []Site
	// replace find sites using mongodb $in operator with loop find
	c.Find(bson.M{
		"_id": bson.M{
			"$in": this.Sites,
		},
	}).All(&siteArr)

	for _, site := range siteArr {
		sites[site.Id.Hex()] = site
	}
	return sites
}
Example #14
0
File: grp.go Project: wuxu92/snp
// update change to mongodb
func (this *Group) Update() bool {
	c := utils.GetMgc().GetDB().C("grp")
	err := c.UpdateId(this.Id, this)
	return err == nil
}
Example #15
0
File: init.go Project: wuxu92/snp
func (ctl *InitController) Get() {
	session := utils.GetMgc().GetSession()

	// defer session.Close()

	session.SetMode(mgo.Monotonic, true)

	c := session.DB("snp").C("site")

	// result := make(Site, )
	c.RemoveAll(nil)
	sites := models.GetInitSites()
	siteArr := make([]interface{}, len(sites))
	for idx, _ := range sites {
		siteArr[idx] = sites[idx]
	}
	fmt.Println("to insert site count: ", len(sites))
	err := c.Insert(siteArr...)
	ErrorChk(err)
	//  query := c.Find(bson.M{})// .All(&result)
	fmt.Print("insert sites:")
	PrintQuery(c.Find(bson.M{}))
	siteCount, _ := c.Find(bson.M{}).Count()

	// create init groups
	grp1 := models.GetGroupFromSites("Tech", sites, 0, 10)
	grp2 := models.GetGroupFromSites("Read", sites, 10, 5)
	grp3 := models.GetGroupFromSites("Mine", sites, 15, 20)
	grp4 := models.GetGroupFromSites("Study", sites, 35, 2)
	grp5 := models.GetGroupFromSites("Music", sites, 37, 6)
	grp6 := models.GetGroupFromSites("Paper", sites, 43, 16)
	cg := session.DB("snp").C("grp")
	cg.RemoveAll(nil)
	err = cg.Insert(grp1, grp2, grp3, grp4, grp5, grp6)
	ErrorChk(err)

	fmt.Print("insert groups ")
	PrintQuery(cg.Find(bson.M{}))
	grpCount, _ := cg.Find(bson.M{}).Count()

	// int package
	cp := session.DB("snp").C("pkg")
	cp.RemoveAll(nil)
	err = cp.Insert(models.GetInitPkg([]models.Group{grp1, grp2, grp3, grp4, grp5, grp6}))
	ErrorChk(err)

	pkgCount, _ := cp.Find(bson.M{}).Count()

	fmt.Print("insert package ")
	PrintQuery(cp.Find(bson.M{}))

	ctl.Data["grpcount"] = grpCount
	ctl.Data["sitecount"] = siteCount
	ctl.Data["pkgcount"] = pkgCount
	// ctl.Data["Sites"] = &result
	ctl.Data["Website"] = "wuxu92.com"
	ctl.Data["Email"] = "*****@*****.**"
	ctl.Layout = "layout/main.html"
	ctl.TplName = "init.tpl"
	ctl.Render()
}
Example #16
0
File: site.go Project: wuxu92/snp
func (this *Site) Update() error {
	c := utils.GetMgc().GetDB().C("site")
	return c.UpdateId(this.Id, this)
}
Example #17
0
File: pkg.go Project: wuxu92/snp
// save change to mongodb
func (this *Pkg) Update() error {
	c := utils.GetMgc().GetDB().C("pkg")
	err := c.UpdateId(this.Id, this)
	return err
}