MCPcopy Index your code
hub / github.com/TruthHun/BookStack / Create

Method Create

controllers/DocumentController.go:596–693  ·  view source on GitHub ↗

创建一个文档.

()

Source from the content-addressed store, hash-verified

594
595// 创建一个文档.
596func (this *DocumentController) Create() {
597 identify := this.GetString("identify") //书籍书籍标识
598 docIdentify := this.GetString("doc_identify") //新建的文档标识
599 docName := this.GetString("doc_name")
600 parentId, _ := this.GetInt("parent_id", 0)
601 docId, _ := this.GetInt("doc_id", 0)
602 bookIdentify := strings.TrimSpace(this.GetString(":key"))
603
604 o := orm.NewOrm()
605
606 if identify == "" {
607 this.JsonResult(6001, "参数错误")
608 }
609 if docName == "" {
610 this.JsonResult(6004, "文档名称不能为空")
611 }
612 if docIdentify != "" {
613 if ok, err := regexp.MatchString(`^[a-zA-Z0-9_\-\.]*$`, docIdentify); !ok || err != nil {
614 this.JsonResult(6003, "文档标识只能是数字、字母,以及“-”、“_”和“.”等字符,并且不能是纯数字")
615 }
616 if num, _ := strconv.Atoi(docIdentify); docIdentify == "0" || strconv.Itoa(num) == docIdentify { //不能是纯数字
617 this.JsonResult(6005, "文档标识只能是数字、字母,以及“-”、“_”和“.”等字符,并且不能是纯数字")
618 }
619
620 if bookIdentify == "" {
621 this.JsonResult(1, "书籍参数不正确")
622 }
623
624 var book models.Book
625 o.QueryTable("md_books").Filter("Identify", bookIdentify).One(&book, "BookId")
626 if book.BookId == 0 {
627 this.JsonResult(1, "书籍未创建")
628 }
629
630 d, _ := models.NewDocument().FindByBookIdAndDocIdentify(book.BookId, docIdentify)
631 if d.DocumentId > 0 && d.DocumentId != docId {
632 this.JsonResult(6006, "文档标识已被使用")
633 }
634 } else {
635 docIdentify = fmt.Sprintf("date%v", time.Now().Format("20060102150405.md"))
636 }
637
638 bookId := 0
639 //如果是超级管理员则不判断权限
640 if this.Member.IsAdministrator() {
641 book, err := models.NewBook().FindByFieldFirst("identify", identify)
642 if err != nil {
643 beego.Error(err)
644 this.JsonResult(6002, "书籍不存在或权限不足")
645 }
646 bookId = book.BookId
647 } else {
648 bookResult, err := models.NewBookResult().FindByIdentify(identify, this.Member.MemberId)
649
650 if err != nil || bookResult.RoleId == conf.BookObserver {
651 beego.Error("FindByIdentify => ", err)
652 this.JsonResult(6002, "书籍不存在或权限不足")
653 }

Callers

nothing calls this directly

Calls 12

NewDocumentFunction · 0.92
NewBookFunction · 0.92
NewBookResultFunction · 0.92
JsonResultMethod · 0.80
IsAdministratorMethod · 0.80
GetFiledByIdMethod · 0.80
ReplaceIdentifyMethod · 0.80
FindByFieldFirstMethod · 0.45
FindByIdentifyMethod · 0.45
FindMethod · 0.45
InsertOrUpdateMethod · 0.45

Tested by

no test coverage detected