本文整理汇总了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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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
}
示例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,
})
}
示例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
}
示例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)
}
示例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)
}
示例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")
}
示例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 :)
}
示例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 :)
}
示例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})
}
示例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)
}