當前位置: 首頁>>代碼示例>>Golang>>正文


Golang bson.IsObjectIdHex函數代碼示例

本文整理匯總了Golang中labix/org/v2/mgo/bson.IsObjectIdHex函數的典型用法代碼示例。如果您正苦於以下問題:Golang IsObjectIdHex函數的具體用法?Golang IsObjectIdHex怎麽用?Golang IsObjectIdHex使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。


在下文中一共展示了IsObjectIdHex函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。

示例1: recordPagingFunc

func recordPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	record := &Record{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(record); err != nil {
			return nil, err
		}
		query = bson.M{
			"starttime": bson.M{
				"$gte": record.PubTime,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(record); err != nil {
			return nil, err
		}
		query = bson.M{
			"starttime": bson.M{
				"$lte": record.PubTime,
			},
		}
	}

	return
}
開發者ID:shevilangle,項目名稱:sports,代碼行數:25,代碼來源:account.go

示例2: txPagingFunc

func txPagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	tx := &Tx{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(tx); err != nil {
			return nil, err
		}
		query = bson.M{
			"time": bson.M{
				"$gte": tx.Time,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(tx); err != nil {
			return nil, err
		}
		query = bson.M{
			"time": bson.M{
				"$lte": tx.Time,
			},
		}
	}

	return
}
開發者ID:shevilangle,項目名稱:sports,代碼行數:25,代碼來源:account.go

示例3: articlePagingFunc

func articlePagingFunc(c *mgo.Collection, first, last string, args ...interface{}) (query bson.M, err error) {
	article := &Article{}

	if bson.IsObjectIdHex(first) {
		if err := c.FindId(bson.ObjectIdHex(first)).One(article); err != nil {
			return nil, err
		}
		query = bson.M{
			"pub_time": bson.M{
				"$gte": article.PubTime,
			},
		}
	} else if bson.IsObjectIdHex(last) {
		if err := c.FindId(bson.ObjectIdHex(last)).One(article); err != nil {
			return nil, err
		}
		query = bson.M{
			"pub_time": bson.M{
				"$lte": article.PubTime,
			},
		}
	}

	return
}
開發者ID:shevilangle,項目名稱:sports,代碼行數:25,代碼來源:article.go

示例4: getId

// getId returns bson.ObjectId form given id.
// id must be a valid bson.ObjectId or a valid ObjectIdHex
func getId(id string) (bson.ObjectId, error) {
	if !bson.IsObjectIdHex(id) {
		return "", authmodel.ErrInvalidId
	}

	return bson.ObjectIdHex(id), nil
}
開發者ID:kidstuff,項目名稱:auth-mongo-mngr,代碼行數:9,代碼來源:util.go

示例5: ExternalPhoto

func ExternalPhoto(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	id := req.URL.Query().Get(":id")
	photoId := req.URL.Query().Get(":photo")
	if !bson.IsObjectIdHex(id) || (photoId != "" && !bson.IsObjectIdHex(photoId)) {
		return perform_status(w, req, http.StatusForbidden)
	}
	muxName := "photos"
	if req.URL.Query().Get(":kind") == "c" {
		muxName = "view_contest"
	}
	return T("ajax_wrapper.html").Execute(w, map[string]interface{}{
		"ajaxurl": reverse(muxName, "id", id, "photo", photoId),
		"ctx":     ctx,
	})
	return nil
}
開發者ID:vichetuc,項目名稱:lov3lyme,代碼行數:16,代碼來源:photos.go

示例6: GetPhotoVotes

func GetPhotoVotes(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	id := req.URL.Query().Get(":id")
	if !bson.IsObjectIdHex(id) {
		return perform_status(w, req, http.StatusForbidden)
	}
	match := bson.M{"photo": bson.ObjectIdHex(id)}
	if f, ok := ctx.Session.Values["filter"]; ok {
		f.(*models.Filter).AddQuery(match)
	}
	var result bson.M
	pipe := ctx.C(V).Pipe([]bson.M{
		{"$match": match},
		{"$group": bson.M{
			"_id":   "$photo",
			"avg":   bson.M{"$avg": "$score"},
			"count": bson.M{"$sum": 1},
			"user":  bson.M{"$addToSet": "$photouser"},
		}},
		{"$unwind": "$user"},
	})
	pipe.One(&result)
	if result["user"] != nil && result["user"].(bson.ObjectId) != ctx.User.Id {
		return perform_status(w, req, http.StatusForbidden)
	}
	w.Header().Set("Content-Type", "application/json")
	fmt.Fprintf(w, `{"avg": %.1f, "count": %d}`, result["avg"], result["count"])
	return nil
}
開發者ID:vichetuc,項目名稱:lov3lyme,代碼行數:28,代碼來源:votes.go

示例7: Photos

func Photos(w http.ResponseWriter, req *http.Request, ctx *models.Context) error {
	id := req.URL.Query().Get(":id")
	if !bson.IsObjectIdHex(id) {
		return perform_status(w, req, http.StatusNotFound)
	}
	var photos []*models.Photo
	if err := ctx.C(P).Find(bson.M{"user": bson.ObjectIdHex(id), "active": true}).All(&photos); err != nil {
		return perform_status(w, req, http.StatusNotFound)
	}
	user := new(models.User)
	if err := ctx.C("users").FindId(bson.ObjectIdHex(id)).One(user); err != nil {
		return perform_status(w, req, http.StatusNotFound)
	}
	// find the index of the photo
	photoId := req.URL.Query().Get(":photo")
	ctx.Data["index"] = 0
	var pIds []bson.ObjectId
	for i, p := range photos {
		if p.Id.Hex() == photoId {
			ctx.Data["index"] = i
		}
		pIds = append(pIds, p.Id)
	}

	return AJAX("galleria.html").Execute(w, map[string]interface{}{
		"photos": photos,
		"user":   user,
		"hash":   models.GenUUID(),
		"ctx":    ctx,
	})
}
開發者ID:vichetuc,項目名稱:lov3lyme,代碼行數:31,代碼來源:photos.go

示例8: GetTicket

func GetTicket(ctx *auth.AuthContext, rw http.ResponseWriter, req *http.Request) (int, error) {
	sid := mux.Vars(req)["ticket_id"]
	if !bson.IsObjectIdHex(sid) {
		return http.StatusBadRequest, errors.New("Invalid id")
	}

	db, ok := ctx.Value(DBKey).(*mgo.Database)
	if !ok {
		ctx.Logs.Errorf("Cannot access database")
		return http.StatusInternalServerError, errors.New("Cannot access database")
	}

	t := Ticket{}
	err := db.C("tickets").FindId(bson.ObjectIdHex(sid)).One(&t)
	if err != nil {
		if err == mgo.ErrNotFound {
			return http.StatusNotFound, err
		}

		return http.StatusInternalServerError, err
	}

	json.NewEncoder(rw).Encode(&t)
	return http.StatusOK, nil
}
開發者ID:kidstuff,項目名稱:auth-example,代碼行數:25,代碼來源:tickets.go

示例9: GetResource

// Retrieve an instance of the resource given the id. Assumes the resource name matches the collection name
func GetResource(p martini.Params, r render.Render, db *mgo.Database) {
	resource := p["resource"]
	id := p["id"]

	// TODO use reflection
	var result *interface{}
	if !bson.IsObjectIdHex(id) {
		r.JSON(400, map[string]string{"error": "Invalid id"})
		return
	}

	err := db.C(resource).Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result)
	if err != nil {
		var status int
		if err == mgo.ErrNotFound {
			status = 404
		} else {
			status = 500
		}

		r.JSON(status, map[string]string{"error": err.Error()})
		return
	}
	r.JSON(200, result)
}
開發者ID:h12w,項目名稱:golang-rest-server,代碼行數:26,代碼來源:handlers.go

示例10: deleteSiteHandler

// URL: /site/{siteId}/delete
// 刪除站點,提交者自己或者管理員可以刪除
func deleteSiteHandler(handler Handler) {
	user, _ := currentUser(handler)

	siteId := mux.Vars(handler.Request)["siteId"]

	if !bson.IsObjectIdHex(siteId) {
		http.NotFound(handler.ResponseWriter, handler.Request)
		return
	}

	var site Site
	c := handler.DB.C(CONTENTS)

	err := c.Find(bson.M{"_id": bson.ObjectIdHex(siteId)}).One(&site)

	if err != nil {
		message(handler, "錯誤的連接", "錯誤的連接", "error")
		return
	}

	if !site.CanEdit(user.Username, handler.DB) {
		message(handler, "沒有權限", "你沒有權限可以刪除站點", "error")
		return
	}

	c.Remove(bson.M{"_id": site.Id_})

	http.Redirect(handler.ResponseWriter, handler.Request, "/sites", http.StatusFound)
}
開發者ID:hello-kukoo,項目名稱:gopher,代碼行數:31,代碼來源:site.go

示例11: LoginCheckHandler

// a handler that checks that the current session id is still active
func LoginCheckHandler(cmd *CmdMessage) error {
	sessId, ok := cmd.Data["session_id"].(string)
	if !ok || !bson.IsObjectIdHex(sessId) {
		return errors.New("Invalid session id supplied")
	}
	data := make(map[string]interface{})
	ClearOldSessions()

	if cmd.Conn.owner.IsUser() {
		id, err := AuthFromSession(sessId)

		if err == nil {
			// everything is ok, logging in user
			err = cmd.Conn.owner.Authenticate(id)
			if err != nil {
				data["status"] = "UNAUTHORIZED"
			} else {
				// sending back the success message
				data["status"] = "OK"
			}
		} else {
			// sending back the failure message
			data["status"] = "UNAUTHORIZED"
		}

		DispatchMessage("loginCheck", data, cmd.Conn)
		return err
	}

	return errors.New("This handler is only available for users")
}
開發者ID:GU-2013-TEAM-M,項目名稱:eos,代碼行數:32,代碼來源:login_check_handler.go

示例12: thumbnailImageHandlerFunc

func thumbnailImageHandlerFunc(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	urlVars := mux.Vars(r)
	documentIDHex := urlVars["documentIDHex"]
	if !bson.IsObjectIdHex(documentIDHex) {
		http.NotFound(w, r)
		return
	}
	documentID := bson.ObjectIdHex(documentIDHex)

	fileName := fmt.Sprintf("document-thumbnails/%s.png", documentID.Hex())
	file, err := gridFS.Open(fileName)
	if err != nil {
		if err == mgo.ErrNotFound {
			http.NotFound(w, r)
			return
		}
		log.Printf("error looking up files in gridFS (%s): %s\n", fileName, err)
		http.Error(w, "error", http.StatusInternalServerError)
		return
	}
	defer file.Close()

	w.Header().Set("Content-Type", "image/png")
	_, err = io.Copy(w, file)
	if err != nil {
		log.Printf("error writing png file (%s) to http client: %s\n", fileName, err)
		return
	}
	// all done :)
}
開發者ID:nulpunt,項目名稱:nulpunt,代碼行數:31,代碼來源:http_document.go

示例13: cropImageHandlerFunc

func cropImageHandlerFunc(w http.ResponseWriter, r *http.Request) {
	defer r.Body.Close()
	urlVars := mux.Vars(r)
	cropIDHex := urlVars["cropIDHex"]
	if !bson.IsObjectIdHex(cropIDHex) {
		http.NotFound(w, r)
		return
	}
	cropID := bson.ObjectIdHex(cropIDHex)

	file, err := gridFS.OpenId(cropID)
	if err != nil {
		if err == mgo.ErrNotFound {
			http.NotFound(w, r)
			return
		}
		log.Printf("error looking up files in gridFS (%s): %s\n", cropIDHex, err)
		http.Error(w, "error", http.StatusInternalServerError)
		return
	}
	defer file.Close()

	w.Header().Set("Content-Type", file.ContentType())
	_, err = io.Copy(w, file)
	if err != nil {
		log.Printf("error writing png file (%s) to http client: %s\n", cropIDHex, err)
		return
	}
	// all done :)
}
開發者ID:nulpunt,項目名稱:nulpunt,代碼行數:30,代碼來源:http_document.go

示例14: delChatHandler

func delChatHandler(w http.ResponseWriter, redis *models.RedisLogger, form delChatForm) {

	msg := &models.Message{}

	if bson.IsObjectIdHex(form.Id) {
		msg.Id = bson.ObjectIdHex(form.Id)
		if err := msg.RemoveId(); err != nil {
			writeResponse(w, err)
		} else {
			writeResponse(w, map[string]int{"count": 1})
		}
		return
	}

	var start, end time.Time
	if form.FromTime == 0 {
		start = time.Unix(0, 0)
	} else {
		start = time.Unix(form.FromTime, 0)
	}
	if form.ToTime == 0 {
		end = time.Now()
	} else {
		end = time.Unix(form.ToTime, 0)
	}

	count, err := msg.Delete(form.From, form.To, start, end)
	if err != nil {
		writeResponse(w, err)
		return
	}
	writeResponse(w, map[string]int{"count": count})
}
開發者ID:shevilangle,項目名稱:sports,代碼行數:33,代碼來源:chat.go

示例15: deletePackageHandler

// URL: /p/{packageId}/delete
// 刪除第三方包
func deletePackageHandler(handler Handler) {
	vars := mux.Vars(handler.Request)
	packageId := vars["packageId"]

	if !bson.IsObjectIdHex(packageId) {
		http.NotFound(handler.ResponseWriter, handler.Request)
		return
	}

	c := DB.C(CONTENTS)

	package_ := Package{}
	err := c.Find(bson.M{"_id": bson.ObjectIdHex(packageId), "content.type": TypePackage}).One(&package_)

	if err != nil {
		return
	}

	c.Remove(bson.M{"_id": bson.ObjectIdHex(packageId)})

	// 修改分類下的數量
	c = DB.C(PACKAGE_CATEGORIES)
	c.Update(bson.M{"_id": package_.CategoryId}, bson.M{"$inc": bson.M{"packagecount": -1}})

	http.Redirect(handler.ResponseWriter, handler.Request, "/packages", http.StatusFound)
}
開發者ID:no2key,項目名稱:gopher,代碼行數:28,代碼來源:package.go


注:本文中的labix/org/v2/mgo/bson.IsObjectIdHex函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。