@@ -2,9 +2,13 @@ package search_sync
22
33import (
44 "context"
5+ "github.com/answerdev/answer/internal/base/constant"
56 "github.com/answerdev/answer/internal/base/data"
67 "github.com/answerdev/answer/internal/entity"
8+ "github.com/answerdev/answer/internal/schema"
9+ "github.com/answerdev/answer/pkg/uid"
710 "github.com/answerdev/answer/plugin"
11+ "github.com/segmentfault/pacman/log"
812)
913
1014func NewPluginSyncer (data * data.Data ) plugin.SearchSyncer {
@@ -15,16 +19,103 @@ type PluginSyncer struct {
1519 data * data.Data
1620}
1721
18- func (p * PluginSyncer ) GetAnswersPage (ctx context.Context , page , pageSize int ) (answerList []* entity.Answer , err error ) {
19- answerList = make ([]* entity.Answer , 0 )
22+ func (p * PluginSyncer ) GetAnswersPage (ctx context.Context , page , pageSize int ) (
23+ answerList []* plugin.SearchContent , err error ) {
24+ answers := make ([]* entity.Answer , 0 )
2025 startNum := (page - 1 ) * pageSize
21- err = p .data .DB .Context (ctx ).Limit (pageSize , startNum ).Find (& answerList )
22- return answerList , err
26+ err = p .data .DB .Context (ctx ).Limit (pageSize , startNum ).Find (& answers )
27+ if err != nil {
28+ return nil , err
29+ }
30+ return p .convertAnswers (ctx , answers )
2331}
2432
25- func (p * PluginSyncer ) GetQuestionsPage (ctx context.Context , page , pageSize int ) (questionList []* entity.Question , err error ) {
26- questionList = make ([]* entity.Question , 0 )
33+ func (p * PluginSyncer ) GetQuestionsPage (ctx context.Context , page , pageSize int ) (
34+ questionList []* plugin.SearchContent , err error ) {
35+ questions := make ([]* entity.Question , 0 )
2736 startNum := (page - 1 ) * pageSize
28- err = p .data .DB .Context (ctx ).Limit (pageSize , startNum ).Find (& questionList )
29- return questionList , err
37+ err = p .data .DB .Context (ctx ).Limit (pageSize , startNum ).Find (& questions )
38+ if err != nil {
39+ return nil , err
40+ }
41+ return p .convertQuestions (ctx , questions )
42+ }
43+
44+ func (p * PluginSyncer ) convertAnswers (ctx context.Context , answers []* entity.Answer ) (
45+ answerList []* plugin.SearchContent , err error ) {
46+ for _ , answer := range answers {
47+ question := & entity.Question {}
48+ exist , err := p .data .DB .Context (ctx ).Where ("id = ?" , answer .QuestionID ).Get (question )
49+ if err != nil {
50+ log .Errorf ("get question failed %s" , err )
51+ continue
52+ }
53+ if ! exist {
54+ continue
55+ }
56+
57+ tagListList := make ([]* entity.TagRel , 0 )
58+ tags := make ([]string , 0 )
59+ err = p .data .DB .Context (ctx ).Where ("object_id = ?" , uid .DeShortID (question .ID )).
60+ Where ("status = ?" , entity .TagRelStatusAvailable ).Find (& tagListList )
61+ if err != nil {
62+ log .Errorf ("get tag list failed %s" , err )
63+ }
64+ for _ , tag := range tagListList {
65+ tags = append (tags , tag .TagID )
66+ }
67+
68+ content := & plugin.SearchContent {
69+ ObjectID : answer .ID ,
70+ Title : question .Title ,
71+ Type : constant .AnswerObjectType ,
72+ Content : answer .ParsedText ,
73+ Answers : 0 ,
74+ Status : plugin .SearchContentStatus (answer .Status ),
75+ Tags : tags ,
76+ QuestionID : answer .QuestionID ,
77+ UserID : answer .UserID ,
78+ Views : int64 (question .ViewCount ),
79+ Created : answer .CreatedAt .Unix (),
80+ Active : answer .UpdatedAt .Unix (),
81+ Score : int64 (answer .VoteCount ),
82+ HasAccepted : answer .Accepted == schema .AnswerAcceptedEnable ,
83+ }
84+ answerList = append (answerList , content )
85+ }
86+ return answerList , nil
87+ }
88+
89+ func (p * PluginSyncer ) convertQuestions (ctx context.Context , questions []* entity.Question ) (
90+ questionList []* plugin.SearchContent , err error ) {
91+ for _ , question := range questions {
92+ tagListList := make ([]* entity.TagRel , 0 )
93+ tags := make ([]string , 0 )
94+ err := p .data .DB .Context (ctx ).Where ("object_id = ?" , question .ID ).
95+ Where ("status = ?" , entity .TagRelStatusAvailable ).Find (& tagListList )
96+ if err != nil {
97+ log .Errorf ("get tag list failed %s" , err )
98+ }
99+ for _ , tag := range tagListList {
100+ tags = append (tags , tag .TagID )
101+ }
102+ content := & plugin.SearchContent {
103+ ObjectID : question .ID ,
104+ Title : question .Title ,
105+ Type : constant .QuestionObjectType ,
106+ Content : question .ParsedText ,
107+ Answers : int64 (question .AnswerCount ),
108+ Status : plugin .SearchContentStatus (question .Status ),
109+ Tags : tags ,
110+ QuestionID : question .ID ,
111+ UserID : question .UserID ,
112+ Views : int64 (question .ViewCount ),
113+ Created : question .CreatedAt .Unix (),
114+ Active : question .UpdatedAt .Unix (),
115+ Score : int64 (question .VoteCount ),
116+ HasAccepted : question .AcceptedAnswerID != "" && question .AcceptedAnswerID != "0" ,
117+ }
118+ questionList = append (questionList , content )
119+ }
120+ return questionList , nil
30121}
0 commit comments