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) }
/// Тайлан боловсруулах 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) }
/// 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) }
// ирц бүртгэх функц 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) }
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) }
func Aggregate(w http.ResponseWriter, r *http.Request) { var err error var resp []bson.M args := AggregateArgs{} err = rpc.ReadJson(r, &args) if err == nil { c := _db.C(args.Collection) pipe := c.Pipe(args.Pipeline) err = pipe.All(&resp) } rpc.WriteJson(r, w, resp, err) }
// мэдээ тоолох // 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) }
/// Хайх func Find(w http.ResponseWriter, r *http.Request) { var err error // параметр vars := mux.Vars(r) args := DbArgs{} rpc.ReadJson(r, &args) c := _db.C(vars["collection"]) if args.Query == nil { args.Query = bson.M{} } onFind(args.Query, w, r) q := c.Find(args.Query) // select if args.Select != nil { q.Select(args.Select) } // skip if args.Skip > 0 { q.Skip(args.Skip) } // limit if args.Limit > 0 { q.Limit(args.Limit) } // sort if len(args.Sort) > 0 { q.Sort(args.Sort...) } var resp []bson.M err = q.All(&resp) //log.Printf("db.Find(%v) алдаа: %v", args, err) rpc.WriteJson(r, w, resp, err) }
func MapReduce(w http.ResponseWriter, r *http.Request) { var err error var resp []bson.M args := MapReduceArgs{} err = rpc.ReadJson(r, &args) if err == nil { job := &mgo.MapReduce{ Map: args.Map, Reduce: args.Reduce, } c := _db.C(args.Collection) _, err = c.Find(args.Query).MapReduce(job, &resp) } rpc.WriteJson(r, w, resp, err) }
// 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) }
/// Нэгийг хайх 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) }
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) }
// Бичлэгийн тоо func Count(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) var n int var err error c := _db.C(vars["collection"]) q := bson.M{} if r.Method == "GET" { // n, err = c.Find(q).Count() } else { args := DbArgs{} rpc.ReadJson(r, &args) q = args.Query } // тоолох үйлдэл дээр мөн дүрийн filter хамаатай onFind(q, w, r) n, err = c.Find(q).Count() rpc.WriteJson(r, w, n, err) }