Exemplo n.º 1
0
        //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()));
        }