func (s *S) TestObjectIdJSONMarshaling(c *C) { id := bson.ObjectIdHex("4d88e15b60f486e428412dc9") v := jsonType{Id: &id} data, err := json.Marshal(&v) c.Assert(err, IsNil) c.Assert(string(data), Equals, `{"Id":"4d88e15b60f486e428412dc9"}`) }
func (s *S) TestObjectIdJSONUnmarshaling(c *C) { data := []byte(`{"Id":"4d88e15b60f486e428412dc9"}`) v := jsonType{} err := json.Unmarshal(data, &v) c.Assert(err, IsNil) c.Assert(*v.Id, Equals, bson.ObjectIdHex("4d88e15b60f486e428412dc9")) }
func (post *PostModel) Publish(postId string) { result := post.Get(postId) result.Status = 1 err := post.c.Update(bson.M{"_id": bson.ObjectIdHex(postId)}, result) if err != nil { panic(err) } }
func (post *PostModel) Get(postId string) *Post { var result *Post err := post.c.Find(bson.M{"_id": bson.ObjectIdHex(postId)}).One(&result) if err != nil { panic(err) } return result }
func (post *PostModel) Update(title string, content string, postId string) { result := post.Get(postId) result.Title = title result.Content = content result.Modified = time.LocalTime().Seconds() err := post.c.Update(bson.M{"_id": bson.ObjectIdHex(postId)}, result) if err != nil { panic(err) } }
func (page *PageModel) Get(id string) *Page { var result *Page err := page.c.Find(bson.M{"_id": bson.ObjectIdHex(id)}).One(&result) if err != nil && err != mgo.NotFound { panic(err) } else if err == mgo.NotFound { return nil } return result }
func (post *PostModel) Update(title string, content string, status string, postId string) { result := post.Get(postId) result.Title = title result.Content = content result.Modified = time.Now().Unix() result.Status, _ = strconv.ParseUint(status, 0, 64) err := post.c.Update(bson.M{"_id": bson.ObjectIdHex(postId)}, result) if err != nil { panic(err) } }
func (post *PostModel) InsertComment(postId string, content string, author string) { result := post.Get(postId) if author == "" { author = "Anonymous" } // Generate ID t := time.Now() var h hash.Hash = sha1.New() h.Write([]byte(author + strconv.FormatInt(t.Unix(), 10))) id := hex.EncodeToString(h.Sum(nil)) comment := Comment{id, content, author, t.Unix()} result.Comments = append(result.Comments, comment) err := post.c.Update(bson.M{"_id": bson.ObjectIdHex(postId)}, result) if err != nil { panic(err) } }
func (page *PageModel) Update(title string, content string, id string) { result := page.Get(id) result.Title = title slug := toAscii(title) tmp := page.GetBySlug(slug) if tmp != nil { if tmp.Id != result.Id { slug += "-2" } } result.Slug = slug result.Content = content result.Modified = time.Now().Unix() err := page.c.Update(bson.M{"_id": bson.ObjectIdHex(id)}, result) if err != nil { panic(err) } }
func (post *PostModel) DeleteComment(postId string, id string) { result := post.Get(postId) // Copy everything from the old one and skip those that we want to delete newComments := make([]Comment, 0) for _, comment := range result.Comments { if comment.Id != id { newComments = append(newComments, comment) } } result.Comments = newComments // Save result err := post.c.Update(bson.M{"_id": bson.ObjectIdHex(postId)}, result) if err != nil { panic(err) } }
func (post *PostModel) Delete(postId string) { err := post.c.Remove(bson.M{"_id": bson.ObjectIdHex(postId)}) if err != nil { panic(err) } }
func (s *S) TestObjectIdHex(c *C) { id := bson.ObjectIdHex("4d88e15b60f486e428412dc9") c.Assert(id.String(), Equals, `ObjectIdHex("4d88e15b60f486e428412dc9")`) c.Assert(id.Hex(), Equals, "4d88e15b60f486e428412dc9") }
} // -------------------------------------------------------------------------- // ObjectId parts extraction tests. type objectIdParts struct { id bson.ObjectId timestamp int32 machine []byte pid uint16 counter int32 } var objectIds = []objectIdParts{ objectIdParts{ bson.ObjectIdHex("4d88e15b60f486e428412dc9"), 1300816219, []byte{0x60, 0xf4, 0x86}, 0xe428, 4271561, }, objectIdParts{ bson.ObjectIdHex("000000000000000000000000"), 0, []byte{0x00, 0x00, 0x00}, 0x0000, 0, }, objectIdParts{ bson.ObjectIdHex("00000000aabbccddee000001"), 0,
func (page *PageModel) Delete(id string) { err := page.c.Remove(bson.M{"_id": bson.ObjectIdHex(id)}) if err != nil { panic(err) } }