func GetBlogsGroup(groupType string, name string) (blogs Blogs) { switch groupType { case "tag": err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find(bson.M{"tags": bson.M{"$all": []string{name}}}).Sort("-created").All(&blogs) }) if err != nil { log.Error("Find error, ", err) return } log.Debug("get tag, ", blogs) return case "category": err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find(bson.M{"category": bson.M{"$all": []string{name}}}).Sort("-created").All(&blogs) }) if err != nil { log.Error("Find error, ", err) return } log.Debug("get category, ", blogs) return default: log.Error("GetBlogsByTag type error") return } return }
func (b *Blog) UpdateID(id string) (err error) { if !bson.IsObjectIdHex(id) { return E_NOT_OBJ_ID } bid := bson.ObjectIdHex(id) old := new(Blog) err = mongo.DB().FindId(mongo.C_BLOGS, bid, old) if err != nil { return } b.ID = old.ID b.Created = old.Created b.Author = old.Author b.Updated = time.Now() if len(b.Source) > 0 { b.Content = b.Source //markdown.Trans2html([]byte(b.Source)) } err = mongo.DB().UpdateId(mongo.C_BLOGS, b.ID, b) if err != nil { return err } err = b.removeAllGroup() if err != nil { return err } err = b.insertGroup() return }
// 插入文章的tag信息到数据库 func (b *Blog) insertGroup() error { for _, tag := range b.Tags { if tag == "" { continue } err := mongo.DB().Upsert(mongo.C_TAGS, bson.M{"name": tag}, bson.M{"$push": bson.M{"blogs": b.ID}}) if err != nil { return fmt.Errorf("%s\n", err) } } for _, tag := range b.Category { if tag == "" { continue } err := mongo.DB().Upsert(mongo.C_CATEGORY, bson.M{"name": tag}, bson.M{"$push": bson.M{"blogs": b.ID}}) if err != nil { return fmt.Errorf("%s\n", err) } } err := mongo.DB().Upsert(mongo.C_MONTH, bson.M{"name": fmt.Sprintf("%d-%d", b.Created.Year(), b.Created.Month())}, bson.M{"$push": bson.M{"blogs": b.ID}}) return err }
// 移除文章所有标签 func (b *Blog) removeAllGroup() (err error) { err = mongo.DB().UpdateAll(mongo.C_TAGS, nil, bson.M{"$pull": bson.M{"blogs": b.ID}}) if err != nil { return } err = mongo.DB().UpdateAll(mongo.C_CATEGORY, nil, bson.M{"$pull": bson.M{"blogs": b.ID}}) if err != nil { return } err = mongo.DB().UpdateAll(mongo.C_MONTH, nil, bson.M{"$pull": bson.M{"blogs": b.ID}}) return }
func GetAllCategories() (tags Tags) { err := mongo.DB().All(mongo.C_CATEGORY, nil, &tags) if err != nil { log.Error("Find error, ", err) } tags.Sort() return }
func GetAllTags() (tags Tags) { err := mongo.DB().All(mongo.C_TAGS, nil, &tags) if err != nil { log.Error("Find error, ", err) } tags.Sort() return }
func (b *Blog) Delete() (err error) { err = mongo.DB().RemoveId(mongo.C_BLOGS, b.ID) if err != nil { return err } err = b.removeAllGroup() return err }
func GetAllBlogs() (blogs Blogs) { err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find(nil).Sort("-created").All(&blogs) }) if err != nil { log.Error("Find error, ", err) } return }
func GetLatestBlogs() (blogs Blogs) { err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find(nil).Sort("-created").Limit(5).All(&blogs) }) if err != nil { log.Error("find latest blog error, ", err) return } return }
func GetMonthBlogs(year, month int) (blogs Blogs) { begin := time.Date(year, time.Month(month), 0, 0, 0, 0, 0, time.Local) end := time.Date(year, time.Month(month+1), 0, 0, 0, 0, 0, time.Local) err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find(bson.M{"created": bson.M{"$gte": begin, "$lt": end}}).Sort("-created").All(&blogs) }) if err != nil { log.Error("find month error, ", err) return } log.Debug("get month, ", blogs) return }
func GetBlogID(id string) (b *Blog) { if !bson.IsObjectIdHex(id) { return } b = new(Blog) err := mongo.DB().FindId(mongo.C_BLOGS, bson.ObjectIdHex(id), b) if err != nil { log.Error("get blog error, ", err) } // log.Debugf("find blog , %#v", b) return }
func SearchBlogs(keyword string) (blogs Blogs) { err := mongo.DB().WithC(mongo.C_BLOGS, func(db *mgo.Collection) error { return db.Find( bson.M{ "$or": []bson.M{bson.M{"source": bson.M{"$regex": keyword}}, bson.M{"title": bson.M{"$regex": keyword}}, bson.M{"summary": bson.M{"$regex": keyword}}}, }).Sort("-created").All(&blogs) }) if err != nil { log.Error("find month error, ", err) return } return }
func (b *Blog) Insert() (err error) { b.ID = bson.NewObjectId() b.Created = time.Now() b.Updated = time.Now() b.Author = "风险观察君" if len(b.Source) > 0 { b.Content = b.Source //markdown.Trans2html([]byte(b.Source)) } err = mongo.DB().Insert(mongo.C_BLOGS, b) if err != nil { return err } err = b.insertGroup() return }
func GetAllMonth() []string { var m []string var ret [](map[string]interface{}) err := mongo.DB().All(mongo.C_MONTH, nil, &ret) if err != nil { log.Error(err) return m } for _, v := range ret { log.Debug(v) if month, ok := v["name"]; ok { m = append(m, month.(string)) } } return m }