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

Method Create

controllers/BookController.go:580–668  ·  view source on GitHub ↗

Create 创建书籍.

()

Source from the content-addressed store, hash-verified

578
579// Create 创建书籍.
580func (this *BookController) Create() {
581 if opt, err := models.NewOption().FindByKey("ALL_CAN_WRITE_BOOK"); err == nil {
582 if opt.OptionValue == "false" && this.Member.Role == conf.MemberGeneralRole { // 读者无权限创建书籍
583 this.JsonResult(1, "普通读者无法创建书籍,如需创建书籍,请向管理员申请成为作者")
584 }
585 }
586
587 bookName := strings.TrimSpace(this.GetString("book_name", ""))
588 identify := strings.TrimSpace(this.GetString("identify", ""))
589 description := strings.TrimSpace(this.GetString("description", ""))
590 author := strings.TrimSpace(this.GetString("author", ""))
591 authorURL := strings.TrimSpace(this.GetString("author_url", ""))
592 privatelyOwned, _ := strconv.Atoi(this.GetString("privately_owned"))
593 commentStatus := this.GetString("comment_status")
594
595 if bookName == "" {
596 this.JsonResult(6001, "书籍名称不能为空")
597 }
598
599 if identify == "" {
600 this.JsonResult(6002, "书籍标识不能为空")
601 }
602
603 ok, err1 := regexp.MatchString(`^[a-zA-Z0-9_\-\.]*$`, identify)
604 if !ok || err1 != nil {
605 this.JsonResult(6003, "书籍标识只能包含字母、数字,以及“-”、“.”和“_”符号,且不能是纯数字")
606 }
607
608 if num, _ := strconv.Atoi(identify); strconv.Itoa(num) == identify {
609 this.JsonResult(6003, "书籍标识不能是纯数字")
610 }
611
612 if strings.Count(identify, "") > 50 {
613 this.JsonResult(6004, "书籍标识不能超过50字")
614 }
615
616 if strings.Count(description, "") > 500 {
617 this.JsonResult(6004, "书籍描述不能大于500字")
618 }
619
620 if privatelyOwned != 0 && privatelyOwned != 1 {
621 privatelyOwned = 1
622 }
623 if commentStatus != "open" && commentStatus != "closed" && commentStatus != "group_only" && commentStatus != "registered_only" {
624 commentStatus = "closed"
625 }
626
627 book := models.NewBook()
628
629 if books, _ := book.FindByField("identify", identify); len(books) > 0 {
630 this.JsonResult(6006, "书籍标识已存在")
631 }
632
633 book.Label = strings.Join(utils.SegWords(bookName+"。"+description, 5), ",")
634 book.BookName = bookName
635 book.Author = author
636 book.AuthorURL = authorURL
637 book.Description = description

Callers 2

RegisterLoggerFunction · 0.45
zipMethod · 0.45

Calls 11

FindByFieldMethod · 0.95
InsertMethod · 0.95
NewOptionFunction · 0.92
NewBookFunction · 0.92
SegWordsFunction · 0.92
GetDefaultCoverFunction · 0.92
NewBookResultFunction · 0.92
FindByKeyMethod · 0.80
JsonResultMethod · 0.80
CountMethod · 0.45
FindByIdentifyMethod · 0.45

Tested by

no test coverage detected