public IActionResult Cate(string blogUserName, string cateHex, int page) { long notebookId = MyConvert.HexToLong(cateHex); User blogUser = ActionInitBlogUser(blogUserName); if (blogUser == null) { return(Content("查无此人")); } if (page < 1) { //页码 page = 1; } ViewBag.page = page; ViewBag.postCount = BlogService.CountTheNumberForBlogsOfNoteBookId(blogUser.UserId, notebookId); NoteAndContent[] noteAndContent = NoteService.GetNoteAndContentForBlogOfNoteBookId(page, notebookId, blogUser.UserId); ViewBag.noteAndContent = noteAndContent; if (blogUser == null) { return(Content("查无此人")); } ViewBag.CateArray = BlogService.GetCateArrayForBlog(blogUser.UserId); Dictionary <string, string> blog = new Dictionary <string, string>(); blog.Add("Title", "标题"); blog.Add("keywords", "关键字"); ViewBag.blog = blog; return(View()); }
//修改笔记 public IActionResult UpdateNotebook(string token, string notebookId, string title, string parentNotebookId, int seq, int usn) { User user = TokenSerivce.GetUserByToken(token); if (user == null) { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "Not logged in", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } else { Notebook notebook; if (NotebookService.UpdateNotebookApi(user.UserId, MyConvert.HexToLong(notebookId), title, MyConvert.HexToLong(parentNotebookId), seq, usn, out notebook)) { ApiNotebook apiNotebook = fixNotebook(notebook); return(Json(apiNotebook, MyJsonConvert.GetOptions())); } else { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "UpdateNotebook is error", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } } }
//todo:得到笔记本下的笔记 public IActionResult GetNotes(string notebookId, string token) { Note[] notes = NoteService.ListNotes(getUserIdByToken(token), MyConvert.HexToLong(notebookId), false); long myNotebookId = MyConvert.HexToLong(notebookId); return(null); }
//todo:格式化URL //todo:得到内容 public IActionResult GetNoteContent(string token, string noteId) { ApiRe falseRe = new ApiRe() { Ok = false, Msg = "GetNoteContent_is_error" }; Note note = NoteService.GetNote(MyConvert.HexToLong(noteId), getUserIdByToken(token)); NoteContent noteContent = NoteContentService.GetNoteContent(MyConvert.HexToLong(noteId), getUserIdByToken(token), false); if (noteContent == null || note == null) { return(Json(falseRe, MyJsonConvert.GetOptions())); } if (noteContent != null && !string.IsNullOrEmpty(noteContent.Content)) { noteContent.Content = NoteService.FixContent(noteContent.Content, note.IsMarkdown); } ApiNoteContent apiNote = new ApiNoteContent() { NoteId = note.NoteId, UserId = note.UserId, Content = noteContent.Content }; return(Json(apiNote, MyJsonConvert.GetOptions())); }
//todo: 输出image public IActionResult GetImage(string fileId) { try { if (string.IsNullOrEmpty(fileId)) { return(Content("error")); } if (fileId.Length == 24) { fileId = fileId.Substring(0, 16); } var myFileId = MyConvert.HexToLong(fileId); var noteFile = FileService.GetFile(myFileId); if (noteFile == null) { //return Content("NoFoundImage"); return(NoFoundImage()); } var stream = System.IO.File.OpenRead(noteFile.Path); string fileExt = Path.GetExtension(noteFile.Name); //获取文件的ContentType var provider = new FileExtensionContentTypeProvider(); var memi = provider.Mappings[fileExt]; return(File(stream, memi)); }catch (Exception ex) { return(NoFoundImage()); } }
public JsonResult DoRegister(string email, string pwd, string iu) { if (!ConfigService.IsOpenRegister()) { return(Json(new ApiRe() { Ok = false, Msg = "管理员已经将注册功能关闭" }, MyJsonConvert.GetSimpleOptions())); } bool result = AuthService.Register(email, pwd, MyConvert.HexToLong(iu)); if (result) { return(Json(new ApiRe() { Ok = true, Msg = "Success" }, MyJsonConvert.GetSimpleOptions())); } else { return(Json(new ApiRe() { Ok = false, Msg = "注册失败" }, MyJsonConvert.GetSimpleOptions())); } }
public long GetUserIdBySession() { string userid_hex = _accessor.HttpContext.Session.GetString("_userId"); long userid_number = MyConvert.HexToLong(userid_hex); return(userid_number); }
//todo:得到note和内容 public IActionResult GetNoteAndContent(string token, string noteId) { User tokenUser = TokenSerivce.GetUserByToken(token); if (tokenUser == null) { return(Json(new ApiRe() { Ok = false, Msg = "" }, MyJsonConvert.GetOptions())); } NoteAndContent noteAndContent = NoteService.GetNoteAndContent(MyConvert.HexToLong(noteId), tokenUser.UserId, false, false, false); ApiNote[] apiNotes = NoteService.ToApiNotes(new Note[] { noteAndContent.note }); ApiNote apiNote = apiNotes[0]; apiNote.Content = NoteService.FixContent(noteAndContent.noteContent.Content, noteAndContent.note.IsMarkdown); apiNote.Desc = noteAndContent.note.Desc; apiNote.Abstract = noteAndContent.noteContent.Abstract; if (noteAndContent == null) { return(Json(new ApiRe() { Ok = false, Msg = "" }, MyJsonConvert.GetOptions())); } else { return(Json(apiNote, MyJsonConvert.GetOptions())); } }
//todo:删除笔记本 public IActionResult DeleteNotebook(string token, string notebookId, int usn) { User user = TokenSerivce.GetUserByToken(token); if (user == null) { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "Not logged in", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } if (NotebookService.DeleteNotebookForce(user.UserId, MyConvert.HexToLong(notebookId), usn)) { ApiRe apiRe = new ApiRe() { Ok = true, Msg = "success", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } else { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "conflict", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } }
public IActionResult Post1(string noteIdHex) { long noteId = MyConvert.HexToLong(noteIdHex); Note note = NoteService.GetNoteById(noteId); User user = UserService.GetUserByUserId(note.UserId); return(Redirect($"/Blog/Post/{user.Username}/{noteIdHex}")); }
public User GetUserBySession() { string userid_hex = _accessor.HttpContext.Session.GetString("_userId"); long userid_number = MyConvert.HexToLong(userid_hex); User user = UserService.GetUserByUserId(userid_number); return(user); }
public void ConvertStrToLongTest() { long a = 1213656203658399745; string hex = a.ToString("x"); Console.WriteLine(hex); long b = MyConvert.HexToLong(hex); Console.WriteLine(b);//1213656226102120449 }
public long ConvertUserIdToLong() { string hex = _accessor.HttpContext.Request.Form["userId"]; if (string.IsNullOrEmpty(hex)) { hex = _accessor.HttpContext.Request.Query["userId"]; } if (string.IsNullOrEmpty(hex)) { return(0); } return(MyConvert.HexToLong(hex)); }
public long getUserIdByToken() { string token = GetToken(); if (string.IsNullOrEmpty(token)) { string userid_hex = _accessor.HttpContext.Session.GetString("userId"); long userid_number = MyConvert.HexToLong(userid_hex); return(userid_number); } else { User user = TokenSerivce.GetUserByToken(token); long userid = (user == null ? 0 : user.UserId); return(userid); } }
public async Task <IActionResult> PostAsync(string blogUserName, string noteIdHex) { //添加访问日志 await InsertLogAsync($"Blog/Post/{blogUserName}/{noteIdHex}/").ConfigureAwait(false); User blogUser = ActionInitBlogUser(blogUserName); if (blogUser == null) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Content("查无此人")); } long noteId = MyConvert.HexToLong(noteIdHex); if (noteId == 0) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Content("未找到")); } Dictionary <string, string> blog = new Dictionary <string, string>(); NoteAndContent noteAndContent = NoteService.GetNoteAndContent(noteId); NoteService.AddReadNum(noteId); if (noteAndContent == null) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Content("未经授权的访问")); } if (noteAndContent.note.IsDeleted || noteAndContent.note.IsTrash) { Response.StatusCode = (int)HttpStatusCode.NotFound; return(Content("这篇文章已经被删除")); } if (!noteAndContent.note.IsBlog) { Response.StatusCode = (int)HttpStatusCode.Unauthorized; return(Content("这篇文章已经被取消分享")); } ViewBag.noteAndContent = noteAndContent; blog.Add("Title", noteAndContent.note.Title); blog.Add("NoteTitle", noteAndContent.note.Title); blog.Add("keywords", "关键字"); ViewBag.blog = blog; return(View()); }
public IActionResult Index(string blogUserIdHex, int page) { if (page < 1) { //页码 page = 1; } ViewBag.page = page; User blogUser = null; if (string.IsNullOrEmpty(blogUserIdHex)) { //默认账号 string blogUserName = "******"; blogUser = UserService.GetUserByUserName(blogUserName); } else { blogUser = UserService.GetUserByUserId(MyConvert.HexToLong(blogUserIdHex)); } if (blogUser == null) { blogUser = UserService.GetUserByUserName(blogUserIdHex); if (blogUser == null) { return(Content("查无此人")); } } ViewBag.blogUser = blogUser; ViewBag.postCount = BlogService.CountTheNumberForBlogs(blogUser.UserId); NoteAndContent[] noteAndContent = NoteService.GetNoteAndContentForBlog(page, blogUser.UserId); ViewBag.noteAndContent = noteAndContent; ViewBag.CateArray = BlogService.GetCateArrayForBlog(blogUser.UserId); Dictionary <string, string> blog = new Dictionary <string, string>(); blog.Add("Title", "moreote云笔记"); blog.Add("keywords", "搜索"); ViewBag.blog = blog; return(View()); }
//添加notebook public IActionResult AddNotebook(string token, string title, string parentNotebookId, int seq) { User user = TokenSerivce.GetUserByToken(token); if (user == null) { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "Not logged in", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } else { Notebook notebook = new Notebook() { NotebookId = SnowFlake_Net.GenerateSnowFlakeID(), Title = title, Seq = seq, UserId = user.UserId, ParentNotebookId = MyConvert.HexToLong(parentNotebookId) }; if (NotebookService.AddNotebook(ref notebook)) { ApiNotebook apiNotebook = fixNotebook(notebook); return(Json(apiNotebook, MyJsonConvert.GetOptions())); } else { ApiRe apiRe = new ApiRe() { Ok = false, Msg = "AddNotebook is error", }; return(Json(apiRe, MyJsonConvert.GetOptions())); } } }
//todo:删除trash public JsonResult DeleteTrash(string noteId, int usn, string token) { bool result = TrashService.DeleteTrashApi(MyConvert.HexToLong(noteId), getUserIdByToken(token), usn, out string msg, out int afterUsn); if (result) { return(Json(new ReUpdate() { Ok = true, Msg = "", Usn = afterUsn }, MyJsonConvert.GetOptions())); } else { return(Json(new ReUpdate() { Ok = false, Msg = msg, Usn = afterUsn }, MyJsonConvert.GetOptions())); } }
//todo:下载附件 public IActionResult GetAttach(string fileId) { if (string.IsNullOrEmpty(fileId)) { return(Content("error")); } if (fileId.Length == 24) { fileId = fileId.Substring(0, 16); } var attachFile = AttachService.GetAttach(MyConvert.HexToLong(fileId)); if (attachFile == null) { return(Content("NoFoundAttach")); } var stream = System.IO.File.OpenRead(attachFile.Path); string fileExt = Path.GetExtension(attachFile.Name); //获取文件的ContentType var provider = new FileExtensionContentTypeProvider(); var memi = provider.Mappings[fileExt]; return(File(stream, memi, Path.GetFileName(attachFile.Path))); }
//todo:添加笔记 public JsonResult AddNote(ApiNote noteOrContent, string token) { var x = _accessor.HttpContext.Request.Form.Files; var z = x["FileDatas[5e36bafc26f2af1a79000000]"]; //json 返回状态好乱呀 /(ㄒoㄒ)/~~ Re re = Re.NewRe(); 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 = SnowFlake_Net.GenerateSnowFlakeID(); 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 = upload("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.GetOptions())); } } else { // 建立映射 file.FileId = serverFileId.ToString("x"); 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 = noteId, UpdatedUserId = noteId, NotebookId = MyConvert.HexToLong(noteOrContent.NotebookId), 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 = SnowFlake_Net.GenerateSnowFlakeID() }; //-------------新增笔记内容对象 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.ToString("x"); noteOrContent.UserId = tokenUserId.ToString("x"); 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.GetOptions())); }
//todo:更新笔记 public JsonResult UpdateNote(ApiNote noteOrContent, string token) { Note noteUpdate = new Note(); var needUpdateNote = false; var re = new ReUpdate(); long tokenUserId = getUserIdByToken(token); var noteId = MyConvert.HexToLong(noteOrContent.NoteId); //-------------校验参数合法性 if (tokenUserId == 0) { re.Msg = "NOlogin"; re.Ok = false; return(Json(re, MyJsonConvert.GetSimpleOptions())); } if (string.IsNullOrEmpty(noteOrContent.NoteId)) { re.Msg = "noteIdNotExists"; re.Ok = false; return(Json(re, MyJsonConvert.GetSimpleOptions())); } if (noteOrContent.Usn < 1) { re.Msg = "usnNotExists"; re.Ok = false; return(Json(re, MyJsonConvert.GetSimpleOptions())); } // 先判断USN的问题, 因为很可能添加完附件后, 会有USN冲突, 这时附件就添错了 var note = NoteService.GetNote(noteId, tokenUserId); var noteContent = NoteContentService.GetNoteContent(note.NoteId, tokenUserId, false); if (note == null || note.NoteId == 0) { re.Msg = "notExists"; re.Ok = false; return(Json(re, MyJsonConvert.GetSimpleOptions())); } //判断服务器版本与客户端版本是否一致 if (note.Usn != noteOrContent.Usn) { re.Msg = "conflict"; re.Ok = false; return(Json(re, MyJsonConvert.GetSimpleOptions())); } //-------------更新文件和附件内容 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 = upload("FileDatas[" + file.LocalFileId + "]", tokenUserId, noteId, file.IsAttach, out long serverFileId, out string msg); if (!result) { if (string.IsNullOrEmpty(msg)) { re.Msg = "fileUploadError"; } if (!string.Equals(msg, "notImage", System.StringComparison.OrdinalIgnoreCase)) { return(Json(re, MyJsonConvert.GetOptions())); } } else { // 建立映射 file.FileId = serverFileId.ToString("x"); noteOrContent.Files[i] = file; } } else { return(Json(new ReUpdate() { Ok = false, Msg = "LocalFileId_Is_NullOrEmpty", Usn = 0 }, MyJsonConvert.GetSimpleOptions())); } } } } //更新用户元数据 //int usn = UserService.IncrUsn(tokenUserId); // 移到外面来, 删除最后一个file时也要处理, 不然总删不掉 // 附件问题, 根据Files, 有些要删除的, 只留下这些 if (noteOrContent.Files != null) { AttachService.UpdateOrDeleteAttachApi(noteId, tokenUserId, noteOrContent.Files); } //-------------更新笔记内容 var afterContentUsn = 0; var contentOk = false; var contentMsg = ""; long contentId = 0; if (noteOrContent.Content != null) { // 把fileId替换下 FixPostNotecontent(ref noteOrContent); // 如果传了Abstract就用之 if (noteOrContent.Abstract != null) { noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Abstract, 200); } else { noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteOrContent.Content, 200); } } else { noteOrContent.Abstract = MyHtmlHelper.SubHTMLToRaw(noteContent.Content, 200); } //上传noteContent的变更 contentOk = NoteContentService.UpdateNoteContent( noteOrContent, out contentMsg, out contentId ); //返回处理结果 if (!contentOk) { re.Ok = false; re.Msg = contentMsg; re.Usn = afterContentUsn; return(Json(re, MyJsonConvert.GetOptions())); } //-------------更新笔记元数据 int afterNoteUsn = 0; var noteOk = false; var noteMsg = ""; noteOk = NoteService.UpdateNote( ref noteOrContent, tokenUserId, contentId, true, true, out noteMsg, out afterNoteUsn ); if (!noteOk) { re.Ok = false; re.Msg = noteMsg; return(Json(re, MyJsonConvert.GetOptions())); } //处理结果 //-------------API返回客户端信息 note = NoteService.GetNote(noteId, tokenUserId); // noteOrContent.NoteId = noteId.ToString("x"); // noteOrContent.UserId = tokenUserId.ToString("x"); // 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.Usn = note.Usn; //noteOrContent.CreatedTime = note.CreatedTime; //noteOrContent.UpdatedTime = note.UpdatedTime; //noteOrContent.PublicTime = note.PublicTime; noteOrContent.Content = ""; noteOrContent.Usn = afterNoteUsn; noteOrContent.UpdatedTime = DateTime.Now; noteOrContent.IsDeleted = false; noteOrContent.UserId = tokenUserId.ToString("x"); return(Json(noteOrContent, MyJsonConvert.GetOptions())); }
/// <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); } }
public static bool UpdateNoteContent(ApiNote apiNote, out string msg, out long contentId) { using (var db = new DataContext()) { //更新 将其他笔记刷新 var noteId = MyConvert.HexToLong(apiNote.NoteId); var note = db.Note.Where(b => b.NoteId == noteId).First(); var noteContent = db.NoteContent.Where(b => b.NoteId == noteId && b.IsHistory == false).FirstOrDefault(); //如果笔记内容发生变化,生成新的笔记内容 if (apiNote.Content != null) { //新增笔记内容,需要将上一个笔记设置为历史笔记 db.NoteContent.Where(b => b.NoteId == noteId && b.IsHistory == false).Update(x => new NoteContent() { IsHistory = true }); contentId = SnowFlake_Net.GenerateSnowFlakeID(); NoteContent contentNew = new NoteContent() { NoteContentId = contentId, NoteId = noteContent.NoteId, UserId = noteContent.UserId, IsBlog = noteContent.IsBlog, Content = noteContent.Content, Abstract = noteContent.Abstract, CreatedTime = noteContent.CreatedTime, UpdatedTime = noteContent.UpdatedTime, UpdatedUserId = noteContent.UpdatedUserId, IsHistory = noteContent.IsHistory, }; contentNew.IsHistory = false; if (apiNote.IsBlog != null) { contentNew.IsBlog = apiNote.IsBlog.GetValueOrDefault(); } if (apiNote.Abstract != null) { contentNew.Abstract = apiNote.Abstract; } if (apiNote.Content != null) { contentNew.Content = apiNote.Content; } if (apiNote.UpdatedTime != null) { contentNew.UpdatedTime = apiNote.UpdatedTime; } db.NoteContent.Add(contentNew); msg = ""; return(db.SaveChanges() > 0); } else { //没有新增笔记内容,那么仅仅修改原始笔记内容就可以了 contentId = noteContent.NoteContentId; if (apiNote.IsBlog != null) { noteContent.IsBlog = apiNote.IsBlog.GetValueOrDefault(); } if (apiNote.Abstract != null) { noteContent.Abstract = apiNote.Abstract; } if (apiNote.UpdatedTime != null) { noteContent.UpdatedTime = apiNote.UpdatedTime; } } msg = ""; db.SaveChanges(); return(true); } }