feat(search): Add search sync function

This commit is contained in:
LinkinStars 2023-08-17 11:30:44 +08:00
parent 3c08f31e07
commit be5231671f
4 changed files with 56 additions and 3 deletions

View File

@ -212,7 +212,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
activityController := controller.NewActivityController(activityService)
roleController := controller_admin.NewRoleController(roleService)
pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData)
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configService)
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configService, dataData)
pluginController := controller_admin.NewPluginController(pluginCommonService)
permissionController := controller.NewPermissionController(rankService)
answerAPIRouter := router.NewAnswerAPIRouter(langController, userController, commentController, reportController, voteController, tagController, followController, collectionController, questionController, answerController, searchController, revisionController, rankController, controller_adminReportController, userAdminController, reasonController, themeController, siteInfoController, controllerSiteInfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController, permissionController)

View File

@ -0,0 +1,29 @@
package search_sync
import (
"context"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/base/pager"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/plugin"
)
func NewPluginSyncer(data *data.Data) plugin.SearchSyncer {
return &PluginSyncer{data: data}
}
type PluginSyncer struct {
data *data.Data
}
func (p *PluginSyncer) GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, total int64, err error) {
answerList = make([]*entity.Answer, 0)
total, err = pager.Help(page, pageSize, answerList, &entity.Answer{}, p.data.DB.Context(ctx))
return answerList, total, err
}
func (p *PluginSyncer) GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, total int64, err error) {
questionList = make([]*entity.Question, 0)
total, err = pager.Help(page, pageSize, questionList, &entity.Question{}, p.data.DB.Context(ctx))
return questionList, total, err
}

View File

@ -3,6 +3,8 @@ package plugin_common
import (
"context"
"encoding/json"
"github.com/answerdev/answer/internal/base/data"
"github.com/answerdev/answer/internal/repo/search_sync"
"github.com/answerdev/answer/internal/base/constant"
"github.com/answerdev/answer/internal/base/reason"
@ -23,12 +25,15 @@ type PluginConfigRepo interface {
type PluginCommonService struct {
configService *config.ConfigService
pluginConfigRepo PluginConfigRepo
data *data.Data
}
// NewPluginCommonService new report service
func NewPluginCommonService(
pluginConfigRepo PluginConfigRepo,
configService *config.ConfigService) *PluginCommonService {
configService *config.ConfigService,
data *data.Data,
) *PluginCommonService {
// init plugin status
pluginStatus, err := configService.GetStringValue(context.TODO(), constant.PluginStatus)
@ -61,6 +66,7 @@ func NewPluginCommonService(
return &PluginCommonService{
configService: configService,
pluginConfigRepo: pluginConfigRepo,
data: data,
}
}
@ -76,5 +82,16 @@ func (ps *PluginCommonService) UpdatePluginStatus(ctx context.Context) (err erro
// UpdatePluginConfig update plugin config
func (ps *PluginCommonService) UpdatePluginConfig(ctx context.Context, req *schema.UpdatePluginConfigReq) (err error) {
configValue, _ := json.Marshal(req.ConfigFields)
return ps.pluginConfigRepo.SavePluginConfig(ctx, req.PluginSlugName, string(configValue))
err = ps.pluginConfigRepo.SavePluginConfig(ctx, req.PluginSlugName, string(configValue))
if err != nil {
return err
}
_ = plugin.CallSearch(func(search plugin.Search) error {
if search.Info().SlugName == req.PluginSlugName {
search.RegisterSyncer(ctx, search_sync.NewPluginSyncer(ps.data))
}
return nil
})
return nil
}

View File

@ -2,6 +2,7 @@ package plugin
import (
"context"
"github.com/answerdev/answer/internal/entity"
)
type SearchResult struct {
@ -83,6 +84,7 @@ const (
type Search interface {
Base
RegisterSyncer(ctx context.Context, syncer SearchSyncer)
SearchContents(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
SearchQuestions(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
SearchAnswers(ctx context.Context, cond *SearchBasicCond) (res []SearchResult, total int64, err error)
@ -90,6 +92,11 @@ type Search interface {
DeleteContent(ctx context.Context, contentID string) error
}
type SearchSyncer interface {
GetAnswersPage(ctx context.Context, page, pageSize int) (answerList []*entity.Answer, total int64, err error)
GetQuestionsPage(ctx context.Context, page, pageSize int) (questionList []*entity.Question, total int64, err error)
}
var (
// CallUserCenter is a function that calls all registered parsers
CallSearch,