// 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() }
func (this *PkgController) Get() { action := this.Ctx.Input.Param(":action") id := this.Ctx.Input.Param(":id") utils.GetLogger().Info("model: pkg, action: %s, id: %s", action, id) data := ResObj{} switch action { case "get": if id == "d" { id = "default" } utils.GetConsole().Info("-getting pkg: %s", id) data.code = 0 data.message = "" data.data = models.GetPkgFullInfo(id) this.Data["json"] = data.Json() case "new": case "fork": utils.GetConsole().Info("forking; %s", id) if id == "" { this.Data["json"] = false } else { name := this.GetString("name", "pkg-"+time.Now().Format(time.Stamp)) pkg, err := forkPkg(id, name) if err != nil { data.code = -1 data.message = err.Error() } else { data.code = 0 data.message = pkg.Name } } } this.Ctx.ResponseWriter.Header().Set("Content-Type", "application/json; charset=utf8") this.ServeJSON() }
// 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}) }
// delete group uri : /api/grp/delete/:id?p=password&pkg=pkgName with delete request // todo check password func (this *GrpController) Delete() { gid := this.Ctx.Input.Param(":id") action := this.Ctx.Input.Param(":action") password := this.GetString("p") pkgName := this.GetString("pkg") data := ResObj{} // log request utils.GetConsole().Info("a: %s, grp: %s, p: %s", action, gid, password) pkg, err := models.GetPkgByName(pkgName) if err != nil { data.SetCode(CODE_NO_SUCH_PKG) this.Json(data) return } if !bson.IsObjectIdHex(gid) { data.SetCode(CODE_MGO_BAD_ID) this.Json(data) return } // todo check password if !pkg.CheckPassword(password) { data.SetCode(CODE_PASSWORD_ERR) this.Json(data) return } // do delete process // just remove id from pkg's grp list deleted := pkg.RemoveGroup(gid) if !deleted { data.SetCode(CODE_NO_SUCH_GRP) } else { data.SetCode(CODE_OK) } this.Json(data) }