[OK]
()
| 404 | |
| 405 | // [OK] |
| 406 | func (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 | } |
no test coverage detected