answer/internal/schema/search_schema.go

135 lines
3.8 KiB
Go
Raw Normal View History

2022-09-27 17:59:05 +08:00
package schema
2023-07-21 17:27:01 +08:00
import (
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/plugin"
)
2023-07-20 18:48:35 +08:00
2022-09-27 17:59:05 +08:00
type SearchDTO struct {
2023-07-18 18:11:18 +08:00
UserID string // UserID current login user ID
Query string `validate:"required,gte=1,lte=60" json:"q" form:"q"` // Query the query string
Page int `validate:"omitempty,min=1" form:"page,default=1" json:"page"` //Query number of pages
Size int `validate:"omitempty,min=1,max=50" form:"size,default=30" json:"size"` //Search page size
Order string `validate:"required,oneof=newest active score relevance" form:"order,default=relevance" json:"order" enums:"newest,active,score,relevance"`
CaptchaID string `json:"captcha_id"` // captcha_id
CaptchaCode string `json:"captcha_code"`
2022-09-27 17:59:05 +08:00
}
2023-07-20 18:48:35 +08:00
type SearchCondition struct {
// search target type: all/question/answer
TargetType string
// search query user id
UserID string
// vote amount
VoteAmount int
// only show not accepted answer's question
NotAccepted bool
// view amount
Views int
// answer count
AnswerAmount int
// only show accepted answer
Accepted bool
// only show this question's answer
QuestionID string
// search query tags
Tags []string
// search query keywords
Words []string
}
// SearchAll check if search all
func (s *SearchCondition) SearchAll() bool {
return len(s.TargetType) == 0
}
// SearchQuestion check if search only need question
func (s *SearchCondition) SearchQuestion() bool {
return s.TargetType == constant.QuestionObjectType
}
// SearchAnswer check if search only need answer
func (s *SearchCondition) SearchAnswer() bool {
return s.TargetType == constant.AnswerObjectType
}
2023-07-21 17:27:01 +08:00
// Convert2PluginSearchCond convert to plugin search condition
func (s *SearchCondition) Convert2PluginSearchCond(page, pageSize int, order string) *plugin.SearchBasicCond {
basic := &plugin.SearchBasicCond{
Page: page,
PageSize: pageSize,
Words: s.Words,
TagIDs: s.Tags,
UserID: s.UserID,
2023-07-26 16:35:33 +08:00
Order: plugin.SearchOrderCond(order),
2023-07-21 17:27:01 +08:00
QuestionID: s.QuestionID,
VoteAmount: s.VoteAmount,
ViewAmount: s.Views,
AnswerAmount: s.AnswerAmount,
}
if s.Accepted {
basic.AnswerAccepted = plugin.AcceptedCondTrue
} else {
basic.AnswerAccepted = plugin.AcceptedCondAll
}
if s.NotAccepted {
basic.QuestionAccepted = plugin.AcceptedCondFalse
} else {
basic.QuestionAccepted = plugin.AcceptedCondAll
}
return basic
}
2022-09-27 17:59:05 +08:00
type SearchObject struct {
ID string `json:"id"`
2022-11-17 14:14:02 +08:00
QuestionID string `json:"question_id"`
2022-09-27 17:59:05 +08:00
Title string `json:"title"`
Excerpt string `json:"excerpt"`
CreatedAtParsed int64 `json:"created_at"`
VoteCount int `json:"vote_count"`
Accepted bool `json:"accepted"`
AnswerCount int `json:"answer_count"`
// user info
UserInfo *SearchObjectUser `json:"user_info"`
2022-09-27 17:59:05 +08:00
// tags
Tags []*TagResp `json:"tags"`
2022-09-30 10:28:38 +08:00
// Status
StatusStr string `json:"status"`
2022-09-27 17:59:05 +08:00
}
type SearchObjectUser struct {
ID string `json:"id"`
Username string `json:"username"`
DisplayName string `json:"display_name"`
Rank int `json:"rank"`
Status string `json:"status"`
}
2022-09-27 17:59:05 +08:00
type TagResp struct {
ID string `json:"-"`
2022-10-11 10:18:26 +08:00
SlugName string `json:"slug_name"`
DisplayName string `json:"display_name"`
2022-09-27 17:59:05 +08:00
// if main tag slug name is not empty, this tag is synonymous with the main tag
MainTagSlugName string `json:"main_tag_slug_name"`
2022-11-14 16:44:48 +08:00
Recommend bool `json:"recommend"`
2022-11-15 19:00:48 +08:00
Reserved bool `json:"reserved"`
2022-09-27 17:59:05 +08:00
}
type SearchResult struct {
2022-09-27 17:59:05 +08:00
// object_type
ObjectType string `json:"object_type"`
// this object
Object *SearchObject `json:"object"`
2022-09-27 17:59:05 +08:00
}
type SearchResp struct {
2022-09-27 17:59:05 +08:00
Total int64 `json:"count"`
// search response
SearchResults []*SearchResult `json:"list"`
}
type SearchDescResp struct {
Name string `json:"name"`
Icon string `json:"icon"`
2022-09-27 17:59:05 +08:00
}