Search search contents
(ctx context.Context, dto *schema.SearchDTO)
| 45 | |
| 46 | // Search search contents |
| 47 | func (ss *SearchService) Search(ctx context.Context, dto *schema.SearchDTO) (resp *schema.SearchResp, err error) { |
| 48 | if dto.Page < 1 { |
| 49 | dto.Page = 1 |
| 50 | } |
| 51 | if len(dto.Query) == 0 { |
| 52 | return &schema.SearchResp{ |
| 53 | Total: 0, |
| 54 | SearchResults: make([]*schema.SearchResult, 0), |
| 55 | }, nil |
| 56 | } |
| 57 | |
| 58 | // search type |
| 59 | cond := ss.searchParser.ParseStructure(ctx, dto) |
| 60 | |
| 61 | // check search plugin |
| 62 | var finder plugin.Search |
| 63 | _ = plugin.CallSearch(func(search plugin.Search) error { |
| 64 | finder = search |
| 65 | return nil |
| 66 | }) |
| 67 | |
| 68 | resp = &schema.SearchResp{} |
| 69 | // search plugin is not found, call system search |
| 70 | if finder == nil { |
| 71 | switch { |
| 72 | case cond.SearchAll(): |
| 73 | resp.SearchResults, resp.Total, err = |
| 74 | ss.searchRepo.SearchContents(ctx, cond.Words, cond.Tags, cond.UserID, cond.VoteAmount, dto.Page, dto.Size, dto.Order) |
| 75 | case cond.SearchQuestion(): |
| 76 | resp.SearchResults, resp.Total, err = |
| 77 | ss.searchRepo.SearchQuestions(ctx, cond.Words, cond.Tags, cond.NotAccepted, cond.Views, cond.AnswerAmount, dto.Page, dto.Size, dto.Order) |
| 78 | case cond.SearchAnswer(): |
| 79 | resp.SearchResults, resp.Total, err = |
| 80 | ss.searchRepo.SearchAnswers(ctx, cond.Words, cond.Tags, cond.Accepted, cond.QuestionID, dto.Page, dto.Size, dto.Order) |
| 81 | } |
| 82 | return |
| 83 | } |
| 84 | return ss.searchByPlugin(ctx, finder, cond, dto) |
| 85 | } |
| 86 | |
| 87 | func (ss *SearchService) searchByPlugin(ctx context.Context, finder plugin.Search, cond *schema.SearchCondition, dto *schema.SearchDTO) (resp *schema.SearchResp, err error) { |
| 88 | var res []plugin.SearchResult |
no test coverage detected