本文整理汇总了Golang中github.com/leanote/leanote/app/db.Insert函数的典型用法代码示例。如果您正苦于以下问题:Golang Insert函数的具体用法?Golang Insert怎么用?Golang Insert使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Insert函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: ImportTheme
// 导入主题
// path == /llllllll/..../public/upload/.../aa.zip, 绝对路径
func (this *ThemeService) ImportTheme(userId, path string) (ok bool, msg string) {
themeIdO := bson.NewObjectId()
themeId := themeIdO.Hex()
targetPath := this.getUserThemePath(userId, themeId) // revel.BasePath + "/public/upload/" + userId + "/themes/" + themeId
if ok, msg = archive.Unzip(path, targetPath); !ok {
DeleteFile(targetPath)
return
}
// 主题验证
if ok, msg = this.ValidateTheme(targetPath, "", ""); !ok {
DeleteFile(targetPath)
return
}
// 解压成功, 那么新建之
// 保存到数据库中
theme, _ := this.getThemeConfig(targetPath)
if theme.Name == "" {
ok = false
DeleteFile(targetPath)
msg = "解析错误"
return
}
theme.ThemeId = themeIdO
theme.Path = this.getUserThemePath2(userId, themeId)
theme.CreatedTime = time.Now()
theme.UpdatedTime = theme.CreatedTime
theme.UserId = bson.ObjectIdHex(userId)
ok = db.Insert(db.Themes, theme)
if !ok {
DeleteFile(targetPath)
}
DeleteFile(path)
return
}
示例2: AddNote
func (this *NoteService) AddNote(note info.Note, fromApi bool) info.Note {
if note.NoteId.Hex() == "" {
noteId := bson.NewObjectId()
note.NoteId = noteId
}
note.CreatedTime = time.Now()
note.UpdatedTime = note.CreatedTime
note.IsTrash = false
note.UpdatedUserId = note.UserId
note.UrlTitle = GetUrTitle(note.UserId.Hex(), note.Title, "note")
note.Usn = userService.IncrUsn(note.UserId.Hex())
notebookId := note.NotebookId.Hex()
// api会传IsBlog, web不会传
if !fromApi {
// 设为blog
note.IsBlog = notebookService.IsBlog(notebookId)
}
// if note.IsBlog {
note.PublicTime = note.UpdatedTime
// }
db.Insert(db.Notes, note)
// tag1
tagService.AddTags(note.UserId.Hex(), note.Tags)
// recount notebooks' notes number
notebookService.ReCountNotebookNumberNotes(notebookId)
return note
}
示例3: AddOrUpdateTag
// 添加或更新标签, 先查下是否存在, 不存在则添加, 存在则更新
// 都要统计下tag的note数
// 什么时候调用? 笔记添加Tag, 删除Tag时
// 删除note时, 都可以调用
// 万能
func (this *TagService) AddOrUpdateTag(userId string, tag string) info.NoteTag {
userIdO := bson.ObjectIdHex(userId)
noteTag := info.NoteTag{}
db.GetByQ(db.NoteTags, bson.M{"UserId": userIdO, "Tag": tag}, ¬eTag)
// 存在, 则更新之
if noteTag.TagId != "" {
// 统计note数
count := noteService.CountNoteByTag(userId, tag)
noteTag.Count = count
noteTag.UpdatedTime = time.Now()
// noteTag.Usn = userService.IncrUsn(userId), 更新count而已
db.UpdateByIdAndUserId(db.NoteTags, noteTag.TagId.Hex(), userId, noteTag)
return noteTag
}
// 不存在, 则创建之
noteTag.TagId = bson.NewObjectId()
noteTag.Count = 1
noteTag.Tag = tag
noteTag.UserId = bson.ObjectIdHex(userId)
noteTag.CreatedTime = time.Now()
noteTag.UpdatedTime = noteTag.CreatedTime
noteTag.Usn = userService.IncrUsn(userId)
noteTag.IsDeleted = false
db.Insert(db.NoteTags, noteTag)
return noteTag
}
示例4: AddShareNotebook1
// 添加一个notebook共享
// [ok]
func (this *ShareService) AddShareNotebook1(shareNotebook info.ShareNotebook) bool {
// 添加一条记录说明两者存在关系
this.AddHasShareNote(shareNotebook.UserId.Hex(), shareNotebook.ToUserId.Hex())
shareNotebook.CreatedTime = time.Now()
return db.Insert(db.ShareNotebooks, shareNotebook)
}
示例5: ReCountBlogTags
// 重新计算博客的标签
// 在设置设置/取消为博客时调用
func (this *BlogService) ReCountBlogTags(userId string) bool {
// 得到所有博客
notes := []info.Note{}
userIdO := bson.ObjectIdHex(userId)
query := bson.M{"UserId": userIdO, "IsTrash": false, "IsDeleted": false, "IsBlog": true}
db.ListByQWithFields(db.Notes, query, []string{"Tags"}, ¬es)
db.DeleteAll(db.TagCounts, bson.M{"UserId": userIdO, "IsBlog": true})
if notes == nil || len(notes) == 0 {
return true
}
// 统计所有的Tags和数目
tagsCount := map[string]int{}
for _, note := range notes {
tags := note.Tags
if tags != nil && len(tags) > 0 {
for _, tag := range tags {
count := tagsCount[tag]
count++
tagsCount[tag] = count
}
}
}
// 一个个插入
for tag, count := range tagsCount {
db.Insert(db.TagCounts,
info.TagCount{UserId: userIdO, IsBlog: true, Tag: tag, Count: count})
}
return true
}
示例6: CopyDefaultTheme
// 新建主题
// 复制默认主题到文件夹下
func (this *ThemeService) CopyDefaultTheme(userBlog info.UserBlog) (ok bool, themeId string) {
newThemeId := bson.NewObjectId()
themeId = newThemeId.Hex()
userId := userBlog.UserId.Hex()
themePath := this.getUserThemePath(userId, themeId)
err := os.MkdirAll(themePath, 0755)
if err != nil {
return
}
// 复制默认主题
defaultThemePath := this.getDefaultThemePath(userBlog.Style)
err = CopyDir(defaultThemePath, themePath)
if err != nil {
return
}
// 保存到数据库中
theme, _ := this.getThemeConfig(themePath)
theme.ThemeId = newThemeId
theme.Path = this.getUserThemePath2(userId, themeId)
theme.CreatedTime = time.Now()
theme.UpdatedTime = theme.CreatedTime
theme.UserId = bson.ObjectIdHex(userId)
ok = db.Insert(db.Themes, theme)
return ok, themeId
}
示例7: AddOrUpdateSingle
// 更新或添加
func (this *BlogService) AddOrUpdateSingle(userId, singleId, title, content string) (ok bool) {
ok = false
if singleId != "" {
ok = db.UpdateByIdAndUserIdMap(db.BlogSingles, singleId, userId, bson.M{
"Title": title,
"Content": content,
"UpdatedTime": time.Now(),
})
if ok {
// 还要修改UserBlog中的Singles
this.updateBlogSingles(userId, false, false, singleId, title, "")
}
return
}
// 添加
page := info.BlogSingle{
SingleId: bson.NewObjectId(),
UserId: bson.ObjectIdHex(userId),
Title: title,
Content: content,
UrlTitle: GetUrTitle(userId, title, "single", singleId),
CreatedTime: time.Now(),
}
page.UpdatedTime = page.CreatedTime
ok = db.Insert(db.BlogSingles, page)
// 还要修改UserBlog中的Singles
this.updateBlogSingles(userId, false, true, page.SingleId.Hex(), title, page.UrlTitle)
return
}
示例8: AddAttach
// add attach
// api调用时, 添加attach之前是没有note的
// fromApi表示是api添加的, updateNote传过来的, 此时不要incNote's usn, 因为updateNote会inc的
func (this *AttachService) AddAttach(attach info.Attach, fromApi bool) (ok bool, msg string) {
attach.CreatedTime = time.Now()
ok = db.Insert(db.Attachs, attach)
note := noteService.GetNoteById(attach.NoteId.Hex())
// api调用时, 添加attach之前是没有note的
var userId string
if note.NoteId != "" {
userId = note.UserId.Hex()
} else {
userId = attach.UploadUserId.Hex()
}
if ok {
// 更新笔记的attachs num
this.updateNoteAttachNum(attach.NoteId, 1)
}
if !fromApi {
// 增长note's usn
noteService.IncrNoteUsn(attach.NoteId.Hex(), userId)
}
return
}
示例9: LikeBlog
// 点赞
// retun ok , isLike
func (this *BlogService) LikeBlog(noteId, userId string) (ok bool, isLike bool) {
ok = false
isLike = false
if noteId == "" || userId == "" {
return
}
// 判断是否点过赞, 如果点过那么取消点赞
note := noteService.GetNoteById(noteId)
if !note.IsBlog /*|| note.UserId.Hex() == userId */ {
return
}
noteIdO := bson.ObjectIdHex(noteId)
userIdO := bson.ObjectIdHex(userId)
var n int
if !db.Has(db.BlogLikes, bson.M{"NoteId": noteIdO, "UserId": userIdO}) {
n = 1
// 添加之
db.Insert(db.BlogLikes, info.BlogLike{LikeId: bson.NewObjectId(), NoteId: noteIdO, UserId: userIdO, CreatedTime: time.Now()})
isLike = true
} else {
// 已点过, 那么删除之
n = -1
db.Delete(db.BlogLikes, bson.M{"NoteId": noteIdO, "UserId": userIdO})
isLike = false
}
ok = db.Update(db.Notes, bson.M{"_id": noteIdO}, bson.M{"$inc": bson.M{"LikeNum": n}})
return
}
示例10: AddNoteContent
// 添加笔记本内容
// [ok]
func (this *NoteService) AddNoteContent(noteContent info.NoteContent) info.NoteContent {
noteContent.CreatedTime = time.Now()
noteContent.UpdatedTime = noteContent.CreatedTime
noteContent.UpdatedUserId = noteContent.UserId
db.Insert(db.NoteContents, noteContent)
return noteContent
}
示例11: UpdateNoteImages
// 解析内容中的图片, 建立图片与note的关系
// <img src="/file/outputImage?fileId=12323232" />
// 图片必须是我的, 不然不添加
// imgSrc 防止博客修改了, 但内容删除了
func (this *NoteImageService) UpdateNoteImages(userId, noteId, imgSrc, content string) bool {
// 让主图成为内容的一员
if imgSrc != "" {
content = "<img src=\"" + imgSrc + "\" >" + content
}
reg, _ := regexp.Compile("outputImage\\?fileId=([a-z0-9A-Z]{24})")
find := reg.FindAllStringSubmatch(content, -1) // 查找所有的
// 删除旧的
db.DeleteAll(db.NoteImages, bson.M{"NoteId": bson.ObjectIdHex(noteId)})
// 添加新的
var fileId string
noteImage := info.NoteImage{NoteId: bson.ObjectIdHex(noteId)}
hasAdded := make(map[string]bool)
if find != nil && len(find) > 0 {
for _, each := range find {
if each != nil && len(each) == 2 {
fileId = each[1]
// 之前没能添加过的
if _, ok := hasAdded[fileId]; !ok {
Log(fileId)
// 判断是否是我的文件
if fileService.IsMyFile(userId, fileId) {
noteImage.ImageId = bson.ObjectIdHex(fileId)
db.Insert(db.NoteImages, noteImage)
}
hasAdded[fileId] = true
}
}
}
}
return true
}
示例12: AddGroup
// 添加分组
func (this *GroupService) AddGroup(userId, title string) (bool, info.Group) {
group := info.Group{
GroupId: bson.NewObjectId(),
UserId: bson.ObjectIdHex(userId),
Title: title,
CreatedTime: time.Now(),
}
return db.Insert(db.Groups, group), group
}
示例13: newHistory
// 新建历史
func (this *NoteContentHistoryService) newHistory(noteId, userId string, eachHistory info.EachHistory) {
history := info.NoteContentHistory{NoteId: bson.ObjectIdHex(noteId),
UserId: bson.ObjectIdHex(userId),
Histories: []info.EachHistory{eachHistory},
}
// 保存之
db.Insert(db.NoteContentHistories, history)
}
示例14: AddAttach
// add attach
func (this *AttachService) AddAttach(attach info.Attach) (ok bool, msg string) {
attach.CreatedTime = time.Now()
ok = db.Insert(db.Attachs, attach)
if ok {
// 更新笔记的attachs num
this.updateNoteAttachNum(attach.NoteId, 1)
}
return
}
示例15: AddNoteContent
// 添加笔记本内容
// [ok]
func (this *NoteService) AddNoteContent(noteContent info.NoteContent) info.NoteContent {
noteContent.CreatedTime = time.Now()
noteContent.UpdatedTime = noteContent.CreatedTime
noteContent.UpdatedUserId = noteContent.UserId
db.Insert(db.NoteContents, noteContent)
// 更新笔记图片
noteImageService.UpdateNoteImages(noteContent.UserId.Hex(), noteContent.NoteId.Hex(), "", noteContent.Content)
return noteContent
}