public async Task <IActionResult> AddNote(ApiNote noteOrContent, string token) { var re = new ApiRe(); var user = tokenSerivce.GetUserByToken(token); if (user == null) { return(LeanoteJson(re)); } //json 返回状态乱 long?tokenUserId = GetUserIdByToken(token);; long?myUserId = tokenUserId; if (noteOrContent == null || string.IsNullOrEmpty(noteOrContent.NotebookId)) { return(Json(new ApiRe() { Ok = false, Msg = "notebookIdNotExists" }, MyJsonConvert.GetSimpleOptions())); } long?noteId = idGenerator.NextId(); if (noteOrContent.Title == null) { noteOrContent.Title = "无标题"; } // TODO 先上传图片/附件, 如果不成功, 则返回false //-------------新增文件和附件内容 int attachNum = 0; if (noteOrContent.Files != null && noteOrContent.Files.Length > 0) { for (int i = 0; i < noteOrContent.Files.Length; i++) { var file = noteOrContent.Files[i]; if (file.HasBody) { if (!string.IsNullOrEmpty(file.LocalFileId)) { var result = UploadImages("FileDatas[" + file.LocalFileId + "]", tokenUserId, noteId, file.IsAttach, out long?serverFileId, out string msg); if (!result) { if (string.IsNullOrEmpty(msg)) { re.Msg = "fileUploadError"; } else { re.Msg = msg; return(Json(re, MyJsonConvert.GetLeanoteOptions())); } } else { // 建立映射 file.FileId = serverFileId.ToHex24(); noteOrContent.Files[i] = file; if (file.IsAttach) { attachNum++; } } } else { //存在疑问 return(Json(new ReUpdate() { Ok = false, Msg = "LocalFileId_Is_NullOrEmpty", Usn = 0 }, MyJsonConvert.GetSimpleOptions())); } } } } else { } //-------------替换笔记内容中的文件ID FixPostNotecontent(ref noteOrContent); if (noteOrContent.Tags != null) { if (noteOrContent.Tags.Length > 0 && noteOrContent.Tags[0] == null) { noteOrContent.Tags = Array.Empty <string>(); //noteOrContent.Tags= new string[] { ""}; } } //-------------新增笔记对象 Note note = new Note() { UserId = tokenUserId, NoteId = noteId, CreatedUserId = tokenUserId, UpdatedUserId = noteId, NotebookId = noteOrContent.NotebookId.ToLongByHex(), Title = noteOrContent.Title, Tags = noteOrContent.Tags, Desc = noteOrContent.Desc, IsBlog = noteOrContent.IsBlog.GetValueOrDefault(), IsMarkdown = noteOrContent.IsMarkdown.GetValueOrDefault(), AttachNum = attachNum, CreatedTime = noteOrContent.CreatedTime, UpdatedTime = noteOrContent.UpdatedTime, ContentId = idGenerator.NextId() }; //-------------新增笔记内容对象 NoteContent noteContent = new NoteContent() { NoteContentId = note.ContentId, NoteId = noteId, UserId = tokenUserId, IsBlog = note.IsBlog, Content = noteOrContent.Content, Abstract = noteOrContent.Abstract, CreatedTime = noteOrContent.CreatedTime, UpdatedTime = noteOrContent.UpdatedTime, IsHistory = false }; //-------------得到Desc, abstract if (string.IsNullOrEmpty(noteOrContent.Abstract)) { if (noteOrContent.IsMarkdown.GetValueOrDefault()) { // note.Desc = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200); noteContent.Abstract = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200); } else { //note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200); noteContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200); } } else { note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Abstract, 200); } if (noteOrContent.Desc == null) { if (noteOrContent.IsMarkdown.GetValueOrDefault()) { note.Desc = MyHtmlHelper.SubMarkDownToRaw(noteOrContent.Content, 200); } else { note.Desc = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200); } } else { note.Desc = noteOrContent.Desc; } note = noteService.AddNoteAndContent(note, noteContent, myUserId); //-------------将笔记与笔记内容保存到数据库 if (note == null || note.NoteId == 0) { return(Json(new ApiRe() { Ok = false, Msg = "AddNoteAndContent_is_error" })); } //-------------API返回客户端信息 noteOrContent.NoteId = noteId.ToHex24(); noteOrContent.UserId = tokenUserId.ToHex24(); noteOrContent.Title = note.Title; noteOrContent.Tags = note.Tags; noteOrContent.IsMarkdown = note.IsMarkdown; noteOrContent.IsBlog = note.IsBlog; noteOrContent.IsTrash = note.IsTrash; noteOrContent.IsDeleted = note.IsDeleted; noteOrContent.IsTrash = note.IsTrash; noteOrContent.IsTrash = note.IsTrash; noteOrContent.Usn = note.Usn; noteOrContent.CreatedTime = note.CreatedTime; noteOrContent.UpdatedTime = note.UpdatedTime; noteOrContent.PublicTime = note.PublicTime; //Files = files //------------- 删除API中不需要返回的内容 noteOrContent.Content = ""; noteOrContent.Abstract = ""; // apiNote := info.NoteToApiNote(note, noteOrContent.Files) return(Json(noteOrContent, MyJsonConvert.GetLeanoteOptions())); }
/// <summary> /// 更新笔记 元数据 /// </summary> /// <param name="apiNote"></param> /// <returns></returns> public static bool UpdateNote(ref ApiNote apiNote, long updateUser, long contentId, bool verifyUsn, bool verifyOwner, out string msg, out int afterUsn) { var noteId = MyConvert.HexToLong(apiNote.NoteId); afterUsn = 0; if (apiNote == null) { msg = "apiNote_is_null"; return(false); } // var noteId = MyConvert.HexToLong(apiNote.NoteId); if (noteId == 0) { msg = "noteId_is_note_long_Number"; return(false); } using (var db = new DataContext()) { var result = db.Note.Where(b => b.NoteId == noteId && b.UserId == updateUser); if (result == null) { msg = "inexistence"; return(false); } var note = result.FirstOrDefault(); if (verifyUsn) { if (note.Usn != apiNote.Usn) { msg = "Verify_Usn_Failure"; return(false); } } if (verifyOwner) { if (note.UserId != updateUser) { msg = "Verify_updateUser_Failure"; return(false); } } if (apiNote.Desc != null) { note.Desc = apiNote.Desc; } if (apiNote.Title != null) { note.Title = apiNote.Title; } if (apiNote.IsTrash != null) { note.IsTrash = apiNote.IsTrash.GetValueOrDefault(); } if (apiNote.IsBlog != null) { if (note.IsBlog == false && apiNote.IsBlog == true) { note.PublicTime = DateTime.Now; } note.IsBlog = apiNote.IsBlog.GetValueOrDefault(false); } if (apiNote.Tags != null) { note.Tags = apiNote.Tags; TagService.AddTags(note.UserId, note.Tags); BlogService.ReCountBlogTags(note.UserId); } if (apiNote.NotebookId != null) { var noteBookId = MyConvert.HexToLong(apiNote.NotebookId); if (note.NotebookId == 0) { msg = "NotebookId_Is_Illegal"; return(false); } if (note.NotebookId != noteBookId) { // 如果修改了notebookId, 则更新notebookId'count // 两方的notebook也要修改 NotebookService.ReCountNotebookNumberNotes(note.NotebookId); NotebookService.ReCountNotebookNumberNotes(noteBookId); note.NotebookId = noteBookId; } } if (apiNote.Content != null) { note.ContentId = contentId; if (apiNote.Abstract == null) { if (apiNote.IsMarkdown.GetValueOrDefault(note.IsMarkdown)) { note.Desc = MyHtmlHelper.SubMarkDownToRaw(apiNote.Content, 200); } else { note.Desc = MyHtmlHelper.SubHTMLToRaw(apiNote.Content, 200); } // note.Desc = MyHtmlHelper.SubStringHTMLToRaw(apiNote.Content, 200); } else { note.Desc = MyHtmlHelper.SubHTMLToRaw(apiNote.Abstract, 200); //note.Desc = MyHtmlHelper.SubStringHTMLToRaw(apiNote.Abstract, 200); } } if (apiNote.UpdatedTime != null) { note.UpdatedTime = Tools.FixUrlTime(apiNote.UpdatedTime); } else { note.UpdatedTime = DateTime.Now; } if (note.IsBlog && note.HasSelfDefined) { note.ImgSrc = null; note.Desc = null; } if (apiNote.IsTrash != null) { note.IsTrash = apiNote.IsTrash.GetValueOrDefault(false); NotebookService.ReCountNotebookNumberNotes(note.NotebookId); } if (apiNote.IsMarkdown != null) { note.IsMarkdown = apiNote.IsMarkdown.GetValueOrDefault(); } note.UpdatedUserId = MyConvert.HexToLong(apiNote.UserId); //更新用户元数据乐观锁 afterUsn = UserService.IncrUsn(note.UserId); //更新笔记元数据乐观锁 note.Usn = afterUsn; db.SaveChanges(); msg = "success"; return(true); } }