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

Method Lists

models/releate_book.go:26–86  ·  view source on GitHub ↗

Get the related books for a given book

(bookId int, limit ...int)

Source from the content-addressed store, hash-verified

24
25// Get the related books for a given book
26func (r *RelateBook) Lists(bookId int, limit ...int) (books []Book) {
27 day, _ := strconv.Atoi(GetOptionValue("RELATE_BOOK", "0"))
28 if day <= 0 {
29 return
30 }
31
32 length := 12
33 if len(limit) > 0 && limit[0] > 0 {
34 length = limit[0]
35 }
36
37 var rb RelateBook
38 var ids []int
39
40 now := int(time.Now().Unix())
41
42 o := orm.NewOrm()
43 o.QueryTable(r).Filter("book_id", bookId).One(&rb)
44 bookModel := NewBook()
45
46 fields := []string{"book_id", "book_name", "cover", "identify"}
47
48 if rb.BookId > 0 && rb.Expire > now {
49 bookIds := rb.BookIds
50 if !strings.HasPrefix(bookIds, "[") {
51 bookIds = "[" + bookIds + "]"
52 }
53
54 err := json.Unmarshal([]byte(bookIds), &ids)
55 if err == nil && len(ids) > 0 {
56 books, _ = bookModel.GetBooksById(ids, fields...)
57 return
58 }
59 }
60
61 book, err := bookModel.Find(bookId)
62 if err != nil {
63 return
64 }
65
66 if GetOptionValue("ELASTICSEARCH_ON", "false") == "true" {
67 ids = listByES(book, length)
68 } else {
69 ids = listByDBWithLabel(book, length)
70 }
71
72 books, _ = bookModel.GetBooksById(ids, fields...)
73 rb.BookId = bookId
74 if ids == nil {
75 ids = []int{}
76 }
77 relatedIdBytes, _ := json.Marshal(ids)
78 rb.BookIds = string(relatedIdBytes)
79 rb.Expire = now + day*24*3600
80 if rb.Id > 0 {
81 o.Update(&rb)
82 } else {
83 o.Insert(&rb)

Callers

nothing calls this directly

Calls 9

GetBooksByIdMethod · 0.95
FindMethod · 0.95
GetOptionValueFunction · 0.85
NewBookFunction · 0.85
listByESFunction · 0.85
listByDBWithLabelFunction · 0.85
stringFunction · 0.85
UpdateMethod · 0.45
InsertMethod · 0.45

Tested by

no test coverage detected