當前位置: 首頁>>代碼示例>>Golang>>正文


Golang db.Insert函數代碼示例

本文整理匯總了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
}
開發者ID:intZz,項目名稱:leanote,代碼行數:37,代碼來源:ThemeService.go

示例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
}
開發者ID:howardroark2018,項目名稱:leanote-all,代碼行數:33,代碼來源:NoteService.go

示例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}, &noteTag)

	// 存在, 則更新之
	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
}
開發者ID:ClaudeXin,項目名稱:leanote,代碼行數:34,代碼來源:TagService.go

示例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)
}
開發者ID:sunyinhuiCoding,項目名稱:leanote,代碼行數:9,代碼來源:ShareService.go

示例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"}, &notes)

	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
}
開發者ID:ZypcGroup,項目名稱:leanote,代碼行數:32,代碼來源:BlogService.go

示例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
}
開發者ID:ClaudeXin,項目名稱:leanote,代碼行數:29,代碼來源:ThemeService.go

示例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
}
開發者ID:ZypcGroup,項目名稱:leanote,代碼行數:32,代碼來源:BlogService.go

示例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 
}
開發者ID:intZz,項目名稱:leanote,代碼行數:29,代碼來源:AttachService.go

示例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
}
開發者ID:ZypcGroup,項目名稱:leanote,代碼行數:32,代碼來源:BlogService.go

示例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
}
開發者ID:jianping11,項目名稱:leanote,代碼行數:9,代碼來源:NoteService.go

示例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
}
開發者ID:sunyinhuiCoding,項目名稱:leanote,代碼行數:39,代碼來源:NoteImageService.go

示例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
}
開發者ID:ClaudeXin,項目名稱:leanote,代碼行數:10,代碼來源:GroupService.go

示例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)
}
開發者ID:ClaudeXin,項目名稱:leanote,代碼行數:10,代碼來源:NoteContentHistoryService.go

示例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
}
開發者ID:sunyinhuiCoding,項目名稱:leanote,代碼行數:12,代碼來源:AttachService.go

示例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
}
開發者ID:howardroark2018,項目名稱:leanote-all,代碼行數:13,代碼來源:NoteService.go


注:本文中的github.com/leanote/leanote/app/db.Insert函數示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。