//setting token for login user func SetToken(id bson.ObjectId, token string) { model.UserC.Do(func(c *mgo.Collection) { if err := c.UpdateId(id, bson.M{"$set": bson.M{"token": token}}); err != nil { model.ErrorLog(model.UserC, err, id.Hex()+";"+token) } }) }
//文章删除 func DeleteArticle(id bson.ObjectId) error { var err error model.ArticleC.Do(func(c *mgo.Collection) { //先存档 art := new(Article) if err = c.FindId(id).One(art); err == nil { art.UpdateTime = time.Now() model.Move2Trash(model.ArticleC, art) } else { model.ErrorLog(model.ArticleC, err, art) } if err = c.RemoveId(id); err != nil { model.ErrorLog(model.ArticleC, err, art) } }) return err }
//文章修改 func ModifyArticle(id bson.ObjectId, title string, tags []string, markdown, htmlContent string) error { var err error //先将上一历史存档 model.ArticleC.Do(func(c *mgo.Collection) { art := new(Article) if err = c.FindId(id).One(art); err == nil { model.AddHistory(model.ArticleC, art) } else { model.ErrorLog(model.ArticleC, err, art) } art.Title = title art.Tags = tags art.Abstract = getAbstract(htmlContent) art.HtmlContent = htmlContent art.Markdown = markdown art.UpdateTime = time.Now() if err = c.UpdateId(id, art); err != nil { model.ErrorLog(model.ArticleC, err, art) } }) return err }
//create an user, with dup_key error for email or nickname. func AddUser(email, nickname, password string) error { salt := util.RandString(8) password = util.Md5(salt + password) u := &User{Id: bson.NewObjectId(), Email: email, NickName: nickname, Salt: salt, Password: password, CreateTime: time.Now()} var err error model.UserC.Do(func(c *mgo.Collection) { err = c.Insert(u) if err != nil && !mgo.IsDup(err) { model.ErrorLog(model.UserC, err, u) } }) return err }