Ejemplo n.º 1
0
Archivo: grp.go Proyecto: wuxu92/snp
func getGroup(id string) map[string]interface{} {
	fmt.Println("get group", id)
	grp := models.GetGroupById(bson.ObjectIdHex(id))
	data := make(map[string]interface{})
	data["grp"] = grp
	data["sites"] = grp.GetSites()
	return data
}
Ejemplo n.º 2
0
Archivo: site.go Proyecto: wuxu92/snp
// Post for edit site, request method post
// /api/site/edit/siteId?grp=grpid
func (this *SiteController) Post() {
	action := this.Ctx.Input.Param(":action")
	sid := this.Ctx.Input.Param(":id")
	gid := this.GetString("grp", "")
	title := this.GetString("title", "")
	url := this.GetString("url", "")

	utils.GetConsole().Info("update site, action:%s, id: %s, grp:%s", action, sid, gid)

	data := ResObj{}
	if gid == "" || title == "" || url == "" {
		data.code = 1
		data.message = "one of grp/title/url is null"
		this.Data["json"] = data.Json()
		this.ServeJSON()
		return
	}
	// get group
	siteId := bson.ObjectIdHex(sid)
	grpId := bson.ObjectIdHex(gid)
	if ge, se := !models.IsGroupExist(grpId), !models.IsSiteExist(siteId); se || ge {
		data.code = 2
		if !se {
			data.message = "site not exist"
		} else if !ge {
			data.message = "group not exist"
		} else {
			data.message = "group and site not exist"
		}
		this.Data["json"] = data.Json()
		this.ServeJSON()
		return
	}
	grp := models.GetGroupById(grpId)
	if !grp.HasSiteId(siteId) {
		data.code = 3
		data.message = "grp do't has this site"
		this.Data["json"] = data.Json()
		this.ServeJSON()
		return
	}
	site := models.GetSiteById(siteId)
	site.Title = title
	site.Url = url
	err := site.Update()
	if err != nil {
		data.code = 4
		data.message = "update site error: " + err.Error()
	} else {
		data.code = 0
		data.message = "ok"
	}
	this.Data["json"] = data.Json()
	this.ServeJSON()
}
Ejemplo n.º 3
0
func newSite(title, url, grpId string) (models.Site, error) {
	grp := models.GetGroupById(bson.ObjectIdHex(grpId))
	//	if grp.Id == nil {
	//		return models.Site{}, errors.New("grp not exist")
	//	}
	if grp.HasSite(url) {
		return models.Site{}, errors.New("grp already has url")
	}
	return grp.AddSite(title, url)
	// eturn models.Site{}, nil
}
Ejemplo n.º 4
0
Archivo: site.go Proyecto: wuxu92/snp
// Delete uri: /api/site/delete/:id?grp=gid&p=password delete
// @todo add grp and password
func (this *SiteController) Delete() {
	sid := this.Ctx.Input.Param(":id")
	action := this.Ctx.Input.Param(":action")
	gid := this.GetString("grp")

	data := ResObj{}
	utils.GetConsole().Info("a: %s, site: %s, grp: %s -- %s", action, sid, gid)
	if !bson.IsObjectIdHex(sid) || !bson.IsObjectIdHex(gid) {
		data.code = 1
		data.message = "one of site or grp is illeagal"
		this.Data["json"] = data.Json()
		this.ServeJSON()
		return
	}
	siteId := bson.ObjectIdHex(sid)
	grpId := bson.ObjectIdHex(gid)

	if ge, se := !models.IsGroupExist(grpId), !models.IsSiteExist(siteId); se || ge {
		data.code = 2
		if !se {
			data.message = "site not exist"
		} else if !ge {
			data.message = "group not exist"
		} else {
			data.message = "group and site not exist"
		}
		this.json(data)
		return
	}

	grp := models.GetGroupById(grpId)
	if !grp.HasSiteId(siteId) {
		//data.code = 3
		//data.message = "grp don't has this site"
		this.json(ResObj{3, "grp don't has this site", nil})
		return
	}

	// remove site from grp
	grp.RemoveSite(siteId)
	// save to db
	grp.Update()
	this.json(ResObj{0, "ok", nil})
}