本文整理汇总了Golang中memex/rpc.WriteJson函数的典型用法代码示例。如果您正苦于以下问题:Golang WriteJson函数的具体用法?Golang WriteJson怎么用?Golang WriteJson使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了WriteJson函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: DoComment
/// add comment
func DoComment(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
args := bson.M{}
if err = rpc.ReadJson(r, &args); err != nil {
rpc.WriteJson(r, w, nil, errors.New("Invalid JSON!"))
}
c := db.C("Post")
q := c.FindId(vars["postId"])
var post Post
err = q.One(&post)
if err == nil {
aCmt := Comment{User: r.Header.Get("User"), Date: time.Now().String()[:19], Text: args["Text"].(string)}
if len(post.Comments) == 0 {
post.Comments = []Comment{aCmt}
} else {
post.Comments = append(post.Comments, aCmt)
}
err = c.UpdateId(post.Id, post)
}
rpc.WriteJson(r, w, "OK", err)
}
示例2: _approveOrReject
func _approveOrReject(approve bool, w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
// wf хүсэлтийг олох
c := db.C(colWfAction)
q := c.FindId(vars["id"])
var reqObj bson.M
if err = q.One(&reqObj); err != nil {
rpc.WriteJson(r, w, nil, errors.New("Буруу хүсэлт !"))
return
}
if reqObj["Active"].(bool) == false {
rpc.WriteJson(r, w, nil, errors.New("Өмнө нь ашиглагдсан холбоос байна !"))
return
}
state := "зөвшөөрсөн"
msg := "Чөлөө зөвшөөрөгдлөө"
if !approve {
state = "татгалзсан"
msg = "Чөлөөг татгалзлаа"
}
c1 := db.C("Leave")
if err = c1.UpdateId(reqObj["LeaveId"], bson.M{"$set": bson.M{"State": state}}); err == nil {
err = c.UpdateId(vars["id"], bson.M{"$set": bson.M{"Active": false}})
}
rpc.WriteJson(r, w, msg, err)
}
示例3: SaveFeedback
func SaveFeedback(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
var obj SurveyFeedback
if err = rpc.ReadJson(r, &obj); err != nil {
rpc.WriteJson(r, w, nil, err)
return
}
c := db.C("SurveyFeedback")
// асуултын тоогоор мөр үүсгэж бичих
for _, ans := range obj.Answers {
a := bson.M{}
a["_id"] = bson.NewObjectId().Hex()
a["User"] = r.Header.Get("User")
a["Survey"] = vars["surveyId"]
a["Question"] = ans["Question"]
a["Answer"] = ans["Answer"]
c.Insert(a)
}
// OK - амжилттай
// DUP - давхардсан
// LIMIT - хариулах хязгаарт хүрсэн
rpc.WriteJson(r, w, "OK", err)
}
示例4: RegAtt
// ирц бүртгэх функц
func RegAtt(w http.ResponseWriter, r *http.Request) {
var err error
obj := bson.M{}
if err = rpc.ReadJson(r, &obj); err != nil {
rpc.WriteJson(r, w, nil, err)
return
}
obj["_id"] = bson.NewObjectId().Hex()
obj["RemoteAddr"] = r.RemoteAddr
// TODO: орсон, гарсан эсэхийг ялгаж таних
// Map EnrollNumber into Employee ID, Name
empC := db.C("Employee")
q := empC.Find(bson.M{"CardNo": obj["EnrollNumber"]})
var emp bson.M
if err = q.One(&emp); err == nil {
obj["Employee"] = bson.M{"_id": emp["_id"], "Name": emp["Name"]}
}
// Баазад оруулах
c := db.C("Attendance")
err = c.Insert(obj)
rpc.WriteJson(r, w, obj["_id"], err)
}
示例5: UnfollowGroup
func UnfollowGroup(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
c := db.C("PlusGroup")
q := c.FindId(vars["groupId"])
var pg PlusGroup
err = q.One(&pg)
if err == nil {
if len(pg.Followers) > 0 {
for i := 0; i < len(pg.Followers); i++ {
if pg.Followers[i] == r.Header.Get("User") {
// remove all occurences
pg.Followers = append(pg.Followers[:i], pg.Followers[i+1:]...)
}
}
err = c.UpdateId(pg.Id, pg)
}
}
rpc.WriteJson(r, w, "OK", nil)
}
示例6: MyManager
func MyManager(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
mgr := ""
// ажилтныг хайж олох
empCol := db.C("Employee")
q := empCol.FindId(vars["empId"])
var emp bson.M
err = q.One(&emp)
if err == nil {
// нэгжийг хайж олох
deptCol := db.C("Dept")
q1 := deptCol.FindId(emp["Dept"].(string))
var dept bson.M
err = q1.One(&dept)
if err == nil {
mgr = dept["Manager"].(string)
}
}
rpc.WriteJson(r, w, mgr, err)
}
示例7: UnSubscribe
func UnSubscribe(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
c := db.C("Event")
q := c.FindId(vars["eventId"])
var ev Event
err = q.One(&ev)
if err == nil {
if len(ev.Participants) > 0 {
for i := 0; i < len(ev.Participants); i++ {
if ev.Participants[i].Id == r.Header.Get("User") {
// remove all occurences
ev.Participants = append(ev.Participants[:i], ev.Participants[i+1:]...)
}
}
err = c.UpdateId(ev.Id, ev)
}
}
rpc.WriteJson(r, w, "OK", nil)
}
示例8: Report
/// Тайлан боловсруулах
func Report(w http.ResponseWriter, r *http.Request) {
var err error
args := JsArgs{}
rpc.ReadJson(r, &args)
// call stored js
var runResult bson.M
var resp []bson.M
err = _db.Run(bson.D{
{"eval", args.Src},
{"args", []bson.M{args.Params}},
{"nolock", true},
},
&runResult)
if err == nil {
// боловсруулалтын дараах үр дүнг уншиж буцаах
c := _db.C(runResult["retval"].(string))
q := c.Find(bson.M{})
if args.Params["Sort"] != nil {
q.Sort([]string{args.Params["Sort"].(string)}...)
}
err = q.All(&resp)
}
rpc.WriteJson(r, w, resp, err)
}
示例9: Delete
// Id-аар устгах
func Delete(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
c := _db.C(vars["collection"])
err = c.Remove(bson.M{"_id": vars["id"]})
rpc.WriteJson(r, w, vars["id"], err)
}
示例10: ExecJS1
func ExecJS1(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
params := bson.M{}
rpc.ReadJson(r, ¶ms)
err, runResult := ExecJS(vars["js"], params)
rpc.WriteJson(r, w, runResult, err)
}
示例11: PostCount
// мэдээ тоолох
// TODO: replace with db.MapReduce
func PostCount(w http.ResponseWriter, r *http.Request) {
var err error
args := bson.M{}
if err = rpc.ReadJson(r, &args); err != nil {
rpc.WriteJson(r, w, nil, errors.New("Invalid JSON!"))
}
c := db.C("Post")
mr := &mgo.MapReduce{
Map: "function() { if (!this.Type) { emit('news', 1) } else { emit(this.Type, 1) } }",
Reduce: "function(key, values) { return Array.sum(values); }",
}
var result []bson.M
_, err = c.Find(nil).MapReduce(mr, &result)
rpc.WriteJson(r, w, result, err)
}
示例12: RegApp
// Applicant бүртгэх
func RegApp(w http.ResponseWriter, r *http.Request) {
var err error
//vars := mux.Vars(r)
obj := bson.M{}
if err = rpc.ReadJson(r, &obj); err != nil {
rpc.WriteJson(r, w, nil, err)
return
}
c := db.C("Applicant")
if obj["_id"] == nil {
obj["_id"] = bson.NewObjectId().Hex()
}
obj["RemoteAddr"] = r.RemoteAddr
err = c.Insert(obj)
rpc.WriteJson(r, w, "OK", err)
}
示例13: FindId
// Id-аар хайх
func FindId(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
c := _db.C(vars["collection"])
q := c.FindId(vars["id"])
var resp bson.M
err = q.One(&resp)
rpc.WriteJson(r, w, resp, err)
}
示例14: FindOne
/// Нэгийг хайх
func FindOne(w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
args := DbArgs{}
if err = rpc.ReadJson(r, &args); err != nil {
rpc.WriteJson(r, w, nil, err)
return
}
c := _db.C(vars["collection"])
q := c.Find(args.Query)
// select
if args.Select != nil {
q.Select(args.Select)
}
var resp bson.M
err = q.One(&resp)
rpc.WriteJson(r, w, resp, err)
}
示例15: _save
func _save(op string, w http.ResponseWriter, r *http.Request) {
var err error
vars := mux.Vars(r)
obj := bson.M{}
if err = rpc.ReadJson(r, &obj); err != nil {
rpc.WriteJson(r, w, nil, err)
return
}
obj["updated"] = time.Now().String()[:19]
obj["updatedBy"] = r.Header.Get("User")
onSave(vars["collection"], obj, w, r)
// TODO: foreign key талбарууд шинэчилэх
// tags шинэчилэх
UpdateTags(obj)
c := _db.C(vars["collection"])
if op == "save" {
if obj["_id"] != nil {
_, err = c.UpsertId(obj["_id"], obj)
} else {
obj["_id"] = bson.NewObjectId().Hex()
err = c.Insert(obj)
}
} else if op == "insert" {
obj["_id"] = bson.NewObjectId().Hex()
err = c.Insert(obj)
} else if op == "update" {
//delete(obj, "_id")
err = c.UpdateId(obj["_id"], obj)
}
rpc.WriteJson(r, w, obj["_id"], err)
}