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

Method SearchBook

controllers/api/CommonController.go:406–466  ·  view source on GitHub ↗

[OK]

()

Source from the content-addressed store, hash-verified

404
405// [OK]
406func (this *CommonController) SearchBook() {
407 wd := this.GetString("wd")
408 if wd == "" {
409 this.Response(http.StatusBadRequest, messageBadRequest)
410 }
411
412 if models.NewOption().IsResponseEmptyForAPP(this.Version, wd) {
413 this.Response(http.StatusOK, messageSuccess, map[string]interface{}{"total": 0})
414 }
415
416 var (
417 page, _ = this.GetInt("page", 1)
418 size, _ = this.GetInt("size", 10)
419 ids []int
420 total int
421 apiBooks []APIBook
422 book APIBook
423 )
424
425 size = utils.RangeNumber(size, 10, maxPageSize)
426
427 client := models.NewElasticSearchClient()
428
429 if client.On { // elasticsearch 进行全文搜索
430 result, err := models.NewElasticSearchClient().Search(wd, page, size, false)
431 if err != nil {
432 beego.Error(err.Error())
433 this.Response(http.StatusInternalServerError, messageInternalServerError)
434 }
435
436 total = result.Hits.Total
437 for _, item := range result.Hits.Hits {
438 ids = append(ids, item.Source.Id)
439 }
440
441 } else { //MySQL like 查询
442 books, count, err := models.NewBook().SearchBook(wd, page, size)
443 if err != nil {
444 beego.Error(err.Error())
445 this.Response(http.StatusInternalServerError, messageInternalServerError)
446 }
447 total = count
448 for _, book := range books {
449 ids = append(ids, book.BookId)
450 }
451 }
452
453 data := map[string]interface{}{"total": total}
454
455 if len(ids) > 0 {
456 books, _ := models.NewBook().GetBooksById(ids)
457 for _, item := range books {
458 utils.CopyObject(&item, &book)
459 book.Cover = this.completeLink(utils.ShowImg(book.Cover, "cover"))
460 apiBooks = append(apiBooks, book)
461 }
462 data["result"] = apiBooks
463 }

Callers 1

ResultMethod · 0.45

Calls 12

NewOptionFunction · 0.92
RangeNumberFunction · 0.92
NewElasticSearchClientFunction · 0.92
NewBookFunction · 0.92
CopyObjectFunction · 0.92
ShowImgFunction · 0.92
ResponseMethod · 0.80
IsResponseEmptyForAPPMethod · 0.80
GetBooksByIdMethod · 0.80
completeLinkMethod · 0.80
appendFunction · 0.50
SearchMethod · 0.45

Tested by

no test coverage detected