From ad644258f5dcb6d4b4242cbd2cea9b380ab22bae Mon Sep 17 00:00:00 2001 From: lealife Date: Sat, 18 Feb 2017 20:01:14 +0800 Subject: [PATCH] Support for leanote-chrome plugin --- app/controllers/NoteController.go | 11 +++++++++++ app/info/NoteInfo.go | 9 +++++++++ app/service/NoteService.go | 29 +++++++++++++++++++++++++++++ conf/routes | 1 + 4 files changed, 50 insertions(+) diff --git a/app/controllers/NoteController.go b/app/controllers/NoteController.go index 02ee66f..fe6ab1e 100644 --- a/app/controllers/NoteController.go +++ b/app/controllers/NoteController.go @@ -156,6 +156,16 @@ func (c Note) GetNoteAndContent(noteId string) revel.Result { return c.RenderJson(noteService.GetNoteAndContent(noteId, c.GetUserId())) } +func (c Note) GetNoteAndContentBySrc(src string) revel.Result { + noteId, noteAndContent := noteService.GetNoteAndContentBySrc(src, c.GetUserId()) + ret := info.Re{} + if noteId != "" { + ret.Ok = true + ret.Item = noteAndContent + } + return c.RenderJson(ret) +} + // 得到内容 func (c Note) GetNoteContent(noteId string) revel.Result { noteContent := noteService.GetNoteContent(noteId, c.GetUserId()) @@ -177,6 +187,7 @@ func (c Note) UpdateNoteOrContent(noteOrContent info.NoteOrContent) revel.Result NoteId: bson.ObjectIdHex(noteOrContent.NoteId), NotebookId: bson.ObjectIdHex(noteOrContent.NotebookId), Title: noteOrContent.Title, + Src: noteOrContent.Src, // 来源 Tags: strings.Split(noteOrContent.Tags, ","), Desc: noteOrContent.Desc, ImgSrc: noteOrContent.ImgSrc, diff --git a/app/info/NoteInfo.go b/app/info/NoteInfo.go index 5a8fb54..c9d29d5 100644 --- a/app/info/NoteInfo.go +++ b/app/info/NoteInfo.go @@ -15,6 +15,8 @@ type Note struct { Title string `Title` // 标题 Desc string `Desc` // 描述, 非html + Src string `Src,omitempty` // 来源, 2016/4/22 + ImgSrc string `ImgSrc` // 图片, 第一张缩略图地址 Tags []string `Tags,omitempty` @@ -92,6 +94,7 @@ type NoteOrContent struct { UserId string Title string Desc string + Src string ImgSrc string Tags string Content string @@ -101,3 +104,9 @@ type NoteOrContent struct { FromUserId string // 为共享而新建 IsBlog bool // 是否是blog, 更新note不需要修改, 添加note时才有可能用到, 此时需要判断notebook是否设为Blog } + +// 分开的 +type NoteAndContentSep struct { + NoteInfo Note + NoteContentInfo NoteContent +} diff --git a/app/service/NoteService.go b/app/service/NoteService.go index 157ad64..304e278 100644 --- a/app/service/NoteService.go +++ b/app/service/NoteService.go @@ -62,6 +62,35 @@ func (this *NoteService) GetNoteAndContent(noteId, userId string) (noteAndConten return info.NoteAndContent{note, noteContent} } +func (this *NoteService) GetNoteBySrc(src, userId string) (note info.Note) { + note = info.Note{} + if src == "" { + return + } + + notes := []info.Note{} + q := db.Notes.Find(bson.M{ + "UserId": bson.ObjectIdHex(userId), + "Src": src, + }) + q.Sort("-Usn").Limit(1).All(¬es) + if len(notes) > 0 { + return notes[0] + } + // db.GetByQ(db.Notes, bson.M{"Src": src, "UserId": bson.ObjectIdHex(userId), "IsDeleted": false}, ¬e) + return +} + +func (this *NoteService) GetNoteAndContentBySrc(src, userId string) (noteId string, noteAndContent info.NoteAndContentSep) { + note := this.GetNoteBySrc(src, userId) + if (note.NoteId != "") { + noteId = note.NoteId.Hex() + noteContent := this.GetNoteContent(note.NoteId.Hex(), userId) + return noteId, info.NoteAndContentSep{note, noteContent} + } + return +} + // 获取同步的笔记 // > afterUsn的笔记 func (this *NoteService) GetSyncNotes(userId string, afterUsn, maxEntry int) []info.ApiNote { diff --git a/conf/routes b/conf/routes index 7a26c58..f56d2dd 100644 --- a/conf/routes +++ b/conf/routes @@ -40,6 +40,7 @@ POST /findPasswordUpdate Auth.FindPasswordUpdate * /note/setNote2Blog Note.SetNote2Blog * /note/exportPdf Note.ExportPDF * /note/toPdf Note.ToPdf +* /note/getNoteAndContentBySrc Note.GetNoteAndContentBySrc # pjax GET /note/:noteId Note.Index