// 新建一个note, 不需要添加历史记录 // 添加历史 func (this *NoteContentHistoryService) AddHistory(noteId, userId string, eachHistory info.EachHistory) { // 检查是否是空 if eachHistory.Content == "" { return } // 先查是否存在历史记录, 没有则添加之 history := info.NoteContentHistory{} db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &history) if history.NoteId == "" { this.newHistory(noteId, userId, eachHistory) } else { // 判断是否超出 maxSize, 如果超出则pop最后一个, 再push之, 不用那么麻烦, 直接update吧, 虽然影响性能 // TODO l := len(history.Histories) if l >= maxSize { // history.Histories = history.Histories[l-maxSize:] // BUG, 致使都是以前的 history.Histories = history.Histories[:maxSize] } newHistory := []info.EachHistory{eachHistory} newHistory = append(newHistory, history.Histories...) // 在开头加了, 最近的在最前 history.Histories = newHistory // 更新之 db.UpdateByIdAndUserId(db.NoteContentHistories, noteId, userId, history) } return }
// delete image func (this *FileService) DeleteImage(userId, fileId string) (bool, string) { file := info.File{} db.GetByIdAndUserId(db.Files, fileId, userId, &file) if file.FileId != "" { if db.DeleteByIdAndUserId(db.Files, fileId, userId) { // delete image // TODO file.Path = strings.TrimLeft(file.Path, "/") var err error if strings.HasPrefix(file.Path, "upload") { Log(file.Path) err = os.Remove(revel.BasePath + "/public/" + file.Path) } else { err = os.Remove(revel.BasePath + "/" + file.Path) } if err == nil { return true, "" } return false, "delete file error!" } return false, "db error" } return false, "no such item" }
// 复制图片 func (this *FileService) CopyImage(userId, fileId, toUserId string) (bool, string) { // 是否已经复制过了 file2 := info.File{} db.GetByQ(db.Files, bson.M{"UserId": bson.ObjectIdHex(toUserId), "FromFileId": bson.ObjectIdHex(fileId)}, &file2) if file2.FileId != "" { return true, file2.FileId.Hex() } // 复制之 file := info.File{} db.GetByIdAndUserId(db.Files, fileId, userId, &file) if file.FileId == "" || file.UserId.Hex() != userId { return false, "" } _, ext := SplitFilename(file.Name) newFilename := NewGuid() + ext dir := "files/" + toUserId + "/images" filePath := dir + "/" + newFilename err := os.MkdirAll(dir, 0755) if err != nil { return false, "" } _, err = CopyFile(revel.BasePath+"/"+file.Path, revel.BasePath+"/"+filePath) if err != nil { Log(err) return false, "" } fileInfo := info.File{Name: newFilename, Title: file.Title, Path: filePath, Size: file.Size, FromFileId: file.FileId} id := bson.NewObjectId() fileInfo.FileId = id fileId = id.Hex() Ok, _ := this.AddImage(fileInfo, "", toUserId, false) if Ok { return Ok, id.Hex() } return false, "" }
// 得到分组, shareService用 func (this *GroupService) GetGroup(userId, groupId string) info.Group { // 得到分组s group := info.Group{} db.GetByIdAndUserId(db.Groups, groupId, userId, &group) return group }
// 得到某notebook func (this *NotebookService) GetNotebook(notebookId, userId string) info.Notebook { notebook := info.Notebook{} db.GetByIdAndUserId(db.Notebooks, notebookId, userId, ¬ebook) return notebook }
// 通过id, userId得到noteContent func (this *NoteService) GetNoteContent(noteId, userId string) (noteContent info.Note) { noteContent = info.Note{} db.GetByIdAndUserId(db.Notes, noteId, userId, ¬eContent) return }
// 列表展示 func (this *NoteContentHistoryService) ListHistories(noteId, userId string) []info.EachHistory { histories := info.NoteContentHistory{} db.GetByIdAndUserId(db.NoteContentHistories, noteId, userId, &histories) return histories.Histories }