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

Method SetBookCates

models/book_category.go:32–71  ·  view source on GitHub ↗

处理书籍分类

(bookId int, cids []string)

Source from the content-addressed store, hash-verified

30
31//处理书籍分类
32func (this *BookCategory) SetBookCates(bookId int, cids []string) {
33
34 if len(cids) == 0 {
35 return
36 }
37
38 var (
39 cates []Category
40 tableCategory = "md_category"
41 tableBookCategory = "md_book_category"
42 )
43
44 o := orm.NewOrm()
45 o.QueryTable(tableCategory).Filter("id__in", cids).All(&cates, "id", "pid")
46
47 cidMap := make(map[string]bool)
48 for _, cate := range cates {
49 cidMap[strconv.Itoa(cate.Pid)] = true
50 cidMap[strconv.Itoa(cate.Id)] = true
51 }
52 cids = []string{}
53 for cid, _ := range cidMap {
54 cids = append(cids, cid)
55 }
56
57 o.QueryTable(tableBookCategory).Filter("book_id", bookId).Delete()
58 var bcs []BookCategory
59 for _, cid := range cids {
60 cidNum, _ := strconv.Atoi(cid)
61 bookCate := BookCategory{
62 CategoryId: cidNum,
63 BookId: bookId,
64 }
65 bcs = append(bcs, bookCate)
66 }
67 if l := len(bcs); l > 0 {
68 o.InsertMulti(l, &bcs)
69 }
70 go CountCategory()
71}

Callers 1

SaveBookMethod · 0.80

Calls 5

CountCategoryFunction · 0.85
InsertMultiMethod · 0.80
appendFunction · 0.50
AllMethod · 0.45
DeleteMethod · 0.45

Tested by

no test coverage detected