mirror of https://gitee.com/answerdev/answer.git
Merge remote-tracking branch 'github/feat/1.1.0/report' into test
This commit is contained in:
commit
d06dcaff1d
|
@ -23,7 +23,6 @@ import (
|
|||
"github.com/answerdev/answer/internal/repo/captcha"
|
||||
"github.com/answerdev/answer/internal/repo/collection"
|
||||
"github.com/answerdev/answer/internal/repo/comment"
|
||||
"github.com/answerdev/answer/internal/repo/common"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/repo/export"
|
||||
"github.com/answerdev/answer/internal/repo/meta"
|
||||
|
@ -52,6 +51,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/service/collection_common"
|
||||
comment2 "github.com/answerdev/answer/internal/service/comment"
|
||||
"github.com/answerdev/answer/internal/service/comment_common"
|
||||
config2 "github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/internal/service/dashboard"
|
||||
export2 "github.com/answerdev/answer/internal/service/export"
|
||||
"github.com/answerdev/answer/internal/service/follow"
|
||||
|
@ -109,14 +109,15 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
langController := controller.NewLangController(i18nTranslator, siteInfoCommonService)
|
||||
authRepo := auth.NewAuthRepo(dataData)
|
||||
authService := auth2.NewAuthService(authRepo)
|
||||
configRepo := config.NewConfigRepo(dataData)
|
||||
userRepo := user.NewUserRepo(dataData, configRepo)
|
||||
userRepo := user.NewUserRepo(dataData)
|
||||
uniqueIDRepo := unique.NewUniqueIDRepo(dataData)
|
||||
activityRepo := activity_common.NewActivityRepo(dataData, uniqueIDRepo, configRepo)
|
||||
userRankRepo := rank.NewUserRankRepo(dataData, configRepo)
|
||||
userActiveActivityRepo := activity.NewUserActiveActivityRepo(dataData, activityRepo, userRankRepo, configRepo)
|
||||
configRepo := config.NewConfigRepo(dataData)
|
||||
configService := config2.NewConfigService(configRepo)
|
||||
activityRepo := activity_common.NewActivityRepo(dataData, uniqueIDRepo, configService)
|
||||
userRankRepo := rank.NewUserRankRepo(dataData, configService)
|
||||
userActiveActivityRepo := activity.NewUserActiveActivityRepo(dataData, activityRepo, userRankRepo, configService)
|
||||
emailRepo := export.NewEmailRepo(dataData)
|
||||
emailService := export2.NewEmailService(configRepo, emailRepo, siteInfoRepo)
|
||||
emailService := export2.NewEmailService(configService, emailRepo, siteInfoRepo)
|
||||
userRoleRelRepo := role.NewUserRoleRelRepo(dataData)
|
||||
roleRepo := role.NewRoleRepo(dataData)
|
||||
roleService := role2.NewRoleService(roleRepo)
|
||||
|
@ -144,13 +145,13 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
commentService := comment2.NewCommentService(commentRepo, commentCommonRepo, userCommon, objService, voteRepo, emailService, userRepo)
|
||||
rolePowerRelRepo := role.NewRolePowerRelRepo(dataData)
|
||||
rolePowerRelService := role2.NewRolePowerRelService(rolePowerRelRepo, userRoleRelService)
|
||||
rankService := rank2.NewRankService(userCommon, userRankRepo, objService, userRoleRelService, rolePowerRelService, configRepo)
|
||||
rankService := rank2.NewRankService(userCommon, userRankRepo, objService, userRoleRelService, rolePowerRelService, configService)
|
||||
commentController := controller.NewCommentController(commentService, rankService)
|
||||
reportRepo := report.NewReportRepo(dataData, uniqueIDRepo)
|
||||
reportService := report2.NewReportService(reportRepo, objService)
|
||||
reportController := controller.NewReportController(reportService, rankService)
|
||||
serviceVoteRepo := activity.NewVoteRepo(dataData, uniqueIDRepo, configRepo, activityRepo, userRankRepo, voteRepo)
|
||||
voteService := service.NewVoteService(serviceVoteRepo, uniqueIDRepo, configRepo, questionRepo, answerRepo, commentCommonRepo, objService)
|
||||
serviceVoteRepo := activity.NewVoteRepo(dataData, uniqueIDRepo, configService, activityRepo, userRankRepo, voteRepo)
|
||||
voteService := service.NewVoteService(serviceVoteRepo, uniqueIDRepo, configService, questionRepo, answerRepo, commentCommonRepo, objService)
|
||||
voteController := controller.NewVoteController(voteService, rankService)
|
||||
followRepo := activity_common.NewFollowRepo(dataData, uniqueIDRepo, activityRepo)
|
||||
tagService := tag2.NewTagService(tagRepo, tagCommonService, revisionService, followRepo, siteInfoCommonService)
|
||||
|
@ -164,7 +165,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
answerCommon := answercommon.NewAnswerCommon(answerRepo)
|
||||
metaRepo := meta.NewMetaRepo(dataData)
|
||||
metaService := meta2.NewMetaService(metaRepo)
|
||||
questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configRepo)
|
||||
questionCommon := questioncommon.NewQuestionCommon(questionRepo, answerRepo, voteRepo, followRepo, tagCommonService, userCommon, collectionCommon, answerCommon, metaService, configService)
|
||||
collectionService := service.NewCollectionService(collectionRepo, collectionGroupRepo, questionCommon)
|
||||
collectionController := controller.NewCollectionController(collectionService)
|
||||
answerActivityRepo := activity.NewAnswerActivityRepo(dataData, activityRepo, userRankRepo)
|
||||
|
@ -173,7 +174,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
questionService := service.NewQuestionService(questionRepo, tagCommonService, questionCommon, userCommon, userRepo, revisionService, metaService, collectionCommon, answerActivityService, dataData, emailService)
|
||||
answerService := service.NewAnswerService(answerRepo, questionRepo, questionCommon, userCommon, collectionCommon, userRepo, revisionService, answerActivityService, answerCommon, voteRepo, emailService, userRoleRelService)
|
||||
questionController := controller.NewQuestionController(questionService, answerService, rankService)
|
||||
dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configRepo, siteInfoCommonService, serviceConf, dataData)
|
||||
dashboardService := dashboard.NewDashboardService(questionRepo, answerRepo, commentCommonRepo, voteRepo, userRepo, reportRepo, configService, siteInfoCommonService, serviceConf, dataData)
|
||||
answerController := controller.NewAnswerController(answerService, rankService, dashboardService)
|
||||
searchParser := search_parser.NewSearchParser(tagCommonService, userCommon)
|
||||
searchRepo := search_common.NewSearchRepo(dataData, uniqueIDRepo, userCommon)
|
||||
|
@ -182,18 +183,17 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
serviceRevisionService := service.NewRevisionService(revisionRepo, userCommon, questionCommon, answerService, objService, questionRepo, answerRepo, tagRepo, tagCommonService)
|
||||
revisionController := controller.NewRevisionController(serviceRevisionService, rankService)
|
||||
rankController := controller.NewRankController(rankService)
|
||||
commonRepo := common.NewCommonRepo(dataData, uniqueIDRepo)
|
||||
reportHandle := report_handle_admin.NewReportHandle(questionCommon, commentRepo, configRepo)
|
||||
reportAdminService := report_admin.NewReportAdminService(reportRepo, userCommon, commonRepo, answerRepo, questionRepo, commentCommonRepo, reportHandle, configRepo)
|
||||
reportHandle := report_handle_admin.NewReportHandle(questionCommon, commentRepo, configService)
|
||||
reportAdminService := report_admin.NewReportAdminService(reportRepo, userCommon, answerRepo, questionRepo, commentCommonRepo, reportHandle, configService, objService)
|
||||
controller_adminReportController := controller_admin.NewReportController(reportAdminService)
|
||||
userAdminRepo := user.NewUserAdminRepo(dataData, authRepo)
|
||||
userAdminService := user_admin.NewUserAdminService(userAdminRepo, userRoleRelService, authService, userCommon, userActiveActivityRepo)
|
||||
userAdminController := controller_admin.NewUserAdminController(userAdminService)
|
||||
reasonRepo := reason.NewReasonRepo(configRepo)
|
||||
reasonRepo := reason.NewReasonRepo(configService)
|
||||
reasonService := reason2.NewReasonService(reasonRepo)
|
||||
reasonController := controller.NewReasonController(reasonService)
|
||||
themeController := controller_admin.NewThemeController()
|
||||
siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configRepo)
|
||||
siteInfoService := siteinfo.NewSiteInfoService(siteInfoRepo, siteInfoCommonService, emailService, tagCommonService, configService)
|
||||
siteInfoController := controller_admin.NewSiteInfoController(siteInfoService)
|
||||
siteinfoController := controller.NewSiteinfoController(siteInfoCommonService)
|
||||
notificationRepo := notification.NewNotificationRepo(dataData)
|
||||
|
@ -203,13 +203,13 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
|
|||
dashboardController := controller.NewDashboardController(dashboardService)
|
||||
uploadController := controller.NewUploadController(uploaderService)
|
||||
activityCommon := activity_common2.NewActivityCommon(activityRepo)
|
||||
activityActivityRepo := activity.NewActivityRepo(dataData)
|
||||
activityActivityRepo := activity.NewActivityRepo(dataData, configService)
|
||||
commentCommonService := comment_common.NewCommentCommonService(commentCommonRepo)
|
||||
activityService := activity2.NewActivityService(activityActivityRepo, userCommon, activityCommon, tagCommonService, objService, commentCommonService, revisionService, metaService)
|
||||
activityService := activity2.NewActivityService(activityActivityRepo, userCommon, activityCommon, tagCommonService, objService, commentCommonService, revisionService, metaService, configService)
|
||||
activityController := controller.NewActivityController(activityCommon, activityService)
|
||||
roleController := controller_admin.NewRoleController(roleService)
|
||||
pluginConfigRepo := plugin_config.NewPluginConfigRepo(dataData)
|
||||
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configRepo)
|
||||
pluginCommonService := plugin_common.NewPluginCommonService(pluginConfigRepo, configService)
|
||||
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, siteinfoController, notificationController, dashboardController, uploadController, activityController, roleController, pluginController, permissionController)
|
||||
|
|
4
go.sum
4
go.sum
|
@ -625,16 +625,12 @@ github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405 h1:2i
|
|||
github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405/go.mod h1:rIxVzVLKlBwLxO+lC+k/I4HJfRQcemg/f/76Xmmzsec=
|
||||
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
|
||||
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
|
||||
github.com/segmentfault/pacman v1.0.3 h1:/K8LJHQMiCaCIvC/e8GQITpYTEG6RH4KTLTZjPTghl4=
|
||||
github.com/segmentfault/pacman v1.0.3/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
|
||||
github.com/segmentfault/pacman v1.0.4 h1:6UIXuMHUeYMWe5toflV9SXZQizRny1RczjZJLj9kul0=
|
||||
github.com/segmentfault/pacman v1.0.4/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
|
||||
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs=
|
||||
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
|
||||
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk=
|
||||
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=
|
||||
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093245-f9384b820548 h1:R+FH23Qrdp5ECuHXmZy4BvoO/x7m2wZgNeiC46+jqCQ=
|
||||
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093245-f9384b820548/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E=
|
||||
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 h1:OEuW1D7RGDE0CZDr0oGMw9Eiq7fAbD9C4WMrvSixamk=
|
||||
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150/go.mod h1:7QcRmnV7OYq4hNOOCWXT5HXnN/u756JUsqIW0Bw8n9E=
|
||||
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05 h1:jcGZU2juv0L3eFEkuZYV14ESLUlWfGMWnP0mjOfrSZc=
|
||||
|
|
|
@ -359,6 +359,8 @@ backend:
|
|||
other: answered
|
||||
modified:
|
||||
other: modified
|
||||
deleted_title:
|
||||
other: Deleted question
|
||||
notification:
|
||||
action:
|
||||
update_question:
|
||||
|
@ -433,6 +435,19 @@ backend:
|
|||
other: "[{{.SiteName}}] Test Email"
|
||||
body:
|
||||
other: "This is a test email."
|
||||
action_activity_type:
|
||||
upvote:
|
||||
other: upvote
|
||||
upvoted:
|
||||
other: upvoted
|
||||
downvote:
|
||||
other: downvote
|
||||
downvoted:
|
||||
other: downvoted
|
||||
accept:
|
||||
other: accept
|
||||
accepted:
|
||||
other: accepted
|
||||
|
||||
# The following fields are used for interface presentation(Front-end)
|
||||
ui:
|
||||
|
|
|
@ -344,6 +344,8 @@ backend:
|
|||
other: 回答于
|
||||
modified:
|
||||
other: 修改于
|
||||
deleted_title:
|
||||
other: 问题已被删除
|
||||
notification:
|
||||
action:
|
||||
update_question:
|
||||
|
@ -418,6 +420,19 @@ backend:
|
|||
other: "[{{.SiteName}}] 测试邮件"
|
||||
body:
|
||||
other: "这是一封测试邮件。"
|
||||
action_activity_type:
|
||||
upvote:
|
||||
other: 点赞
|
||||
upvoted:
|
||||
other: 被赞
|
||||
downvote:
|
||||
other: 点踩
|
||||
downvoted:
|
||||
other: 被踩
|
||||
accept:
|
||||
other: 采纳
|
||||
accepted:
|
||||
other: 被采纳
|
||||
#The following fields are used for interface presentation(Front-end)
|
||||
ui:
|
||||
how_to_format:
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package constant
|
||||
|
||||
const (
|
||||
PluginStatus = "plugin.status"
|
||||
PluginStatus = "plugin.status"
|
||||
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
|
||||
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
|
||||
)
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package constant
|
||||
|
||||
const (
|
||||
DeletedQuestionTitleTrKey = "question.deleted_title"
|
||||
)
|
|
@ -27,7 +27,7 @@ func NewRankController(
|
|||
// @Param page query int false "page"
|
||||
// @Param page_size query int false "page size"
|
||||
// @Param username query string false "username"
|
||||
// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetRankPersonalWithPageResp}}
|
||||
// @Success 200 {object} handler.RespBody{data=pager.PageModel{list=[]schema.GetRankPersonalPageResp}}
|
||||
// @Router /answer/api/v1/personal/rank/page [get]
|
||||
func (cc *RankController) GetRankPersonalWithPage(ctx *gin.Context) {
|
||||
req := &schema.GetRankPersonalWithPageReq{}
|
||||
|
@ -37,6 +37,6 @@ func (cc *RankController) GetRankPersonalWithPage(ctx *gin.Context) {
|
|||
|
||||
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
|
||||
|
||||
resp, err := cc.rankService.GetRankPersonalWithPage(ctx, req)
|
||||
resp, err := cc.rankService.GetRankPersonalPage(ctx, req)
|
||||
handler.HandleResponse(ctx, err, resp)
|
||||
}
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package entity
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
|
||||
"github.com/answerdev/answer/pkg/converter"
|
||||
)
|
||||
|
||||
// Config config
|
||||
type Config struct {
|
||||
ID int `xorm:"not null pk autoincr INT(11) id"`
|
||||
|
@ -8,6 +14,36 @@ type Config struct {
|
|||
}
|
||||
|
||||
// TableName config table name
|
||||
func (Config) TableName() string {
|
||||
func (c *Config) TableName() string {
|
||||
return "config"
|
||||
}
|
||||
|
||||
func (c *Config) BuildByJSON(data []byte) {
|
||||
cf := &Config{}
|
||||
_ = json.Unmarshal(data, cf)
|
||||
c.ID = cf.ID
|
||||
c.Key = cf.Key
|
||||
c.Value = cf.Value
|
||||
}
|
||||
|
||||
func (c *Config) JsonString() string {
|
||||
data, _ := json.Marshal(c)
|
||||
return string(data)
|
||||
}
|
||||
|
||||
// GetIntValue get int value
|
||||
func (c *Config) GetIntValue() int {
|
||||
return converter.StringToInt(c.Value)
|
||||
}
|
||||
|
||||
// GetArrayStringValue get array string value
|
||||
func (c *Config) GetArrayStringValue() []string {
|
||||
var arr []string
|
||||
_ = json.Unmarshal([]byte(c.Value), &arr)
|
||||
return arr
|
||||
}
|
||||
|
||||
// GetByteValue get byte value
|
||||
func (c *Config) GetByteValue() []byte {
|
||||
return []byte(c.Value)
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ type QuestionPostTime struct {
|
|||
CreatedAt time.Time `xorm:"not null default CURRENT_TIMESTAMP TIMESTAMP created_at"`
|
||||
UpdatedAt time.Time `xorm:"updated_at TIMESTAMP"`
|
||||
UserID string `xorm:"not null default 0 BIGINT(20) INDEX user_id"`
|
||||
InviteUserID string `xorm:"TEXT invite_user_id"`
|
||||
LastEditUserID string `xorm:"not null default 0 BIGINT(20) last_edit_user_id"`
|
||||
Title string `xorm:"not null default '' VARCHAR(150) title"`
|
||||
OriginalText string `xorm:"not null MEDIUMTEXT original_text"`
|
||||
|
|
|
@ -2,48 +2,41 @@ package activity
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/base/data"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/service/activity"
|
||||
"github.com/answerdev/answer/internal/service/activity_type"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// activityRepo activity repository
|
||||
type activityRepo struct {
|
||||
data *data.Data
|
||||
data *data.Data
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
// NewActivityRepo new repository
|
||||
func NewActivityRepo(
|
||||
data *data.Data,
|
||||
configService *config.ConfigService,
|
||||
) activity.ActivityRepo {
|
||||
return &activityRepo{
|
||||
data: data,
|
||||
data: data,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
func (ar *activityRepo) GetObjectAllActivity(ctx context.Context, objectID string, showVote bool) (
|
||||
activityList []*entity.Activity, err error) {
|
||||
activityList = make([]*entity.Activity, 0)
|
||||
session := ar.data.DB.Desc("created_at")
|
||||
session := ar.data.DB.Context(ctx).Desc("created_at")
|
||||
|
||||
if !showVote {
|
||||
var activityTypeNotShown []int
|
||||
for _, obj := range []string{constant.AnswerObjectType, constant.QuestionObjectType, constant.CommentObjectType} {
|
||||
for _, act := range []string{
|
||||
constant.ActVotedDown,
|
||||
constant.ActVotedUp,
|
||||
constant.ActVoteDown,
|
||||
constant.ActVoteUp,
|
||||
} {
|
||||
activityTypeNotShown = append(activityTypeNotShown, config.Key2IDMapping[fmt.Sprintf("%s.%s", obj, act)])
|
||||
}
|
||||
}
|
||||
activityTypeNotShown := ar.getAllActivityType(ctx)
|
||||
session.NotIn("activity_type", activityTypeNotShown)
|
||||
}
|
||||
err = session.Find(&activityList, &entity.Activity{OriginalObjectID: objectID})
|
||||
|
@ -52,3 +45,16 @@ func (ar *activityRepo) GetObjectAllActivity(ctx context.Context, objectID strin
|
|||
}
|
||||
return activityList, nil
|
||||
}
|
||||
|
||||
func (ar *activityRepo) getAllActivityType(ctx context.Context) (activityTypes []int) {
|
||||
var activityTypeNotShown []int
|
||||
for _, key := range activity_type.VoteActivityTypeList {
|
||||
id, err := ar.configService.GetIDByKey(ctx, key)
|
||||
if err != nil {
|
||||
log.Errorf("get config id by key [%s] error: %v", key, err)
|
||||
} else {
|
||||
activityTypeNotShown = append(activityTypeNotShown, id)
|
||||
}
|
||||
}
|
||||
return activityTypeNotShown
|
||||
}
|
||||
|
|
|
@ -63,7 +63,7 @@ func NewQuestionActivityRepo(
|
|||
|
||||
func (ar *AnswerActivityRepo) DeleteQuestion(ctx context.Context, questionID string) (err error) {
|
||||
questionInfo := &entity.Question{}
|
||||
exist, err := ar.data.DB.Where("id = ?", questionID).Get(questionInfo)
|
||||
exist, err := ar.data.DB.Context(ctx).Where("id = ?", questionID).Get(questionInfo)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ func (ar *AnswerActivityRepo) DeleteQuestion(ctx context.Context, questionID str
|
|||
|
||||
// get all this object activity
|
||||
activityList := make([]*entity.Activity, 0)
|
||||
session := ar.data.DB.Where("has_rank = 1")
|
||||
session := ar.data.DB.Context(ctx).Where("has_rank = 1")
|
||||
session.Where("cancelled = ?", entity.ActivityAvailable)
|
||||
err = session.Find(&activityList, &entity.Activity{ObjectID: questionID})
|
||||
if err != nil {
|
||||
|
@ -86,6 +86,7 @@ func (ar *AnswerActivityRepo) DeleteQuestion(ctx context.Context, questionID str
|
|||
log.Infof("questionInfo %s deleted will rollback activity %d", questionID, len(activityList))
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
for _, act := range activityList {
|
||||
log.Infof("user %s rollback rank %d", act.UserID, -act.Rank)
|
||||
_, e := ar.userRankRepo.TriggerUserRank(
|
||||
|
@ -107,7 +108,7 @@ func (ar *AnswerActivityRepo) DeleteQuestion(ctx context.Context, questionID str
|
|||
|
||||
// get all answers
|
||||
answerList := make([]*entity.Answer, 0)
|
||||
err = ar.data.DB.Find(&answerList, &entity.Answer{QuestionID: questionID})
|
||||
err = ar.data.DB.Context(ctx).Find(&answerList, &entity.Answer{QuestionID: questionID})
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -154,6 +155,7 @@ func (ar *AnswerActivityRepo) AcceptAnswer(ctx context.Context,
|
|||
}
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
for _, addActivity := range addActivityList {
|
||||
existsActivity, exists, e := ar.activityRepo.GetActivity(
|
||||
ctx, session, answerObjID, addActivity.UserID, addActivity.ActivityType)
|
||||
|
@ -251,6 +253,7 @@ func (ar *AnswerActivityRepo) CancelAcceptAnswer(ctx context.Context,
|
|||
}
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
for _, addActivity := range addActivityList {
|
||||
existsActivity, exists, e := ar.activityRepo.GetActivity(
|
||||
ctx, session, answerObjID, addActivity.UserID, addActivity.ActivityType)
|
||||
|
@ -300,7 +303,7 @@ func (ar *AnswerActivityRepo) CancelAcceptAnswer(ctx context.Context,
|
|||
|
||||
func (ar *AnswerActivityRepo) DeleteAnswer(ctx context.Context, answerID string) (err error) {
|
||||
answerInfo := &entity.Answer{}
|
||||
exist, err := ar.data.DB.Where("id = ?", answerID).Get(answerInfo)
|
||||
exist, err := ar.data.DB.Context(ctx).Where("id = ?", answerID).Get(answerInfo)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -310,7 +313,7 @@ func (ar *AnswerActivityRepo) DeleteAnswer(ctx context.Context, answerID string)
|
|||
|
||||
// get all this object activity
|
||||
activityList := make([]*entity.Activity, 0)
|
||||
session := ar.data.DB.Where("has_rank = 1")
|
||||
session := ar.data.DB.Context(ctx).Where("has_rank = 1")
|
||||
session.Where("cancelled = ?", entity.ActivityAvailable)
|
||||
err = session.Find(&activityList, &entity.Activity{ObjectID: answerID})
|
||||
if err != nil {
|
||||
|
@ -323,6 +326,7 @@ func (ar *AnswerActivityRepo) DeleteAnswer(ctx context.Context, answerID string)
|
|||
log.Infof("answerInfo %s deleted will rollback activity %d", answerID, len(activityList))
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
for _, act := range activityList {
|
||||
log.Infof("user %s rollback rank %d", act.UserID, -act.Rank)
|
||||
_, e := ar.userRankRepo.TriggerUserRank(
|
||||
|
|
|
@ -39,12 +39,17 @@ func NewFollowRepo(
|
|||
}
|
||||
|
||||
func (ar *FollowRepo) Follow(ctx context.Context, objectID, userID string) error {
|
||||
activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(ctx, objectID, "follow")
|
||||
objectTypeStr, err := obj.GetObjectTypeStrByObjectID(objectID)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectTypeStr, "follow")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
var (
|
||||
existsActivity entity.Activity
|
||||
has bool
|
||||
|
@ -101,12 +106,17 @@ func (ar *FollowRepo) Follow(ctx context.Context, objectID, userID string) error
|
|||
}
|
||||
|
||||
func (ar *FollowRepo) FollowCancel(ctx context.Context, objectID, userID string) error {
|
||||
activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(ctx, objectID, "follow")
|
||||
objectTypeStr, err := obj.GetObjectTypeStrByObjectID(objectID)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectTypeStr, "follow")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
var (
|
||||
existsActivity entity.Activity
|
||||
has bool
|
||||
|
|
|
@ -16,10 +16,10 @@ import (
|
|||
|
||||
// UserActiveActivityRepo answer accepted
|
||||
type UserActiveActivityRepo struct {
|
||||
data *data.Data
|
||||
activityRepo activity_common.ActivityRepo
|
||||
userRankRepo rank.UserRankRepo
|
||||
configRepo config.ConfigRepo
|
||||
data *data.Data
|
||||
activityRepo activity_common.ActivityRepo
|
||||
userRankRepo rank.UserRankRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
const (
|
||||
|
@ -31,37 +31,36 @@ func NewUserActiveActivityRepo(
|
|||
data *data.Data,
|
||||
activityRepo activity_common.ActivityRepo,
|
||||
userRankRepo rank.UserRankRepo,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
) activity.UserActiveActivityRepo {
|
||||
return &UserActiveActivityRepo{
|
||||
data: data,
|
||||
activityRepo: activityRepo,
|
||||
userRankRepo: userRankRepo,
|
||||
configRepo: configRepo,
|
||||
data: data,
|
||||
activityRepo: activityRepo,
|
||||
userRankRepo: userRankRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
// UserActive accept other answer
|
||||
func (ar *UserActiveActivityRepo) UserActive(ctx context.Context, userID string) (err error) {
|
||||
cfg, err := ar.configService.GetConfigByKey(ctx, UserActivated)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
activityType := cfg.ID
|
||||
deltaRank := cfg.GetIntValue()
|
||||
addActivity := &entity.Activity{
|
||||
UserID: userID,
|
||||
ObjectID: "0",
|
||||
OriginalObjectID: "0",
|
||||
ActivityType: activityType,
|
||||
Rank: deltaRank,
|
||||
HasRank: 1,
|
||||
}
|
||||
|
||||
_, err = ar.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
|
||||
activityType, err := ar.configRepo.GetConfigType(UserActivated)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
deltaRank, err := ar.configRepo.GetInt(UserActivated)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
addActivity := &entity.Activity{
|
||||
UserID: userID,
|
||||
ObjectID: "0",
|
||||
OriginalObjectID: "0",
|
||||
ActivityType: activityType,
|
||||
Rank: deltaRank,
|
||||
HasRank: 1,
|
||||
}
|
||||
_, exists, err := ar.activityRepo.GetActivity(ctx, session, "0", addActivity.UserID, activityType)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
|
|
@ -30,30 +30,30 @@ import (
|
|||
|
||||
// VoteRepo activity repository
|
||||
type VoteRepo struct {
|
||||
data *data.Data
|
||||
uniqueIDRepo unique.UniqueIDRepo
|
||||
configRepo config.ConfigRepo
|
||||
activityRepo activity_common.ActivityRepo
|
||||
userRankRepo rank.UserRankRepo
|
||||
voteCommon activity_common.VoteRepo
|
||||
data *data.Data
|
||||
uniqueIDRepo unique.UniqueIDRepo
|
||||
configService *config.ConfigService
|
||||
activityRepo activity_common.ActivityRepo
|
||||
userRankRepo rank.UserRankRepo
|
||||
voteCommon activity_common.VoteRepo
|
||||
}
|
||||
|
||||
// NewVoteRepo new repository
|
||||
func NewVoteRepo(
|
||||
data *data.Data,
|
||||
uniqueIDRepo unique.UniqueIDRepo,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
activityRepo activity_common.ActivityRepo,
|
||||
userRankRepo rank.UserRankRepo,
|
||||
voteCommon activity_common.VoteRepo,
|
||||
) service.VoteRepo {
|
||||
return &VoteRepo{
|
||||
data: data,
|
||||
uniqueIDRepo: uniqueIDRepo,
|
||||
configRepo: configRepo,
|
||||
activityRepo: activityRepo,
|
||||
userRankRepo: userRankRepo,
|
||||
voteCommon: voteCommon,
|
||||
data: data,
|
||||
uniqueIDRepo: uniqueIDRepo,
|
||||
configService: configService,
|
||||
activityRepo: activityRepo,
|
||||
userRankRepo: userRankRepo,
|
||||
voteCommon: voteCommon,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -75,6 +75,7 @@ func (vr *VoteRepo) vote(ctx context.Context, objectID string, userID, objectUse
|
|||
sendInboxNotification := false
|
||||
upVote := false
|
||||
_, err = vr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
result = nil
|
||||
for _, action := range actions {
|
||||
var (
|
||||
|
@ -185,6 +186,7 @@ func (vr *VoteRepo) voteCancel(ctx context.Context, objectID string, userID, obj
|
|||
resp = &schema.VoteResp{}
|
||||
notificationUserIDs := make([]string, 0)
|
||||
_, err = vr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
for _, action := range actions {
|
||||
var (
|
||||
existsActivity entity.Activity
|
||||
|
@ -362,7 +364,7 @@ func (vr *VoteRepo) GetVoteResultByObjectId(ctx context.Context, objectID string
|
|||
|
||||
activityType, _, _, _ = vr.activityRepo.GetActivityTypeByObjID(ctx, objectID, action)
|
||||
|
||||
votes, err = vr.data.DB.Where(builder.Eq{"object_id": objectID}).
|
||||
votes, err = vr.data.DB.Context(ctx).Where(builder.Eq{"object_id": objectID}).
|
||||
And(builder.Eq{"activity_type": activityType}).
|
||||
And(builder.Eq{"cancelled": 0}).
|
||||
Count(&activity)
|
||||
|
@ -389,7 +391,7 @@ func (vr *VoteRepo) ListUserVotes(
|
|||
req schema.GetVoteWithPageReq,
|
||||
activityTypes []int,
|
||||
) (voteList []entity.Activity, total int64, err error) {
|
||||
session := vr.data.DB.NewSession()
|
||||
session := vr.data.DB.Context(ctx)
|
||||
cond := builder.
|
||||
And(
|
||||
builder.Eq{"user_id": userID},
|
||||
|
|
|
@ -21,34 +21,37 @@ import (
|
|||
|
||||
// ActivityRepo activity repository
|
||||
type ActivityRepo struct {
|
||||
data *data.Data
|
||||
uniqueIDRepo unique.UniqueIDRepo
|
||||
configRepo config.ConfigRepo
|
||||
data *data.Data
|
||||
uniqueIDRepo unique.UniqueIDRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
// NewActivityRepo new repository
|
||||
func NewActivityRepo(
|
||||
data *data.Data,
|
||||
uniqueIDRepo unique.UniqueIDRepo,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
) activity_common.ActivityRepo {
|
||||
return &ActivityRepo{
|
||||
data: data,
|
||||
uniqueIDRepo: uniqueIDRepo,
|
||||
configRepo: configRepo,
|
||||
data: data,
|
||||
uniqueIDRepo: uniqueIDRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
func (ar *ActivityRepo) GetActivityTypeByObjID(ctx context.Context, objectID string, action string) (activityType, rank, hasRank int, err error) {
|
||||
func (ar *ActivityRepo) GetActivityTypeByObjID(ctx context.Context, objectID string, action string) (
|
||||
activityType, rank, hasRank int, err error) {
|
||||
objectKey, err := obj.GetObjectTypeStrByObjectID(objectID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
confKey := fmt.Sprintf("%s.%s", objectKey, action)
|
||||
activityType, _ = ar.configRepo.GetConfigType(confKey)
|
||||
|
||||
rank, err = ar.configRepo.GetInt(confKey)
|
||||
cfg, err := ar.configService.GetConfigByKey(ctx, confKey)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
activityType, rank = cfg.ID, cfg.GetIntValue()
|
||||
hasRank = 0
|
||||
if rank != 0 {
|
||||
hasRank = 1
|
||||
|
@ -57,20 +60,20 @@ func (ar *ActivityRepo) GetActivityTypeByObjID(ctx context.Context, objectID str
|
|||
}
|
||||
|
||||
func (ar *ActivityRepo) GetActivityTypeByObjKey(ctx context.Context, objectKey, action string) (activityType int, err error) {
|
||||
confKey := fmt.Sprintf("%s.%s", objectKey, action)
|
||||
activityType, err = ar.configRepo.GetConfigType(confKey)
|
||||
configKey := fmt.Sprintf("%s.%s", objectKey, action)
|
||||
cfg, err := ar.configService.GetConfigByKey(ctx, configKey)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
return cfg.ID, nil
|
||||
}
|
||||
|
||||
func (ar *ActivityRepo) GetActivityTypeByConfigKey(ctx context.Context, configKey string) (activityType int, err error) {
|
||||
activityType, err = ar.configRepo.GetConfigType(configKey)
|
||||
cfg, err := ar.configService.GetConfigByKey(ctx, configKey)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
return cfg.ID, nil
|
||||
}
|
||||
|
||||
func (ar *ActivityRepo) GetActivity(ctx context.Context, session *xorm.Session,
|
||||
|
@ -87,7 +90,7 @@ func (ar *ActivityRepo) GetActivity(ctx context.Context, session *xorm.Session,
|
|||
|
||||
func (ar *ActivityRepo) GetUserIDObjectIDActivitySum(ctx context.Context, userID, objectID string) (int, error) {
|
||||
sum := &entity.ActivityRankSum{}
|
||||
_, err := ar.data.DB.Table(entity.Activity{}.TableName()).
|
||||
_, err := ar.data.DB.Context(ctx).Table(entity.Activity{}.TableName()).
|
||||
Select("sum(`rank`) as `rank`").
|
||||
Where("user_id =?", userID).
|
||||
And("object_id = ?", objectID).
|
||||
|
@ -102,7 +105,7 @@ func (ar *ActivityRepo) GetUserIDObjectIDActivitySum(ctx context.Context, userID
|
|||
|
||||
// AddActivity add activity
|
||||
func (ar *ActivityRepo) AddActivity(ctx context.Context, activity *entity.Activity) (err error) {
|
||||
_, err = ar.data.DB.Insert(activity)
|
||||
_, err = ar.data.DB.Context(ctx).Insert(activity)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -113,7 +116,7 @@ func (ar *ActivityRepo) AddActivity(ctx context.Context, activity *entity.Activi
|
|||
func (ar *ActivityRepo) GetUsersWhoHasGainedTheMostReputation(
|
||||
ctx context.Context, startTime, endTime time.Time, limit int) (rankStat []*entity.ActivityUserRankStat, err error) {
|
||||
rankStat = make([]*entity.ActivityUserRankStat, 0)
|
||||
session := ar.data.DB.Select("user_id, SUM(`rank`) AS rank_amount").Table("activity")
|
||||
session := ar.data.DB.Context(ctx).Select("user_id, SUM(`rank`) AS rank_amount").Table("activity")
|
||||
session.Where("has_rank = 1 AND cancelled = 0")
|
||||
session.Where("created_at >= ?", startTime)
|
||||
session.Where("created_at <= ?", endTime)
|
||||
|
@ -134,13 +137,13 @@ func (ar *ActivityRepo) GetUsersWhoHasVoteMost(
|
|||
|
||||
actIDs := make([]int, 0)
|
||||
for _, act := range activity_type.ActivityTypeList {
|
||||
configType, err := ar.configRepo.GetConfigType(act)
|
||||
cfg, err := ar.configService.GetConfigByKey(ctx, act)
|
||||
if err == nil {
|
||||
actIDs = append(actIDs, configType)
|
||||
actIDs = append(actIDs, cfg.ID)
|
||||
}
|
||||
}
|
||||
|
||||
session := ar.data.DB.Select("user_id, COUNT(*) AS vote_count").Table("activity")
|
||||
session := ar.data.DB.Context(ctx).Select("user_id, COUNT(*) AS vote_count").Table("activity")
|
||||
session.Where("cancelled = 0")
|
||||
session.In("activity_type", actIDs)
|
||||
session.Where("created_at >= ?", startTime)
|
||||
|
|
|
@ -41,19 +41,19 @@ func (ar *FollowRepo) GetFollowAmount(ctx context.Context, objectID string) (fol
|
|||
switch objectType {
|
||||
case "question":
|
||||
model := &entity.Question{}
|
||||
_, err = ar.data.DB.Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
if err == nil {
|
||||
follows = int(model.FollowCount)
|
||||
}
|
||||
case "user":
|
||||
model := &entity.User{}
|
||||
_, err = ar.data.DB.Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
if err == nil {
|
||||
follows = int(model.FollowCount)
|
||||
}
|
||||
case "tag":
|
||||
model := &entity.Tag{}
|
||||
_, err = ar.data.DB.Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id = ?", objectID).Cols("`follow_count`").Get(model)
|
||||
if err == nil {
|
||||
follows = int(model.FollowCount)
|
||||
}
|
||||
|
@ -79,7 +79,7 @@ func (ar *FollowRepo) GetFollowUserIDs(ctx context.Context, objectID string) (us
|
|||
}
|
||||
|
||||
userIDs = make([]string, 0)
|
||||
session := ar.data.DB.Select("user_id")
|
||||
session := ar.data.DB.Context(ctx).Select("user_id")
|
||||
session.Table(entity.Activity{}.TableName())
|
||||
session.Where("object_id = ?", objectID)
|
||||
session.Where("activity_type = ?", activityType)
|
||||
|
@ -98,7 +98,7 @@ func (ar *FollowRepo) GetFollowIDs(ctx context.Context, userID, objectKey string
|
|||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
session := ar.data.DB.Select("object_id")
|
||||
session := ar.data.DB.Context(ctx).Select("object_id")
|
||||
session.Table(entity.Activity{}.TableName())
|
||||
session.Where("user_id = ? AND activity_type = ?", userID, activityType)
|
||||
session.Where("cancelled = 0")
|
||||
|
@ -110,14 +110,19 @@ func (ar *FollowRepo) GetFollowIDs(ctx context.Context, userID, objectKey string
|
|||
}
|
||||
|
||||
// IsFollowed check user if follow object or not
|
||||
func (ar *FollowRepo) IsFollowed(userID, objectID string) (bool, error) {
|
||||
activityType, _, _, err := ar.activityRepo.GetActivityTypeByObjID(context.TODO(), objectID, "follow")
|
||||
func (ar *FollowRepo) IsFollowed(ctx context.Context, userID, objectID string) (followed bool, err error) {
|
||||
objectKey, err := obj.GetObjectTypeStrByObjectID(objectID)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
activityType, err := ar.activityRepo.GetActivityTypeByObjKey(ctx, objectKey, "follow")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
at := &entity.Activity{}
|
||||
has, err := ar.data.DB.Where("user_id = ? AND object_id = ? AND activity_type = ?", userID, objectID, activityType).Get(at)
|
||||
has, err := ar.data.DB.Context(ctx).Where("user_id = ? AND object_id = ? AND activity_type = ?", userID, objectID, activityType).Get(at)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
|
|
@ -8,6 +8,7 @@ import (
|
|||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/service/activity_common"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// VoteRepo activity repository
|
||||
|
@ -31,8 +32,9 @@ func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string)
|
|||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
has, err := vr.data.DB.Where("object_id =? AND cancelled=0 AND activity_type=? AND user_id=?", objectID, activityType, userID).Get(at)
|
||||
has, err := vr.data.DB.Context(ctx).Where("object_id =? AND cancelled=0 AND activity_type=? AND user_id=?", objectID, activityType, userID).Get(at)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return ""
|
||||
}
|
||||
if has {
|
||||
|
@ -44,7 +46,7 @@ func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string)
|
|||
|
||||
func (vr *VoteRepo) GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error) {
|
||||
list := make([]*entity.Activity, 0)
|
||||
count, err = vr.data.DB.Where("cancelled =0").In("activity_type", activityTypes).FindAndCount(&list)
|
||||
count, err = vr.data.DB.Context(ctx).Where("cancelled =0").In("activity_type", activityTypes).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -53,7 +53,7 @@ func (ar *answerRepo) AddAnswer(ctx context.Context, answer *entity.Answer) (err
|
|||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
answer.ID = ID
|
||||
_, err = ar.data.DB.Insert(answer)
|
||||
_, err = ar.data.DB.Context(ctx).Insert(answer)
|
||||
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -70,7 +70,7 @@ func (ar *answerRepo) RemoveAnswer(ctx context.Context, id string) (err error) {
|
|||
ID: id,
|
||||
Status: entity.AnswerStatusDeleted,
|
||||
}
|
||||
_, err = ar.data.DB.Where("id = ?", id).Cols("status").Update(answer)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id = ?", id).Cols("status").Update(answer)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ func (ar *answerRepo) RemoveAnswer(ctx context.Context, id string) (err error) {
|
|||
func (ar *answerRepo) UpdateAnswer(ctx context.Context, answer *entity.Answer, Colar []string) (err error) {
|
||||
answer.ID = uid.DeShortID(answer.ID)
|
||||
answer.QuestionID = uid.DeShortID(answer.QuestionID)
|
||||
_, err = ar.data.DB.ID(answer.ID).Cols(Colar...).Update(answer)
|
||||
_, err = ar.data.DB.Context(ctx).ID(answer.ID).Cols(Colar...).Update(answer)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ func (ar *answerRepo) UpdateAnswerStatus(ctx context.Context, answer *entity.Ans
|
|||
now := time.Now()
|
||||
answer.ID = uid.DeShortID(answer.ID)
|
||||
answer.UpdatedAt = now
|
||||
_, err = ar.data.DB.Where("id =?", answer.ID).Cols("status", "updated_at").Update(answer)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id =?", answer.ID).Cols("status", "updated_at").Update(answer)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -105,7 +105,7 @@ func (ar *answerRepo) GetAnswer(ctx context.Context, id string) (
|
|||
) {
|
||||
id = uid.DeShortID(id)
|
||||
answer = &entity.Answer{}
|
||||
exist, err = ar.data.DB.ID(id).Get(answer)
|
||||
exist, err = ar.data.DB.Context(ctx).ID(id).Get(answer)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ func (ar *answerRepo) GetAnswer(ctx context.Context, id string) (
|
|||
// GetQuestionCount
|
||||
func (ar *answerRepo) GetAnswerCount(ctx context.Context) (count int64, err error) {
|
||||
list := make([]*entity.Answer, 0)
|
||||
count, err = ar.data.DB.Where("status = ?", entity.AnswerStatusAvailable).FindAndCount(&list)
|
||||
count, err = ar.data.DB.Context(ctx).Where("status = ?", entity.AnswerStatusAvailable).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -130,7 +130,7 @@ func (ar *answerRepo) GetAnswerList(ctx context.Context, answer *entity.Answer)
|
|||
answerList = make([]*entity.Answer, 0)
|
||||
answer.ID = uid.DeShortID(answer.ID)
|
||||
answer.QuestionID = uid.DeShortID(answer.QuestionID)
|
||||
err = ar.data.DB.Find(answerList, answer)
|
||||
err = ar.data.DB.Context(ctx).Find(answerList, answer)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -146,7 +146,7 @@ func (ar *answerRepo) GetAnswerPage(ctx context.Context, page, pageSize int, ans
|
|||
answer.ID = uid.DeShortID(answer.ID)
|
||||
answer.QuestionID = uid.DeShortID(answer.QuestionID)
|
||||
answerList = make([]*entity.Answer, 0)
|
||||
total, err = pager.Help(page, pageSize, answerList, answer, ar.data.DB.NewSession())
|
||||
total, err = pager.Help(page, pageSize, answerList, answer, ar.data.DB.Context(ctx))
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -169,13 +169,13 @@ func (ar *answerRepo) UpdateAccepted(ctx context.Context, id string, questionID
|
|||
data.ID = id
|
||||
|
||||
data.Accepted = schema.AnswerAcceptedFailed
|
||||
_, err := ar.data.DB.Where("question_id =?", questionID).Cols("adopted").Update(&data)
|
||||
_, err := ar.data.DB.Context(ctx).Where("question_id =?", questionID).Cols("adopted").Update(&data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if id != "0" {
|
||||
data.Accepted = schema.AnswerAcceptedEnable
|
||||
_, err = ar.data.DB.Where("id = ?", id).Cols("adopted").Update(&data)
|
||||
_, err = ar.data.DB.Context(ctx).Where("id = ?", id).Cols("adopted").Update(&data)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -187,7 +187,7 @@ func (ar *answerRepo) UpdateAccepted(ctx context.Context, id string, questionID
|
|||
func (ar *answerRepo) GetByID(ctx context.Context, id string) (*entity.Answer, bool, error) {
|
||||
var resp entity.Answer
|
||||
id = uid.DeShortID(id)
|
||||
has, err := ar.data.DB.Where("id =? ", id).Get(&resp)
|
||||
has, err := ar.data.DB.Context(ctx).Where("id =? ", id).Get(&resp)
|
||||
if err != nil {
|
||||
return &resp, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ func (ar *answerRepo) GetByID(ctx context.Context, id string) (*entity.Answer, b
|
|||
func (ar *answerRepo) GetByUserIDQuestionID(ctx context.Context, userID string, questionID string) (*entity.Answer, bool, error) {
|
||||
questionID = uid.DeShortID(questionID)
|
||||
var resp entity.Answer
|
||||
has, err := ar.data.DB.Where("question_id =? and user_id = ?", questionID, userID).Get(&resp)
|
||||
has, err := ar.data.DB.Context(ctx).Where("question_id =? and user_id = ?", questionID, userID).Get(&resp)
|
||||
if err != nil {
|
||||
return &resp, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -226,7 +226,7 @@ func (ar *answerRepo) SearchList(ctx context.Context, search *entity.AnswerSearc
|
|||
search.PageSize = constant.DefaultPageSize
|
||||
}
|
||||
offset := search.Page * search.PageSize
|
||||
session := ar.data.DB.Where("")
|
||||
session := ar.data.DB.Context(ctx).Where("")
|
||||
|
||||
if search.QuestionID != "" {
|
||||
session = session.And("question_id = ?", search.QuestionID)
|
||||
|
@ -262,7 +262,7 @@ func (ar *answerRepo) AdminSearchList(ctx context.Context, search *entity.AdminA
|
|||
var (
|
||||
count int64
|
||||
err error
|
||||
session = ar.data.DB.Table([]string{entity.Answer{}.TableName(), "a"}).Select("a.*")
|
||||
session = ar.data.DB.Context(ctx).Table([]string{entity.Answer{}.TableName(), "a"}).Select("a.*")
|
||||
)
|
||||
if search.QuestionID != "" {
|
||||
search.QuestionID = uid.DeShortID(search.QuestionID)
|
||||
|
|
|
@ -26,7 +26,7 @@ func NewCollectionGroupRepo(data *data.Data) service.CollectionGroupRepo {
|
|||
|
||||
// AddCollectionGroup add collection group
|
||||
func (cr *collectionGroupRepo) AddCollectionGroup(ctx context.Context, collectionGroup *entity.CollectionGroup) (err error) {
|
||||
_, err = cr.data.DB.Insert(collectionGroup)
|
||||
_, err = cr.data.DB.Context(ctx).Insert(collectionGroup)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (cr *collectionGroupRepo) AddCollectionDefaultGroup(ctx context.Context, us
|
|||
DefaultGroup: schema.CGDefault,
|
||||
UserID: userID,
|
||||
}
|
||||
_, err = cr.data.DB.Insert(defaultGroup)
|
||||
_, err = cr.data.DB.Context(ctx).Insert(defaultGroup)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return
|
||||
|
@ -51,7 +51,7 @@ func (cr *collectionGroupRepo) AddCollectionDefaultGroup(ctx context.Context, us
|
|||
|
||||
// UpdateCollectionGroup update collection group
|
||||
func (cr *collectionGroupRepo) UpdateCollectionGroup(ctx context.Context, collectionGroup *entity.CollectionGroup, cols []string) (err error) {
|
||||
_, err = cr.data.DB.ID(collectionGroup.ID).Cols(cols...).Update(collectionGroup)
|
||||
_, err = cr.data.DB.Context(ctx).ID(collectionGroup.ID).Cols(cols...).Update(collectionGroup)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (cr *collectionGroupRepo) GetCollectionGroup(ctx context.Context, id string
|
|||
collectionGroup *entity.CollectionGroup, exist bool, err error,
|
||||
) {
|
||||
collectionGroup = &entity.CollectionGroup{}
|
||||
exist, err = cr.data.DB.ID(id).Get(collectionGroup)
|
||||
exist, err = cr.data.DB.Context(ctx).ID(id).Get(collectionGroup)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -74,7 +74,7 @@ func (cr *collectionGroupRepo) GetCollectionGroup(ctx context.Context, id string
|
|||
func (cr *collectionGroupRepo) GetCollectionGroupPage(ctx context.Context, page, pageSize int, collectionGroup *entity.CollectionGroup) (collectionGroupList []*entity.CollectionGroup, total int64, err error) {
|
||||
collectionGroupList = make([]*entity.CollectionGroup, 0)
|
||||
|
||||
session := cr.data.DB.NewSession()
|
||||
session := cr.data.DB.Context(ctx)
|
||||
if collectionGroup.UserID != "" && collectionGroup.UserID != "0" {
|
||||
session = session.Where("user_id = ?", collectionGroup.UserID)
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ func (cr *collectionGroupRepo) GetCollectionGroupPage(ctx context.Context, page,
|
|||
|
||||
func (cr *collectionGroupRepo) GetDefaultID(ctx context.Context, userID string) (collectionGroup *entity.CollectionGroup, has bool, err error) {
|
||||
collectionGroup = &entity.CollectionGroup{}
|
||||
has, err = cr.data.DB.Where("user_id =? and default_group = ?", userID, schema.CGDefault).Get(collectionGroup)
|
||||
has, err = cr.data.DB.Context(ctx).Where("user_id =? and default_group = ?", userID, schema.CGDefault).Get(collectionGroup)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return
|
||||
|
|
|
@ -32,6 +32,7 @@ func NewCollectionRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo) collec
|
|||
func (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.Collection) (err error) {
|
||||
needAdd := false
|
||||
_, err = cr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {
|
||||
session = session.Context(ctx)
|
||||
var has bool
|
||||
dbcollection := &entity.Collection{}
|
||||
result = nil
|
||||
|
@ -52,7 +53,7 @@ func (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.
|
|||
id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())
|
||||
if err == nil {
|
||||
collection.ID = id
|
||||
_, err = cr.data.DB.Insert(collection)
|
||||
_, err = cr.data.DB.Context(ctx).Insert(collection)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -64,7 +65,7 @@ func (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.
|
|||
|
||||
// RemoveCollection delete collection
|
||||
func (cr *collectionRepo) RemoveCollection(ctx context.Context, id string) (err error) {
|
||||
_, err = cr.data.DB.Where("id =?", id).Delete(&entity.Collection{})
|
||||
_, err = cr.data.DB.Context(ctx).Where("id =?", id).Delete(&entity.Collection{})
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -73,14 +74,14 @@ func (cr *collectionRepo) RemoveCollection(ctx context.Context, id string) (err
|
|||
|
||||
// UpdateCollection update collection
|
||||
func (cr *collectionRepo) UpdateCollection(ctx context.Context, collection *entity.Collection, cols []string) (err error) {
|
||||
_, err = cr.data.DB.ID(collection.ID).Cols(cols...).Update(collection)
|
||||
_, err = cr.data.DB.Context(ctx).ID(collection.ID).Cols(cols...).Update(collection)
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
||||
// GetCollection get collection one
|
||||
func (cr *collectionRepo) GetCollection(ctx context.Context, id int) (collection *entity.Collection, exist bool, err error) {
|
||||
collection = &entity.Collection{}
|
||||
exist, err = cr.data.DB.ID(id).Get(collection)
|
||||
exist, err = cr.data.DB.Context(ctx).ID(id).Get(collection)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -90,7 +91,7 @@ func (cr *collectionRepo) GetCollection(ctx context.Context, id int) (collection
|
|||
// GetCollectionList get collection list all
|
||||
func (cr *collectionRepo) GetCollectionList(ctx context.Context, collection *entity.Collection) (collectionList []*entity.Collection, err error) {
|
||||
collectionList = make([]*entity.Collection, 0)
|
||||
err = cr.data.DB.Find(collectionList, collection)
|
||||
err = cr.data.DB.Context(ctx).Find(collectionList, collection)
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return
|
||||
}
|
||||
|
@ -98,7 +99,7 @@ func (cr *collectionRepo) GetCollectionList(ctx context.Context, collection *ent
|
|||
// GetOneByObjectIDAndUser get one by object TagID and user
|
||||
func (cr *collectionRepo) GetOneByObjectIDAndUser(ctx context.Context, userID string, objectID string) (collection *entity.Collection, exist bool, err error) {
|
||||
collection = &entity.Collection{}
|
||||
exist, err = cr.data.DB.Where("user_id = ? and object_id = ?", userID, objectID).Get(collection)
|
||||
exist, err = cr.data.DB.Context(ctx).Where("user_id = ? and object_id = ?", userID, objectID).Get(collection)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -108,7 +109,7 @@ func (cr *collectionRepo) GetOneByObjectIDAndUser(ctx context.Context, userID st
|
|||
// SearchByObjectIDsAndUser search by object IDs and user
|
||||
func (cr *collectionRepo) SearchByObjectIDsAndUser(ctx context.Context, userID string, objectIDs []string) ([]*entity.Collection, error) {
|
||||
collectionList := make([]*entity.Collection, 0)
|
||||
err := cr.data.DB.Where("user_id = ?", userID).In("object_id", objectIDs).Find(&collectionList)
|
||||
err := cr.data.DB.Context(ctx).Where("user_id = ?", userID).In("object_id", objectIDs).Find(&collectionList)
|
||||
if err != nil {
|
||||
return collectionList, err
|
||||
}
|
||||
|
@ -118,7 +119,7 @@ func (cr *collectionRepo) SearchByObjectIDsAndUser(ctx context.Context, userID s
|
|||
// CountByObjectID count by object TagID
|
||||
func (cr *collectionRepo) CountByObjectID(ctx context.Context, objectID string) (total int64, err error) {
|
||||
collection := &entity.Collection{}
|
||||
total, err = cr.data.DB.Where("object_id = ?", objectID).Count(collection)
|
||||
total, err = cr.data.DB.Context(ctx).Where("object_id = ?", objectID).Count(collection)
|
||||
if err != nil {
|
||||
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -129,7 +130,7 @@ func (cr *collectionRepo) CountByObjectID(ctx context.Context, objectID string)
|
|||
func (cr *collectionRepo) GetCollectionPage(ctx context.Context, page, pageSize int, collection *entity.Collection) (collectionList []*entity.Collection, total int64, err error) {
|
||||
collectionList = make([]*entity.Collection, 0)
|
||||
|
||||
session := cr.data.DB.NewSession()
|
||||
session := cr.data.DB.Context(ctx)
|
||||
if collection.UserID != "" && collection.UserID != "0" {
|
||||
session = session.Where("user_id = ?", collection.UserID)
|
||||
}
|
||||
|
@ -172,7 +173,7 @@ func (cr *collectionRepo) SearchList(ctx context.Context, search *entity.Collect
|
|||
search.PageSize = constant.DefaultPageSize
|
||||
}
|
||||
offset := search.Page * search.PageSize
|
||||
session := cr.data.DB.Where("")
|
||||
session := cr.data.DB.Context(ctx).Where("")
|
||||
if len(search.UserID) > 0 {
|
||||
session = session.And("user_id = ?", search.UserID)
|
||||
} else {
|
||||
|
|
|
@ -41,7 +41,7 @@ func (cr *commentRepo) AddComment(ctx context.Context, comment *entity.Comment)
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = cr.data.DB.Insert(comment)
|
||||
_, err = cr.data.DB.Context(ctx).Insert(comment)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func (cr *commentRepo) AddComment(ctx context.Context, comment *entity.Comment)
|
|||
|
||||
// RemoveComment delete comment
|
||||
func (cr *commentRepo) RemoveComment(ctx context.Context, commentID string) (err error) {
|
||||
session := cr.data.DB.ID(commentID)
|
||||
session := cr.data.DB.Context(ctx).ID(commentID)
|
||||
_, err = session.Update(&entity.Comment{Status: entity.CommentStatusDeleted})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -60,7 +60,7 @@ func (cr *commentRepo) RemoveComment(ctx context.Context, commentID string) (err
|
|||
|
||||
// UpdateComment update comment
|
||||
func (cr *commentRepo) UpdateComment(ctx context.Context, comment *entity.Comment) (err error) {
|
||||
_, err = cr.data.DB.ID(comment.ID).Where("user_id = ?", comment.UserID).Update(comment)
|
||||
_, err = cr.data.DB.Context(ctx).ID(comment.ID).Where("user_id = ?", comment.UserID).Update(comment)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
|
|||
comment *entity.Comment, exist bool, err error,
|
||||
) {
|
||||
comment = &entity.Comment{}
|
||||
exist, err = cr.data.DB.ID(commentID).Get(comment)
|
||||
exist, err = cr.data.DB.Context(ctx).ID(commentID).Get(comment)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -81,7 +81,7 @@ func (cr *commentRepo) GetComment(ctx context.Context, commentID string) (
|
|||
|
||||
func (cr *commentRepo) GetCommentCount(ctx context.Context) (count int64, err error) {
|
||||
list := make([]*entity.Comment, 0)
|
||||
count, err = cr.data.DB.Where("status = ?", entity.CommentStatusAvailable).FindAndCount(&list)
|
||||
count, err = cr.data.DB.Context(ctx).Where("status = ?", entity.CommentStatusAvailable).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -94,7 +94,7 @@ func (cr *commentRepo) GetCommentPage(ctx context.Context, commentQuery *comment
|
|||
) {
|
||||
commentList = make([]*entity.Comment, 0)
|
||||
|
||||
session := cr.data.DB.NewSession()
|
||||
session := cr.data.DB.Context(ctx)
|
||||
session.OrderBy(commentQuery.GetOrderBy())
|
||||
session.Where("status = ?", entity.CommentStatusAvailable)
|
||||
|
||||
|
|
|
@ -1,93 +0,0 @@
|
|||
package common
|
||||
|
||||
import (
|
||||
"github.com/answerdev/answer/internal/base/data"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/service/unique"
|
||||
"github.com/answerdev/answer/pkg/obj"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
type CommonRepo struct {
|
||||
data *data.Data
|
||||
uniqueIDRepo unique.UniqueIDRepo
|
||||
}
|
||||
|
||||
func NewCommonRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo) *CommonRepo {
|
||||
return &CommonRepo{
|
||||
data: data,
|
||||
uniqueIDRepo: uniqueIDRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// GetRootObjectID get root object ID
|
||||
func (cr *CommonRepo) GetRootObjectID(objectID string) (rootObjectID string, err error) {
|
||||
var (
|
||||
exist bool
|
||||
objectType string
|
||||
answer = entity.Answer{}
|
||||
comment = entity.Comment{}
|
||||
)
|
||||
|
||||
objectType, err = obj.GetObjectTypeStrByObjectID(objectID)
|
||||
switch objectType {
|
||||
case "answer":
|
||||
exist, err = cr.data.DB.ID(objectID).Get(&answer)
|
||||
if !exist {
|
||||
err = errors.BadRequest(reason.ObjectNotFound)
|
||||
}
|
||||
case "comment":
|
||||
exist, _ = cr.data.DB.ID(objectID).Get(&comment)
|
||||
if !exist {
|
||||
err = errors.BadRequest(reason.ObjectNotFound)
|
||||
} else {
|
||||
_, err = cr.GetRootObjectID(comment.ObjectID)
|
||||
}
|
||||
default:
|
||||
rootObjectID = objectID
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// GetObjectIDMap get object ID map from object id
|
||||
func (cr *CommonRepo) GetObjectIDMap(objectID string) (objectIDMap map[string]string, err error) {
|
||||
var (
|
||||
exist bool
|
||||
ID,
|
||||
objectType string
|
||||
answer = entity.Answer{}
|
||||
comment = entity.Comment{}
|
||||
)
|
||||
|
||||
objectIDMap = map[string]string{}
|
||||
// 10070000000000450
|
||||
objectType, err = obj.GetObjectTypeStrByObjectID(objectID)
|
||||
if err != nil {
|
||||
log.Error("get report object type:", objectID, ",err:", err)
|
||||
return
|
||||
}
|
||||
switch objectType {
|
||||
case "answer":
|
||||
exist, _ = cr.data.DB.ID(objectID).Get(&answer)
|
||||
if !exist {
|
||||
err = errors.BadRequest(reason.ObjectNotFound)
|
||||
} else {
|
||||
objectIDMap, err = cr.GetObjectIDMap(answer.QuestionID)
|
||||
ID = answer.ID
|
||||
}
|
||||
case "comment":
|
||||
exist, _ = cr.data.DB.ID(objectID).Get(&comment)
|
||||
if !exist {
|
||||
err = errors.BadRequest(reason.ObjectNotFound)
|
||||
} else {
|
||||
objectIDMap, err = cr.GetObjectIDMap(comment.ObjectID)
|
||||
ID = comment.ID
|
||||
}
|
||||
case "question":
|
||||
ID = objectID
|
||||
}
|
||||
objectIDMap[objectType] = ID
|
||||
return
|
||||
}
|
|
@ -1,29 +1,21 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/pkg/converter"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/base/data"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/segmentfault/pacman/errors"
|
||||
)
|
||||
|
||||
var (
|
||||
Key2ValueMapping = make(map[string]interface{})
|
||||
Key2IDMapping = make(map[string]int)
|
||||
ID2KeyMapping = make(map[int]string)
|
||||
"github.com/segmentfault/pacman/log"
|
||||
)
|
||||
|
||||
// configRepo config repository
|
||||
type configRepo struct {
|
||||
data *data.Data
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// NewConfigRepo new repository
|
||||
|
@ -31,110 +23,87 @@ func NewConfigRepo(data *data.Data) config.ConfigRepo {
|
|||
repo := &configRepo{
|
||||
data: data,
|
||||
}
|
||||
repo.init()
|
||||
return repo
|
||||
}
|
||||
|
||||
// init initializes the Key2ValueMapping map data structures
|
||||
func (cr *configRepo) init() {
|
||||
cr.mu.Lock()
|
||||
defer cr.mu.Unlock()
|
||||
rows := &[]entity.Config{}
|
||||
err := cr.data.DB.Find(rows)
|
||||
if err == nil {
|
||||
for _, row := range *rows {
|
||||
Key2ValueMapping[row.Key] = row.Value
|
||||
Key2IDMapping[row.Key] = row.ID
|
||||
ID2KeyMapping[row.ID] = row.Key
|
||||
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
|
||||
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
|
||||
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
|
||||
c = &entity.Config{}
|
||||
c.BuildByJSON([]byte(cacheData))
|
||||
if c.ID > 0 {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get Base method for getting the config value
|
||||
// Key string
|
||||
func (cr *configRepo) Get(key string) (interface{}, error) {
|
||||
value, ok := Key2ValueMapping[key]
|
||||
if ok {
|
||||
return value, nil
|
||||
} else {
|
||||
return value, errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config key: %v", key))
|
||||
}
|
||||
}
|
||||
|
||||
// GetString method for getting the config value to string
|
||||
// key string
|
||||
func (cr *configRepo) GetString(key string) (string, error) {
|
||||
value, err := cr.Get(key)
|
||||
c = &entity.Config{}
|
||||
exist, err := cr.data.DB.ID(id).Get(c)
|
||||
if err != nil {
|
||||
return "", err
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
str, ok := value.(string)
|
||||
if !ok {
|
||||
return "", errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("config value is wrong type: %v", key))
|
||||
if !exist {
|
||||
return nil, fmt.Errorf("config not found by id: %d", id)
|
||||
}
|
||||
return str, nil
|
||||
|
||||
// update cache
|
||||
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// GetInt method for getting the config value to int64
|
||||
// key string
|
||||
func (cr *configRepo) GetInt(key string) (int, error) {
|
||||
value, err := cr.GetString(key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
|
||||
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
|
||||
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
|
||||
c = &entity.Config{}
|
||||
c.BuildByJSON([]byte(cacheData))
|
||||
if c.ID > 0 {
|
||||
return c, nil
|
||||
}
|
||||
}
|
||||
return converter.StringToInt(value), nil
|
||||
|
||||
c = &entity.Config{Key: key}
|
||||
exist, err := cr.data.DB.Context(ctx).Get(c)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
if !exist {
|
||||
return nil, fmt.Errorf("config not found by key: %s", key)
|
||||
}
|
||||
|
||||
// update cache
|
||||
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
// GetArrayString method for getting the config value to string array
|
||||
func (cr *configRepo) GetArrayString(key string) ([]string, error) {
|
||||
arr := &[]string{}
|
||||
value, err := cr.GetString(key)
|
||||
func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
|
||||
// check if key exists
|
||||
cf := &entity.Config{}
|
||||
exist, err := cr.data.DB.Context(ctx).Get(cf)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
err = json.Unmarshal([]byte(value), arr)
|
||||
return *arr, err
|
||||
}
|
||||
|
||||
// GetConfigType method for getting the config type
|
||||
func (cr *configRepo) GetConfigType(key string) (int, error) {
|
||||
value, ok := Key2IDMapping[key]
|
||||
if !ok {
|
||||
return 0, errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config type: %v", key))
|
||||
}
|
||||
return value, nil
|
||||
}
|
||||
|
||||
// GetJsonConfigByIDAndSetToObject get config key from config id
|
||||
func (cr *configRepo) GetJsonConfigByIDAndSetToObject(id int, object any) (err error) {
|
||||
key, ok := ID2KeyMapping[id]
|
||||
if !ok {
|
||||
return errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
if !exist {
|
||||
return errors.BadRequest(reason.ObjectNotFound)
|
||||
}
|
||||
|
||||
conf, err := cr.Get(key)
|
||||
// update database
|
||||
_, err = cr.data.DB.Context(ctx).ID(cf.ID).Update(&entity.Config{Value: value})
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err)
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
str, ok := conf.(string)
|
||||
if !ok {
|
||||
return errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
|
||||
cacheVal := cf.JsonString()
|
||||
// update cache
|
||||
if err := cr.data.Cache.SetString(ctx,
|
||||
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
err = json.Unmarshal([]byte(str), object)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithMsg(fmt.Sprintf("no such config id: %v", id))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// SetConfig set config
|
||||
func (cr *configRepo) SetConfig(key, value string) (err error) {
|
||||
id := Key2IDMapping[key]
|
||||
_, err = cr.data.DB.ID(id).Update(&entity.Config{Value: value})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
} else {
|
||||
Key2ValueMapping[key] = value
|
||||
if err := cr.data.Cache.SetString(ctx,
|
||||
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, cf.ID), cacheVal, -1); err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewMetaRepo(data *data.Data) meta.MetaRepo {
|
|||
|
||||
// AddMeta add meta
|
||||
func (mr *metaRepo) AddMeta(ctx context.Context, meta *entity.Meta) (err error) {
|
||||
_, err = mr.data.DB.Insert(meta)
|
||||
_, err = mr.data.DB.Context(ctx).Insert(meta)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func (mr *metaRepo) AddMeta(ctx context.Context, meta *entity.Meta) (err error)
|
|||
|
||||
// RemoveMeta delete meta
|
||||
func (mr *metaRepo) RemoveMeta(ctx context.Context, id int) (err error) {
|
||||
_, err = mr.data.DB.ID(id).Delete(&entity.Meta{})
|
||||
_, err = mr.data.DB.Context(ctx).ID(id).Delete(&entity.Meta{})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -43,7 +43,7 @@ func (mr *metaRepo) RemoveMeta(ctx context.Context, id int) (err error) {
|
|||
|
||||
// UpdateMeta update meta
|
||||
func (mr *metaRepo) UpdateMeta(ctx context.Context, meta *entity.Meta) (err error) {
|
||||
_, err = mr.data.DB.ID(meta.ID).Update(meta)
|
||||
_, err = mr.data.DB.Context(ctx).ID(meta.ID).Update(meta)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ func (mr *metaRepo) UpdateMeta(ctx context.Context, meta *entity.Meta) (err erro
|
|||
func (mr *metaRepo) GetMetaByObjectIdAndKey(ctx context.Context, objectID, key string) (
|
||||
meta *entity.Meta, exist bool, err error) {
|
||||
meta = &entity.Meta{}
|
||||
exist, err = mr.data.DB.Where(builder.Eq{"object_id": objectID}.And(builder.Eq{"`key`": key})).Desc("created_at").Get(meta)
|
||||
exist, err = mr.data.DB.Context(ctx).Where(builder.Eq{"object_id": objectID}.And(builder.Eq{"`key`": key})).Desc("created_at").Get(meta)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -64,7 +64,7 @@ func (mr *metaRepo) GetMetaByObjectIdAndKey(ctx context.Context, objectID, key s
|
|||
// GetMetaList get meta list all
|
||||
func (mr *metaRepo) GetMetaList(ctx context.Context, meta *entity.Meta) (metaList []*entity.Meta, err error) {
|
||||
metaList = make([]*entity.Meta, 0)
|
||||
err = mr.data.DB.Find(&metaList, meta)
|
||||
err = mr.data.DB.Context(ctx).Find(&metaList, meta)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -29,7 +29,7 @@ func NewNotificationRepo(data *data.Data) notficationcommon.NotificationRepo {
|
|||
// AddNotification add notification
|
||||
func (nr *notificationRepo) AddNotification(ctx context.Context, notification *entity.Notification) (err error) {
|
||||
notification.ObjectID = uid.DeShortID(notification.ObjectID)
|
||||
_, err = nr.data.DB.Insert(notification)
|
||||
_, err = nr.data.DB.Context(ctx).Insert(notification)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ func (nr *notificationRepo) UpdateNotificationContent(ctx context.Context, notif
|
|||
now := time.Now()
|
||||
notification.UpdatedAt = now
|
||||
notification.ObjectID = uid.DeShortID(notification.ObjectID)
|
||||
_, err = nr.data.DB.Where("id =?", notification.ID).Cols("content", "updated_at").Update(notification)
|
||||
_, err = nr.data.DB.Context(ctx).Where("id =?", notification.ID).Cols("content", "updated_at").Update(notification)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func (nr *notificationRepo) UpdateNotificationContent(ctx context.Context, notif
|
|||
func (nr *notificationRepo) ClearUnRead(ctx context.Context, userID string, notificationType int) (err error) {
|
||||
info := &entity.Notification{}
|
||||
info.IsRead = schema.NotificationRead
|
||||
_, err = nr.data.DB.Where("user_id =?", userID).And("type =?", notificationType).Cols("is_read").Update(info)
|
||||
_, err = nr.data.DB.Context(ctx).Where("user_id =?", userID).And("type =?", notificationType).Cols("is_read").Update(info)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -60,7 +60,7 @@ func (nr *notificationRepo) ClearUnRead(ctx context.Context, userID string, noti
|
|||
func (nr *notificationRepo) ClearIDUnRead(ctx context.Context, userID string, id string) (err error) {
|
||||
info := &entity.Notification{}
|
||||
info.IsRead = schema.NotificationRead
|
||||
_, err = nr.data.DB.Where("user_id =?", userID).And("id =?", id).Cols("is_read").Update(info)
|
||||
_, err = nr.data.DB.Context(ctx).Where("user_id =?", userID).And("id =?", id).Cols("is_read").Update(info)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ func (nr *notificationRepo) ClearIDUnRead(ctx context.Context, userID string, id
|
|||
|
||||
func (nr *notificationRepo) GetById(ctx context.Context, id string) (*entity.Notification, bool, error) {
|
||||
info := &entity.Notification{}
|
||||
exist, err := nr.data.DB.Where("id = ? ", id).Get(info)
|
||||
exist, err := nr.data.DB.Context(ctx).Where("id = ? ", id).Get(info)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return info, false, err
|
||||
|
@ -79,7 +79,7 @@ func (nr *notificationRepo) GetById(ctx context.Context, id string) (*entity.Not
|
|||
|
||||
func (nr *notificationRepo) GetByUserIdObjectIdTypeId(ctx context.Context, userID, objectID string, notificationType int) (*entity.Notification, bool, error) {
|
||||
info := &entity.Notification{}
|
||||
exist, err := nr.data.DB.Where("user_id = ? ", userID).And("object_id = ?", objectID).And("type = ?", notificationType).Get(info)
|
||||
exist, err := nr.data.DB.Context(ctx).Where("user_id = ? ", userID).And("object_id = ?", objectID).And("type = ?", notificationType).Get(info)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return info, false, err
|
||||
|
@ -94,7 +94,7 @@ func (nr *notificationRepo) GetNotificationPage(ctx context.Context, searchCond
|
|||
return notificationList, 0, nil
|
||||
}
|
||||
|
||||
session := nr.data.DB.NewSession()
|
||||
session := nr.data.DB.Context(ctx)
|
||||
session = session.Desc("updated_at")
|
||||
|
||||
cond := &entity.Notification{
|
||||
|
|
|
@ -23,15 +23,15 @@ func NewPluginConfigRepo(data *data.Data) plugin_common.PluginConfigRepo {
|
|||
|
||||
func (ur *pluginConfigRepo) SavePluginConfig(ctx context.Context, pluginSlugName, configValue string) (err error) {
|
||||
old := &entity.PluginConfig{PluginSlugName: pluginSlugName}
|
||||
exist, err := ur.data.DB.Get(old)
|
||||
exist, err := ur.data.DB.Context(ctx).Get(old)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
if exist {
|
||||
old.Value = configValue
|
||||
_, err = ur.data.DB.ID(old.ID).Update(old)
|
||||
_, err = ur.data.DB.Context(ctx).ID(old.ID).Update(old)
|
||||
} else {
|
||||
_, err = ur.data.DB.InsertOne(&entity.PluginConfig{PluginSlugName: pluginSlugName, Value: configValue})
|
||||
_, err = ur.data.DB.Context(ctx).InsertOne(&entity.PluginConfig{PluginSlugName: pluginSlugName, Value: configValue})
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -41,7 +41,7 @@ func (ur *pluginConfigRepo) SavePluginConfig(ctx context.Context, pluginSlugName
|
|||
|
||||
func (ur *pluginConfigRepo) GetPluginConfigAll(ctx context.Context) (pluginConfigs []*entity.PluginConfig, err error) {
|
||||
pluginConfigs = make([]*entity.PluginConfig, 0)
|
||||
err = ur.data.DB.Find(&pluginConfigs)
|
||||
err = ur.data.DB.Context(ctx).Find(&pluginConfigs)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"github.com/answerdev/answer/internal/repo/captcha"
|
||||
"github.com/answerdev/answer/internal/repo/collection"
|
||||
"github.com/answerdev/answer/internal/repo/comment"
|
||||
"github.com/answerdev/answer/internal/repo/common"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/repo/export"
|
||||
"github.com/answerdev/answer/internal/repo/meta"
|
||||
|
@ -33,7 +32,6 @@ import (
|
|||
|
||||
// ProviderSetRepo is data providers.
|
||||
var ProviderSetRepo = wire.NewSet(
|
||||
common.NewCommonRepo,
|
||||
data.NewData,
|
||||
data.NewDB,
|
||||
data.NewCache,
|
||||
|
|
|
@ -46,7 +46,7 @@ func (qr *questionRepo) AddQuestion(ctx context.Context, question *entity.Questi
|
|||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
_, err = qr.data.DB.Insert(question)
|
||||
_, err = qr.data.DB.Context(ctx).Insert(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func (qr *questionRepo) AddQuestion(ctx context.Context, question *entity.Questi
|
|||
// RemoveQuestion delete question
|
||||
func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err error) {
|
||||
id = uid.DeShortID(id)
|
||||
_, err = qr.data.DB.Where("id =?", id).Delete(&entity.Question{})
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", id).Delete(&entity.Question{})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err erro
|
|||
// UpdateQuestion update question
|
||||
func (qr *questionRepo) UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols(Cols...).Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols(Cols...).Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -78,7 +78,7 @@ func (qr *questionRepo) UpdateQuestion(ctx context.Context, question *entity.Que
|
|||
func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (err error) {
|
||||
questionID = uid.DeShortID(questionID)
|
||||
question := &entity.Question{}
|
||||
_, err = qr.data.DB.Where("id =?", questionID).Incr("view_count", 1).Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("view_count", 1).Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -88,7 +88,7 @@ func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (e
|
|||
func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error) {
|
||||
questionID = uid.DeShortID(questionID)
|
||||
question := &entity.Question{}
|
||||
_, err = qr.data.DB.Where("id =?", questionID).Incr("answer_count", num).Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("answer_count", num).Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -98,7 +98,7 @@ func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string
|
|||
func (qr *questionRepo) UpdateCollectionCount(ctx context.Context, questionID string, num int) (err error) {
|
||||
questionID = uid.DeShortID(questionID)
|
||||
question := &entity.Question{}
|
||||
_, err = qr.data.DB.Where("id =?", questionID).Incr("collection_count", num).Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("collection_count", num).Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *enti
|
|||
question.ID = uid.DeShortID(question.ID)
|
||||
now := time.Now()
|
||||
question.UpdatedAt = now
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols("status", "updated_at").Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("status", "updated_at").Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -118,7 +118,7 @@ func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *enti
|
|||
|
||||
func (qr *questionRepo) UpdateQuestionStatusWithOutUpdateTime(ctx context.Context, question *entity.Question) (err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols("status").Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("status").Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -127,7 +127,7 @@ func (qr *questionRepo) UpdateQuestionStatusWithOutUpdateTime(ctx context.Contex
|
|||
|
||||
func (qr *questionRepo) UpdateQuestionOperation(ctx context.Context, question *entity.Question) (err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols("pin", "show").Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("pin", "show").Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -136,7 +136,7 @@ func (qr *questionRepo) UpdateQuestionOperation(ctx context.Context, question *e
|
|||
|
||||
func (qr *questionRepo) UpdateAccepted(ctx context.Context, question *entity.Question) (err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols("accepted_answer_id").Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("accepted_answer_id").Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -145,7 +145,7 @@ func (qr *questionRepo) UpdateAccepted(ctx context.Context, question *entity.Que
|
|||
|
||||
func (qr *questionRepo) UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
_, err = qr.data.DB.Where("id =?", question.ID).Cols("last_answer_id").Update(question)
|
||||
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("last_answer_id").Update(question)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -159,7 +159,7 @@ func (qr *questionRepo) GetQuestion(ctx context.Context, id string) (
|
|||
id = uid.DeShortID(id)
|
||||
question = &entity.Question{}
|
||||
question.ID = id
|
||||
exist, err = qr.data.DB.Where("id = ?", id).Get(question)
|
||||
exist, err = qr.data.DB.Context(ctx).Where("id = ?", id).Get(question)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -170,7 +170,7 @@ func (qr *questionRepo) GetQuestion(ctx context.Context, id string) (
|
|||
// GetTagBySlugName get tag by slug name
|
||||
func (qr *questionRepo) SearchByTitleLike(ctx context.Context, title string) (questionList []*entity.Question, err error) {
|
||||
questionList = make([]*entity.Question, 0)
|
||||
err = qr.data.DB.Table("question").Where("title like ?", "%"+title+"%").Limit(10, 0).Find(&questionList)
|
||||
err = qr.data.DB.Context(ctx).Table("question").Where("title like ?", "%"+title+"%").Limit(10, 0).Find(&questionList)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -185,7 +185,7 @@ func (qr *questionRepo) FindByID(ctx context.Context, id []string) (questionList
|
|||
id[key] = uid.DeShortID(itemID)
|
||||
}
|
||||
questionList = make([]*entity.Question, 0)
|
||||
err = qr.data.DB.Table("question").In("id", id).Find(&questionList)
|
||||
err = qr.data.DB.Context(ctx).Table("question").In("id", id).Find(&questionList)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -199,7 +199,7 @@ func (qr *questionRepo) FindByID(ctx context.Context, id []string) (questionList
|
|||
func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Question) (questionList []*entity.Question, err error) {
|
||||
question.ID = uid.DeShortID(question.ID)
|
||||
questionList = make([]*entity.Question, 0)
|
||||
err = qr.data.DB.Find(questionList, question)
|
||||
err = qr.data.DB.Context(ctx).Find(questionList, question)
|
||||
if err != nil {
|
||||
return questionList, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -212,7 +212,7 @@ func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Qu
|
|||
func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err error) {
|
||||
questionList := make([]*entity.Question, 0)
|
||||
|
||||
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).FindAndCount(&questionList)
|
||||
count, err = qr.data.DB.Context(ctx).In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).FindAndCount(&questionList)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -221,7 +221,7 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err
|
|||
|
||||
func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {
|
||||
questionList := make([]*entity.Question, 0)
|
||||
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).And("user_id = ?", userID).Count(&questionList)
|
||||
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).And("question.user_id = ?", userID).FindAndCount(&questionList)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -249,7 +249,7 @@ func (qr *questionRepo) GetQuestionIDsPage(ctx context.Context, page, pageSize i
|
|||
pageSize = constant.DefaultPageSize
|
||||
}
|
||||
offset := page * pageSize
|
||||
session := qr.data.DB.Table("question")
|
||||
session := qr.data.DB.Context(ctx).Table("question")
|
||||
session = session.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed})
|
||||
session.And("question.show = ?", entity.QuestionShow)
|
||||
session = session.Limit(pageSize, offset)
|
||||
|
@ -277,7 +277,7 @@ func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int,
|
|||
questionList []*entity.Question, total int64, err error) {
|
||||
questionList = make([]*entity.Question, 0)
|
||||
|
||||
session := qr.data.DB.Where("question.status = ? OR question.status = ?",
|
||||
session := qr.data.DB.Context(ctx).Where("question.status = ? OR question.status = ?",
|
||||
entity.QuestionStatusAvailable, entity.QuestionStatusClosed)
|
||||
if len(tagID) > 0 {
|
||||
session.Join("LEFT", "tag_rel", "question.id = tag_rel.object_id")
|
||||
|
@ -321,7 +321,7 @@ func (qr *questionRepo) AdminSearchList(ctx context.Context, search *schema.Admi
|
|||
var (
|
||||
count int64
|
||||
err error
|
||||
session = qr.data.DB.Table("question")
|
||||
session = qr.data.DB.Context(ctx).Table("question")
|
||||
)
|
||||
|
||||
session.Where(builder.Eq{
|
||||
|
|
|
@ -19,15 +19,15 @@ import (
|
|||
|
||||
// UserRankRepo user rank repository
|
||||
type UserRankRepo struct {
|
||||
data *data.Data
|
||||
configRepo config.ConfigRepo
|
||||
data *data.Data
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
// NewUserRankRepo new repository
|
||||
func NewUserRankRepo(data *data.Data, configRepo config.ConfigRepo) rank.UserRankRepo {
|
||||
func NewUserRankRepo(data *data.Data, configService *config.ConfigService) rank.UserRankRepo {
|
||||
return &UserRankRepo{
|
||||
data: data,
|
||||
configRepo: configRepo,
|
||||
data: data,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,14 +94,13 @@ func (ur *UserRankRepo) checkUserTodayRank(ctx context.Context,
|
|||
session *xorm.Session, userID string, activityType int,
|
||||
) (isReachStandard bool, err error) {
|
||||
// exclude daily rank
|
||||
exclude, _ := ur.configRepo.GetArrayString("daily_rank_limit.exclude")
|
||||
exclude, _ := ur.configService.GetArrayStringValue(ctx, "daily_rank_limit.exclude")
|
||||
for _, item := range exclude {
|
||||
var excludeActivityType int
|
||||
excludeActivityType, err = ur.configRepo.GetInt(item)
|
||||
cfg, err := ur.configService.GetConfigByKey(ctx, item)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if activityType == excludeActivityType {
|
||||
if activityType == cfg.ID {
|
||||
return false, nil
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +120,7 @@ func (ur *UserRankRepo) checkUserTodayRank(ctx context.Context,
|
|||
}
|
||||
|
||||
// max rank
|
||||
maxDailyRank, err := ur.configRepo.GetInt("daily_rank_limit")
|
||||
maxDailyRank, err := ur.configService.GetIntValue(ctx, "daily_rank_limit")
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
@ -138,7 +137,7 @@ func (ur *UserRankRepo) UserRankPage(ctx context.Context, userID string, page, p
|
|||
) {
|
||||
rankPage = make([]*entity.Activity, 0)
|
||||
|
||||
session := ur.data.DB.Where(builder.Eq{"has_rank": 1}.And(builder.Eq{"cancelled": 0})).And(builder.Gt{"rank": 0})
|
||||
session := ur.data.DB.Context(ctx).Where(builder.Eq{"has_rank": 1}.And(builder.Eq{"cancelled": 0})).And(builder.Gt{"rank": 0})
|
||||
session.Desc("created_at")
|
||||
|
||||
cond := &entity.Activity{UserID: userID}
|
||||
|
|
|
@ -13,12 +13,12 @@ import (
|
|||
)
|
||||
|
||||
type reasonRepo struct {
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
func NewReasonRepo(configRepo config.ConfigRepo) reason_common.ReasonRepo {
|
||||
func NewReasonRepo(configService *config.ConfigService) reason_common.ReasonRepo {
|
||||
return &reasonRepo{
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -27,31 +27,25 @@ func (rr *reasonRepo) ListReasons(ctx context.Context, objectType, action string
|
|||
reasonAction := fmt.Sprintf("%s.%s.reasons", objectType, action)
|
||||
resp = make([]*schema.ReasonItem, 0)
|
||||
|
||||
reasonKeys, err := rr.configRepo.GetArrayString(reasonAction)
|
||||
reasonKeys, err := rr.configService.GetArrayStringValue(ctx, reasonAction)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, reasonKey := range reasonKeys {
|
||||
cfgValue, err := rr.configRepo.GetString(reasonKey)
|
||||
cfg, err := rr.configService.GetConfigByKey(ctx, reasonKey)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
reason := &schema.ReasonItem{}
|
||||
err = json.Unmarshal([]byte(cfgValue), reason)
|
||||
err = json.Unmarshal(cfg.GetByteValue(), reason)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
reason.Translate(reasonKey, lang)
|
||||
|
||||
reason.ReasonType, err = rr.configRepo.GetConfigType(reasonKey)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
reason.ReasonType = cfg.ID
|
||||
resp = append(resp, reason)
|
||||
}
|
||||
return resp, nil
|
||||
|
|
|
@ -3,19 +3,18 @@ package repo_test
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_configRepo_Get(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
_, err := configRepo.Get("email.config")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetArrayString(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetArrayString("daily_rank_limit.exclude")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(got))
|
||||
|
@ -23,7 +22,7 @@ func Test_configRepo_GetArrayString(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_configRepo_GetConfigById(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
|
||||
closeInfo := &schema.GetReportTypeResp{}
|
||||
err := configRepo.GetJsonConfigByIDAndSetToObject(74, closeInfo)
|
||||
|
@ -33,27 +32,27 @@ func Test_configRepo_GetConfigById(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_configRepo_GetConfigType(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
configType, err := configRepo.GetConfigType("answer.accepted")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, configType)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetInt(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetInt("answer.accepted")
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 15, got)
|
||||
}
|
||||
|
||||
func Test_configRepo_GetString(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
_, err := configRepo.GetString("email.config")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_configRepo_SetConfig(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
got, err := configRepo.GetString("email.config")
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
|
|
@ -4,13 +4,12 @@ import (
|
|||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/repo/reason"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_reasonRepo_ListReasons(t *testing.T) {
|
||||
configRepo := config.NewConfigRepo(testDataSource)
|
||||
configRepo := config_common.NewConfigRepo(testDataSource)
|
||||
reasonRepo := reason.NewReasonRepo(configRepo)
|
||||
reasonItems, err := reasonRepo.ListReasons(context.TODO(), "question", "close")
|
||||
assert.NoError(t, err)
|
||||
|
|
|
@ -5,13 +5,12 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/repo/user"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func Test_userRepo_AddUser(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
userInfo := &entity.User{
|
||||
Username: "answer",
|
||||
Pass: "answer",
|
||||
|
@ -26,7 +25,7 @@ func Test_userRepo_AddUser(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_BatchGetByID(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
got, err := userRepo.BatchGetByID(context.TODO(), []string{"1"})
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 1, len(got))
|
||||
|
@ -34,7 +33,7 @@ func Test_userRepo_BatchGetByID(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_GetByEmail(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
got, exist, err := userRepo.GetByEmail(context.TODO(), "admin@admin.com")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, exist)
|
||||
|
@ -42,7 +41,7 @@ func Test_userRepo_GetByEmail(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_GetByUserID(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
got, exist, err := userRepo.GetByUserID(context.TODO(), "1")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, exist)
|
||||
|
@ -50,7 +49,7 @@ func Test_userRepo_GetByUserID(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_GetByUsername(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
got, exist, err := userRepo.GetByUsername(context.TODO(), "admin")
|
||||
assert.NoError(t, err)
|
||||
assert.True(t, exist)
|
||||
|
@ -58,7 +57,7 @@ func Test_userRepo_GetByUsername(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_IncreaseAnswerCount(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.IncreaseAnswerCount(context.TODO(), "1", 1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
@ -69,7 +68,7 @@ func Test_userRepo_IncreaseAnswerCount(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_IncreaseQuestionCount(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.IncreaseQuestionCount(context.TODO(), "1", 1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
@ -80,19 +79,19 @@ func Test_userRepo_IncreaseQuestionCount(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_UpdateEmail(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdateEmail(context.TODO(), "1", "admin@admin.com")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_userRepo_UpdateEmailStatus(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdateEmailStatus(context.TODO(), "1", entity.EmailStatusToBeVerified)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_userRepo_UpdateInfo(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdateInfo(context.TODO(), &entity.User{ID: "1", Bio: "test"})
|
||||
assert.NoError(t, err)
|
||||
|
||||
|
@ -103,19 +102,19 @@ func Test_userRepo_UpdateInfo(t *testing.T) {
|
|||
}
|
||||
|
||||
func Test_userRepo_UpdateLastLoginDate(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdateLastLoginDate(context.TODO(), "1")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_userRepo_UpdateNoticeStatus(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdateNoticeStatus(context.TODO(), "1", 1)
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func Test_userRepo_UpdatePass(t *testing.T) {
|
||||
userRepo := user.NewUserRepo(testDataSource, config.NewConfigRepo(testDataSource))
|
||||
userRepo := user.NewUserRepo(testDataSource, config_common.NewConfigRepo(testDataSource))
|
||||
err := userRepo.UpdatePass(context.TODO(), "1", "admin")
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ func (rr *reportRepo) AddReport(ctx context.Context, report *entity.Report) (err
|
|||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = rr.data.DB.Insert(report)
|
||||
_, err = rr.data.DB.Context(ctx).Insert(report)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -48,7 +48,7 @@ func (rr *reportRepo) GetReportListPage(ctx context.Context, dto schema.GetRepor
|
|||
ok bool
|
||||
status int
|
||||
objectType int
|
||||
session = rr.data.DB.NewSession()
|
||||
session = rr.data.DB.Context(ctx)
|
||||
cond = entity.Report{}
|
||||
)
|
||||
|
||||
|
@ -78,7 +78,7 @@ func (rr *reportRepo) GetReportListPage(ctx context.Context, dto schema.GetRepor
|
|||
// GetByID get report by ID
|
||||
func (rr *reportRepo) GetByID(ctx context.Context, id string) (report *entity.Report, exist bool, err error) {
|
||||
report = &entity.Report{}
|
||||
exist, err = rr.data.DB.ID(id).Get(report)
|
||||
exist, err = rr.data.DB.Context(ctx).ID(id).Get(report)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -87,7 +87,7 @@ func (rr *reportRepo) GetByID(ctx context.Context, id string) (report *entity.Re
|
|||
|
||||
// UpdateByID handle report by ID
|
||||
func (rr *reportRepo) UpdateByID(ctx context.Context, id string, handleData entity.Report) (err error) {
|
||||
_, err = rr.data.DB.ID(id).Update(&handleData)
|
||||
_, err = rr.data.DB.Context(ctx).ID(id).Update(&handleData)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -96,7 +96,7 @@ func (rr *reportRepo) UpdateByID(ctx context.Context, id string, handleData enti
|
|||
|
||||
func (rr *reportRepo) GetReportCount(ctx context.Context) (count int64, err error) {
|
||||
list := make([]*entity.Report, 0)
|
||||
count, err = rr.data.DB.Where("status =?", entity.ReportStatusPending).FindAndCount(&list)
|
||||
count, err = rr.data.DB.Context(ctx).Where("status =?", entity.ReportStatusPending).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ func (rr *revisionRepo) AddRevision(ctx context.Context, revision *entity.Revisi
|
|||
return nil
|
||||
}
|
||||
_, err = rr.data.DB.Transaction(func(session *xorm.Session) (interface{}, error) {
|
||||
session = session.Context(ctx)
|
||||
_, err = session.Insert(revision)
|
||||
if err != nil {
|
||||
_ = session.Rollback()
|
||||
|
@ -90,7 +91,7 @@ func (rr *revisionRepo) UpdateStatus(ctx context.Context, id string, status int,
|
|||
data.ID = id
|
||||
data.Status = status
|
||||
data.ReviewUserID = converter.StringToInt64(reviewUserID)
|
||||
_, err = rr.data.DB.Where("id =?", id).Cols("status", "review_user_id").Update(&data)
|
||||
_, err = rr.data.DB.Context(ctx).Where("id =?", id).Cols("status", "review_user_id").Update(&data)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -102,7 +103,7 @@ func (rr *revisionRepo) GetRevision(ctx context.Context, id string) (
|
|||
revision *entity.Revision, exist bool, err error,
|
||||
) {
|
||||
revision = &entity.Revision{}
|
||||
exist, err = rr.data.DB.ID(id).Get(revision)
|
||||
exist, err = rr.data.DB.Context(ctx).ID(id).Get(revision)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -113,7 +114,7 @@ func (rr *revisionRepo) GetRevision(ctx context.Context, id string) (
|
|||
func (rr *revisionRepo) GetRevisionByID(ctx context.Context, revisionID string) (
|
||||
revision *entity.Revision, exist bool, err error) {
|
||||
revision = &entity.Revision{}
|
||||
exist, err = rr.data.DB.Where("id = ?", revisionID).Get(revision)
|
||||
exist, err = rr.data.DB.Context(ctx).Where("id = ?", revisionID).Get(revision)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -123,7 +124,7 @@ func (rr *revisionRepo) GetRevisionByID(ctx context.Context, revisionID string)
|
|||
func (rr *revisionRepo) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (
|
||||
revision *entity.Revision, exist bool, err error) {
|
||||
revision = &entity.Revision{}
|
||||
exist, err = rr.data.DB.Where("object_id = ?", objectID).And("status = ?", entity.RevisionUnreviewedStatus).Get(revision)
|
||||
exist, err = rr.data.DB.Context(ctx).Where("object_id = ?", objectID).And("status = ?", entity.RevisionUnreviewedStatus).Get(revision)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -135,7 +136,7 @@ func (rr *revisionRepo) GetLastRevisionByObjectID(ctx context.Context, objectID
|
|||
revision *entity.Revision, exist bool, err error,
|
||||
) {
|
||||
revision = &entity.Revision{}
|
||||
exist, err = rr.data.DB.Where("object_id = ?", objectID).OrderBy("created_at DESC").Get(revision)
|
||||
exist, err = rr.data.DB.Context(ctx).Where("object_id = ?", objectID).OrderBy("created_at DESC").Get(revision)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -145,7 +146,7 @@ func (rr *revisionRepo) GetLastRevisionByObjectID(ctx context.Context, objectID
|
|||
// GetRevisionList get revision list all
|
||||
func (rr *revisionRepo) GetRevisionList(ctx context.Context, revision *entity.Revision) (revisionList []entity.Revision, err error) {
|
||||
revisionList = []entity.Revision{}
|
||||
err = rr.data.DB.Where(builder.Eq{
|
||||
err = rr.data.DB.Context(ctx).Where(builder.Eq{
|
||||
"object_id": revision.ObjectID,
|
||||
}).OrderBy("created_at DESC").Find(&revisionList)
|
||||
if err != nil {
|
||||
|
@ -175,7 +176,7 @@ func (rr *revisionRepo) GetUnreviewedRevisionPage(ctx context.Context, page int,
|
|||
if len(objectTypeList) == 0 {
|
||||
return revisionList, 0, nil
|
||||
}
|
||||
session := rr.data.DB.NewSession()
|
||||
session := rr.data.DB.Context(ctx)
|
||||
session = session.And("status = ?", entity.RevisionUnreviewedStatus)
|
||||
session = session.In("object_type", objectTypeList)
|
||||
session = session.OrderBy("created_at asc")
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewPowerRepo(data *data.Data) role.PowerRepo {
|
|||
// GetPowerList get list all
|
||||
func (pr *powerRepo) GetPowerList(ctx context.Context, power *entity.Power) (powerList []*entity.Power, err error) {
|
||||
powerList = make([]*entity.Power, 0)
|
||||
err = pr.data.DB.Find(powerList, power)
|
||||
err = pr.data.DB.Context(ctx).Find(powerList, power)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewRolePowerRelRepo(data *data.Data) role.RolePowerRelRepo {
|
|||
// GetRolePowerTypeList get role power type list
|
||||
func (rr *rolePowerRelRepo) GetRolePowerTypeList(ctx context.Context, roleID int) (powers []string, err error) {
|
||||
powers = make([]string, 0)
|
||||
err = rr.data.DB.Table("role_power_rel").
|
||||
err = rr.data.DB.Context(ctx).Table("role_power_rel").
|
||||
Cols("power_type").Where(builder.Eq{"role_id": roleID}).Find(&powers)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
|
|
@ -25,7 +25,7 @@ func NewRoleRepo(data *data.Data) service.RoleRepo {
|
|||
// GetRoleAllList get role list all
|
||||
func (rr *roleRepo) GetRoleAllList(ctx context.Context) (roleList []*entity.Role, err error) {
|
||||
roleList = make([]*entity.Role, 0)
|
||||
err = rr.data.DB.Find(&roleList)
|
||||
err = rr.data.DB.Context(ctx).Find(&roleList)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@ func NewUserRoleRelRepo(data *data.Data) role.UserRoleRelRepo {
|
|||
// SaveUserRoleRel save user role rel
|
||||
func (ur *userRoleRelRepo) SaveUserRoleRel(ctx context.Context, userID string, roleID int) (err error) {
|
||||
_, err = ur.data.DB.Transaction(func(session *xorm.Session) (interface{}, error) {
|
||||
session = session.Context(ctx)
|
||||
item := &entity.UserRoleRel{UserID: userID}
|
||||
exist, err := session.Get(item)
|
||||
if err != nil {
|
||||
|
@ -53,7 +54,7 @@ func (ur *userRoleRelRepo) SaveUserRoleRel(ctx context.Context, userID string, r
|
|||
func (ur *userRoleRelRepo) GetUserRoleRelList(ctx context.Context, userIDs []string) (
|
||||
userRoleRelList []*entity.UserRoleRel, err error) {
|
||||
userRoleRelList = make([]*entity.UserRoleRel, 0)
|
||||
err = ur.data.DB.In("user_id", userIDs).Find(&userRoleRelList)
|
||||
err = ur.data.DB.Context(ctx).In("user_id", userIDs).Find(&userRoleRelList)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -64,7 +65,7 @@ func (ur *userRoleRelRepo) GetUserRoleRelList(ctx context.Context, userIDs []str
|
|||
func (ur *userRoleRelRepo) GetUserRoleRelListByRoleID(ctx context.Context, roleIDs []int) (
|
||||
userRoleRelList []*entity.UserRoleRel, err error) {
|
||||
userRoleRelList = make([]*entity.UserRoleRel, 0)
|
||||
err = ur.data.DB.In("role_id", roleIDs).Find(&userRoleRelList)
|
||||
err = ur.data.DB.Context(ctx).In("role_id", roleIDs).Find(&userRoleRelList)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -75,7 +76,7 @@ func (ur *userRoleRelRepo) GetUserRoleRelListByRoleID(ctx context.Context, roleI
|
|||
func (ur *userRoleRelRepo) GetUserRoleRel(ctx context.Context, userID string) (
|
||||
rolePowerRel *entity.UserRoleRel, exist bool, err error) {
|
||||
rolePowerRel = &entity.UserRoleRel{}
|
||||
exist, err = ur.data.DB.Where(builder.Eq{"user_id": userID}).Get(rolePowerRel)
|
||||
exist, err = ur.data.DB.Context(ctx).Where(builder.Eq{"user_id": userID}).Get(rolePowerRel)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -190,12 +190,12 @@ func (sr *searchRepo) SearchContents(ctx context.Context, words []string, tagIDs
|
|||
countArgs = append(countArgs, argsQ...)
|
||||
countArgs = append(countArgs, argsA...)
|
||||
|
||||
res, err := sr.data.DB.Query(queryArgs...)
|
||||
res, err := sr.data.DB.Context(ctx).Query(queryArgs...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tr, err := sr.data.DB.Query(countArgs...)
|
||||
tr, err := sr.data.DB.Context(ctx).Query(countArgs...)
|
||||
if len(tr) != 0 {
|
||||
total = converter.StringToInt64(string(tr[0]["total"]))
|
||||
}
|
||||
|
@ -297,12 +297,12 @@ func (sr *searchRepo) SearchQuestions(ctx context.Context, words []string, tagID
|
|||
countArgs = append(countArgs, countSQL)
|
||||
countArgs = append(countArgs, args...)
|
||||
|
||||
res, err := sr.data.DB.Query(queryArgs...)
|
||||
res, err := sr.data.DB.Context(ctx).Query(queryArgs...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tr, err := sr.data.DB.Query(countArgs...)
|
||||
tr, err := sr.data.DB.Context(ctx).Query(countArgs...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -392,12 +392,12 @@ func (sr *searchRepo) SearchAnswers(ctx context.Context, words []string, tagIDs
|
|||
countArgs = append(countArgs, countSQL)
|
||||
countArgs = append(countArgs, args...)
|
||||
|
||||
res, err := sr.data.DB.Query(queryArgs...)
|
||||
res, err := sr.data.DB.Context(ctx).Query(queryArgs...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tr, err := sr.data.DB.Query(countArgs...)
|
||||
tr, err := sr.data.DB.Context(ctx).Query(countArgs...)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
@ -451,7 +451,7 @@ func (sr *searchRepo) parseResult(ctx context.Context, res []map[string][]byte)
|
|||
}
|
||||
|
||||
// get tags
|
||||
err = sr.data.DB.
|
||||
err = sr.data.DB.Context(ctx).
|
||||
Select("`display_name`,`slug_name`,`main_tag_slug_name`,`recommend`,`reserved`").
|
||||
Table("tag").
|
||||
Join("INNER", "tag_rel", "tag.id = tag_rel.tag_id").
|
||||
|
|
|
@ -27,14 +27,14 @@ func NewSiteInfo(data *data.Data) siteinfo_common.SiteInfoRepo {
|
|||
// SaveByType save site setting by type
|
||||
func (sr *siteInfoRepo) SaveByType(ctx context.Context, siteType string, data *entity.SiteInfo) (err error) {
|
||||
old := &entity.SiteInfo{}
|
||||
exist, err := sr.data.DB.Where(builder.Eq{"type": siteType}).Get(old)
|
||||
exist, err := sr.data.DB.Context(ctx).Where(builder.Eq{"type": siteType}).Get(old)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
if exist {
|
||||
_, err = sr.data.DB.ID(old.ID).Update(data)
|
||||
_, err = sr.data.DB.Context(ctx).ID(old.ID).Update(data)
|
||||
} else {
|
||||
_, err = sr.data.DB.Insert(data)
|
||||
_, err = sr.data.DB.Context(ctx).Insert(data)
|
||||
}
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -50,7 +50,7 @@ func (sr *siteInfoRepo) GetByType(ctx context.Context, siteType string) (siteInf
|
|||
return siteInfo, true, nil
|
||||
}
|
||||
siteInfo = &entity.SiteInfo{}
|
||||
exist, err = sr.data.DB.Where(builder.Eq{"type": siteType}).Get(siteInfo)
|
||||
exist, err = sr.data.DB.Context(ctx).Where(builder.Eq{"type": siteType}).Get(siteInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func (tr *tagRelRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRe
|
|||
for _, item := range tagList {
|
||||
item.ObjectID = uid.DeShortID(item.ObjectID)
|
||||
}
|
||||
_, err = tr.data.DB.Insert(tagList)
|
||||
_, err = tr.data.DB.Context(ctx).Insert(tagList)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -45,7 +45,7 @@ func (tr *tagRelRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRe
|
|||
// RemoveTagRelListByObjectID delete tag list
|
||||
func (tr *tagRelRepo) RemoveTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
|
||||
objectID = uid.DeShortID(objectID)
|
||||
_, err = tr.data.DB.Where("object_id = ?", objectID).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
|
||||
_, err = tr.data.DB.Context(ctx).Where("object_id = ?", objectID).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ func (tr *tagRelRepo) ShowTagRelListByObjectID(ctx context.Context, objectID str
|
|||
|
||||
// RemoveTagRelListByIDs delete tag list
|
||||
func (tr *tagRelRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (err error) {
|
||||
_, err = tr.data.DB.In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
|
||||
_, err = tr.data.DB.Context(ctx).In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ func (tr *tagRelRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectID
|
|||
) {
|
||||
objectID = uid.DeShortID(objectID)
|
||||
tagRel = &entity.TagRel{}
|
||||
session := tr.data.DB.Where("object_id = ?", objectID).And("tag_id = ?", tagID)
|
||||
session := tr.data.DB.Context(ctx).Where("object_id = ?", objectID).And("tag_id = ?", tagID)
|
||||
exist, err = session.Get(tagRel)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -96,7 +96,7 @@ func (tr *tagRelRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectID
|
|||
|
||||
// EnableTagRelByIDs update tag status to available
|
||||
func (tr *tagRelRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err error) {
|
||||
_, err = tr.data.DB.In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusAvailable})
|
||||
_, err = tr.data.DB.Context(ctx).In("id", ids).Update(&entity.TagRel{Status: entity.TagRelStatusAvailable})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -107,7 +107,7 @@ func (tr *tagRelRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err e
|
|||
func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string) (tagListList []*entity.TagRel, err error) {
|
||||
objectID = uid.DeShortID(objectID)
|
||||
tagListList = make([]*entity.TagRel, 0)
|
||||
session := tr.data.DB.Where("object_id = ?", objectID)
|
||||
session := tr.data.DB.Context(ctx).Where("object_id = ?", objectID)
|
||||
session.In("status", []int{entity.TagRelStatusAvailable, entity.TagRelStatusHide})
|
||||
err = session.Find(&tagListList)
|
||||
if err != nil {
|
||||
|
@ -125,7 +125,7 @@ func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []
|
|||
objectIds[num] = uid.DeShortID(item)
|
||||
}
|
||||
tagListList = make([]*entity.TagRel, 0)
|
||||
session := tr.data.DB.In("object_id", objectIds)
|
||||
session := tr.data.DB.Context(ctx).In("object_id", objectIds)
|
||||
session.Where("status = ?", entity.TagRelStatusAvailable)
|
||||
err = session.Find(&tagListList)
|
||||
if err != nil {
|
||||
|
@ -139,7 +139,7 @@ func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []
|
|||
|
||||
// CountTagRelByTagID count tag relation
|
||||
func (tr *tagRelRepo) CountTagRelByTagID(ctx context.Context, tagID string) (count int64, err error) {
|
||||
count, err = tr.data.DB.Count(&entity.TagRel{TagID: tagID, Status: entity.AnswerStatusAvailable})
|
||||
count, err = tr.data.DB.Context(ctx).Count(&entity.TagRel{TagID: tagID, Status: entity.AnswerStatusAvailable})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -32,7 +32,7 @@ func NewTagRepo(
|
|||
|
||||
// RemoveTag delete tag
|
||||
func (tr *tagRepo) RemoveTag(ctx context.Context, tagID string) (err error) {
|
||||
session := tr.data.DB.Where(builder.Eq{"id": tagID})
|
||||
session := tr.data.DB.Context(ctx).Where(builder.Eq{"id": tagID})
|
||||
_, err = session.Update(&entity.Tag{Status: entity.TagStatusDeleted})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -42,7 +42,7 @@ func (tr *tagRepo) RemoveTag(ctx context.Context, tagID string) (err error) {
|
|||
|
||||
// UpdateTag update tag
|
||||
func (tr *tagRepo) UpdateTag(ctx context.Context, tag *entity.Tag) (err error) {
|
||||
_, err = tr.data.DB.Where(builder.Eq{"id": tag.ID}).Update(tag)
|
||||
_, err = tr.data.DB.Context(ctx).Where(builder.Eq{"id": tag.ID}).Update(tag)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ func (tr *tagRepo) UpdateTagSynonym(ctx context.Context, tagSlugNameList []strin
|
|||
mainTagSlugName string,
|
||||
) (err error) {
|
||||
bean := &entity.Tag{MainTagID: mainTagID, MainTagSlugName: mainTagSlugName}
|
||||
session := tr.data.DB.In("slug_name", tagSlugNameList).MustCols("main_tag_id", "main_tag_slug_name")
|
||||
session := tr.data.DB.Context(ctx).In("slug_name", tagSlugNameList).MustCols("main_tag_id", "main_tag_slug_name")
|
||||
_, err = session.Update(bean)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -63,7 +63,7 @@ func (tr *tagRepo) UpdateTagSynonym(ctx context.Context, tagSlugNameList []strin
|
|||
}
|
||||
|
||||
func (tr *tagRepo) GetTagSynonymCount(ctx context.Context, tagID string) (count int64, err error) {
|
||||
count, err = tr.data.DB.Count(&entity.Tag{MainTagID: converter.StringToInt64(tagID), Status: entity.TagStatusAvailable})
|
||||
count, err = tr.data.DB.Context(ctx).Count(&entity.Tag{MainTagID: converter.StringToInt64(tagID), Status: entity.TagStatusAvailable})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -73,7 +73,7 @@ func (tr *tagRepo) GetTagSynonymCount(ctx context.Context, tagID string) (count
|
|||
// GetTagList get tag list all
|
||||
func (tr *tagRepo) GetTagList(ctx context.Context, tag *entity.Tag) (tagList []*entity.Tag, err error) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
session := tr.data.DB.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
session := tr.data.DB.Context(ctx).Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
err = session.Find(&tagList, tag)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
|
|
@ -34,7 +34,7 @@ func NewTagCommonRepo(
|
|||
// GetTagListByIDs get tag list all
|
||||
func (tr *tagCommonRepo) GetTagListByIDs(ctx context.Context, ids []string) (tagList []*entity.Tag, err error) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
session := tr.data.DB.In("id", ids)
|
||||
session := tr.data.DB.Context(ctx).In("id", ids)
|
||||
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList)
|
||||
if err != nil {
|
||||
|
@ -46,7 +46,7 @@ func (tr *tagCommonRepo) GetTagListByIDs(ctx context.Context, ids []string) (tag
|
|||
// GetTagBySlugName get tag by slug name
|
||||
func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string) (tagInfo *entity.Tag, exist bool, err error) {
|
||||
tagInfo = &entity.Tag{}
|
||||
session := tr.data.DB.Where("LOWER(slug_name) = ?", slugName)
|
||||
session := tr.data.DB.Context(ctx).Where("LOWER(slug_name) = ?", slugName)
|
||||
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
exist, err = session.Get(tagInfo)
|
||||
if err != nil {
|
||||
|
@ -59,7 +59,7 @@ func (tr *tagCommonRepo) GetTagBySlugName(ctx context.Context, slugName string)
|
|||
func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, hasReserved bool) (tagList []*entity.Tag, err error) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
cond := &entity.Tag{}
|
||||
session := tr.data.DB.Where("")
|
||||
session := tr.data.DB.Context(ctx).Where("")
|
||||
if name != "" {
|
||||
session.Where("slug_name LIKE LOWER(?) or display_name LIKE ?", name+"%", name+"%")
|
||||
} else {
|
||||
|
@ -78,7 +78,7 @@ func (tr *tagCommonRepo) GetTagListByName(ctx context.Context, name string, hasR
|
|||
func (tr *tagCommonRepo) GetRecommendTagList(ctx context.Context) (tagList []*entity.Tag, err error) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
cond := &entity.Tag{}
|
||||
session := tr.data.DB.Where("")
|
||||
session := tr.data.DB.Context(ctx).Where("")
|
||||
cond.Recommend = true
|
||||
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
session.Asc("slug_name")
|
||||
|
@ -93,7 +93,7 @@ func (tr *tagCommonRepo) GetRecommendTagList(ctx context.Context) (tagList []*en
|
|||
func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*entity.Tag, err error) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
cond := &entity.Tag{}
|
||||
session := tr.data.DB.Where("")
|
||||
session := tr.data.DB.Context(ctx).Where("")
|
||||
cond.Reserved = true
|
||||
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
session.Asc("slug_name")
|
||||
|
@ -109,7 +109,7 @@ func (tr *tagCommonRepo) GetReservedTagList(ctx context.Context) (tagList []*ent
|
|||
func (tr *tagCommonRepo) GetTagListByNames(ctx context.Context, names []string) (tagList []*entity.Tag, err error) {
|
||||
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
session := tr.data.DB.In("slug_name", names).UseBool("recommend", "reserved")
|
||||
session := tr.data.DB.Context(ctx).In("slug_name", names).UseBool("recommend", "reserved")
|
||||
// session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
err = session.OrderBy("recommend desc,reserved desc,id desc").Find(&tagList)
|
||||
if err != nil {
|
||||
|
@ -123,7 +123,7 @@ func (tr *tagCommonRepo) GetTagByID(ctx context.Context, tagID string, includeDe
|
|||
tag *entity.Tag, exist bool, err error,
|
||||
) {
|
||||
tag = &entity.Tag{}
|
||||
session := tr.data.DB.Where(builder.Eq{"id": tagID})
|
||||
session := tr.data.DB.Context(ctx).Where(builder.Eq{"id": tagID})
|
||||
if !includeDeleted {
|
||||
session.Where(builder.Eq{"status": entity.TagStatusAvailable})
|
||||
}
|
||||
|
@ -139,7 +139,7 @@ func (tr *tagCommonRepo) GetTagPage(ctx context.Context, page, pageSize int, tag
|
|||
tagList []*entity.Tag, total int64, err error,
|
||||
) {
|
||||
tagList = make([]*entity.Tag, 0)
|
||||
session := tr.data.DB.NewSession()
|
||||
session := tr.data.DB.Context(ctx)
|
||||
|
||||
if len(tag.SlugName) > 0 {
|
||||
session.Where(builder.Or(builder.Like{"slug_name", fmt.Sprintf("LOWER(%s)", tag.SlugName)}, builder.Like{"display_name", tag.SlugName}))
|
||||
|
@ -173,7 +173,7 @@ func (tr *tagCommonRepo) AddTagList(ctx context.Context, tagList []*entity.Tag)
|
|||
}
|
||||
item.RevisionID = "0"
|
||||
}
|
||||
_, err = tr.data.DB.Insert(tagList)
|
||||
_, err = tr.data.DB.Context(ctx).Insert(tagList)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -183,7 +183,7 @@ func (tr *tagCommonRepo) AddTagList(ctx context.Context, tagList []*entity.Tag)
|
|||
// UpdateTagQuestionCount update tag question count
|
||||
func (tr *tagCommonRepo) UpdateTagQuestionCount(ctx context.Context, tagID string, questionCount int) (err error) {
|
||||
cond := &entity.Tag{QuestionCount: questionCount}
|
||||
_, err = tr.data.DB.Where(builder.Eq{"id": tagID}).MustCols("question_count").Update(cond)
|
||||
_, err = tr.data.DB.Context(ctx).Where(builder.Eq{"id": tagID}).MustCols("question_count").Update(cond)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -200,7 +200,7 @@ func (tr *tagCommonRepo) UpdateTagsAttribute(ctx context.Context, tags []string,
|
|||
default:
|
||||
return
|
||||
}
|
||||
session := tr.data.DB.In("slug_name", tags).Cols(attribute).UseBool(attribute)
|
||||
session := tr.data.DB.Context(ctx).In("slug_name", tags).Cols(attribute).UseBool(attribute)
|
||||
_, err = session.Update(bean)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
|
|
@ -29,7 +29,7 @@ func NewUniqueIDRepo(data *data.Data) unique.UniqueIDRepo {
|
|||
func (ur *uniqueIDRepo) GenUniqueIDStr(ctx context.Context, key string) (uniqueID string, err error) {
|
||||
objectType := constant.ObjectTypeStrMapping[key]
|
||||
bean := &entity.Uniqid{UniqidType: objectType}
|
||||
_, err = ur.data.DB.Insert(bean)
|
||||
_, err = ur.data.DB.Context(ctx).Insert(bean)
|
||||
if err != nil {
|
||||
return "", errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -42,7 +42,7 @@ func (ur *userAdminRepo) UpdateUserStatus(ctx context.Context, userID string, us
|
|||
case entity.UserStatusDeleted:
|
||||
cond.DeletedAt = time.Now()
|
||||
}
|
||||
_, err = ur.data.DB.ID(userID).Update(cond)
|
||||
_, err = ur.data.DB.Context(ctx).ID(userID).Update(cond)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -63,7 +63,7 @@ func (ur *userAdminRepo) UpdateUserStatus(ctx context.Context, userID string, us
|
|||
|
||||
// AddUser add user
|
||||
func (ur *userAdminRepo) AddUser(ctx context.Context, user *entity.User) (err error) {
|
||||
_, err = ur.data.DB.Insert(user)
|
||||
_, err = ur.data.DB.Context(ctx).Insert(user)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ func (ur *userAdminRepo) AddUser(ctx context.Context, user *entity.User) (err er
|
|||
|
||||
// UpdateUserPassword update user password
|
||||
func (ur *userAdminRepo) UpdateUserPassword(ctx context.Context, userID string, password string) (err error) {
|
||||
_, err = ur.data.DB.ID(userID).Update(&entity.User{Pass: password})
|
||||
_, err = ur.data.DB.Context(ctx).ID(userID).Update(&entity.User{Pass: password})
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -82,7 +82,7 @@ func (ur *userAdminRepo) UpdateUserPassword(ctx context.Context, userID string,
|
|||
// GetUserInfo get user info
|
||||
func (ur *userAdminRepo) GetUserInfo(ctx context.Context, userID string) (user *entity.User, exist bool, err error) {
|
||||
user = &entity.User{}
|
||||
exist, err = ur.data.DB.ID(userID).Get(user)
|
||||
exist, err = ur.data.DB.Context(ctx).ID(userID).Get(user)
|
||||
if err != nil {
|
||||
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -99,7 +99,7 @@ func (ur *userAdminRepo) GetUserInfo(ctx context.Context, userID string) (user *
|
|||
// GetUserInfoByEmail get user info
|
||||
func (ur *userAdminRepo) GetUserInfoByEmail(ctx context.Context, email string) (user *entity.User, exist bool, err error) {
|
||||
userInfo := &entity.User{}
|
||||
exist, err = ur.data.DB.Where("e_mail = ?", email).
|
||||
exist, err = ur.data.DB.Context(ctx).Where("e_mail = ?", email).
|
||||
Where("status != ?", entity.UserStatusDeleted).Get(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -119,7 +119,7 @@ func (ur *userAdminRepo) GetUserInfoByEmail(ctx context.Context, email string) (
|
|||
func (ur *userAdminRepo) GetUserPage(ctx context.Context, page, pageSize int, user *entity.User,
|
||||
usernameOrDisplayName string, isStaff bool) (users []*entity.User, total int64, err error) {
|
||||
users = make([]*entity.User, 0)
|
||||
session := ur.data.DB.NewSession()
|
||||
session := ur.data.DB.Context(ctx)
|
||||
switch user.Status {
|
||||
case entity.UserStatusDeleted:
|
||||
session.Desc("user.deleted_at")
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
usercommon "github.com/answerdev/answer/internal/service/user_common"
|
||||
"github.com/answerdev/answer/pkg/converter"
|
||||
"github.com/answerdev/answer/plugin"
|
||||
|
@ -19,21 +18,20 @@ import (
|
|||
|
||||
// userRepo user repository
|
||||
type userRepo struct {
|
||||
data *data.Data
|
||||
configRepo config.ConfigRepo
|
||||
data *data.Data
|
||||
}
|
||||
|
||||
// NewUserRepo new repository
|
||||
func NewUserRepo(data *data.Data, configRepo config.ConfigRepo) usercommon.UserRepo {
|
||||
func NewUserRepo(data *data.Data) usercommon.UserRepo {
|
||||
return &userRepo{
|
||||
data: data,
|
||||
configRepo: configRepo,
|
||||
data: data,
|
||||
}
|
||||
}
|
||||
|
||||
// AddUser add user
|
||||
func (ur *userRepo) AddUser(ctx context.Context, user *entity.User) (err error) {
|
||||
_, err = ur.data.DB.Transaction(func(session *xorm.Session) (interface{}, error) {
|
||||
session = session.Context(ctx)
|
||||
userInfo := &entity.User{}
|
||||
exist, err := session.Where("username = ?", user.Username).Get(userInfo)
|
||||
if err != nil {
|
||||
|
@ -54,7 +52,7 @@ func (ur *userRepo) AddUser(ctx context.Context, user *entity.User) (err error)
|
|||
// IncreaseAnswerCount increase answer count
|
||||
func (ur *userRepo) IncreaseAnswerCount(ctx context.Context, userID string, amount int) (err error) {
|
||||
user := &entity.User{}
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Incr("answer_count", amount).Update(user)
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Incr("answer_count", amount).Update(user)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -64,7 +62,7 @@ func (ur *userRepo) IncreaseAnswerCount(ctx context.Context, userID string, amou
|
|||
// IncreaseQuestionCount increase question count
|
||||
func (ur *userRepo) IncreaseQuestionCount(ctx context.Context, userID string, amount int) (err error) {
|
||||
user := &entity.User{}
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Incr("question_count", amount).Update(user)
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Incr("question_count", amount).Update(user)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -94,7 +92,7 @@ func (ur *userRepo) UpdateAnswerCount(ctx context.Context, userID string, count
|
|||
// UpdateLastLoginDate update last login date
|
||||
func (ur *userRepo) UpdateLastLoginDate(ctx context.Context, userID string) (err error) {
|
||||
user := &entity.User{LastLoginDate: time.Now()}
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Cols("last_login_date").Update(user)
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Cols("last_login_date").Update(user)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -104,7 +102,7 @@ func (ur *userRepo) UpdateLastLoginDate(ctx context.Context, userID string) (err
|
|||
// UpdateEmailStatus update email status
|
||||
func (ur *userRepo) UpdateEmailStatus(ctx context.Context, userID string, emailStatus int) error {
|
||||
cond := &entity.User{MailStatus: emailStatus}
|
||||
_, err := ur.data.DB.Where("id = ?", userID).Cols("mail_status").Update(cond)
|
||||
_, err := ur.data.DB.Context(ctx).Where("id = ?", userID).Cols("mail_status").Update(cond)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -114,7 +112,7 @@ func (ur *userRepo) UpdateEmailStatus(ctx context.Context, userID string, emailS
|
|||
// UpdateNoticeStatus update notice status
|
||||
func (ur *userRepo) UpdateNoticeStatus(ctx context.Context, userID string, noticeStatus int) error {
|
||||
cond := &entity.User{NoticeStatus: noticeStatus}
|
||||
_, err := ur.data.DB.Where("id = ?", userID).Cols("notice_status").Update(cond)
|
||||
_, err := ur.data.DB.Context(ctx).Where("id = ?", userID).Cols("notice_status").Update(cond)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -122,7 +120,7 @@ func (ur *userRepo) UpdateNoticeStatus(ctx context.Context, userID string, notic
|
|||
}
|
||||
|
||||
func (ur *userRepo) UpdatePass(ctx context.Context, userID, pass string) error {
|
||||
_, err := ur.data.DB.Where("id = ?", userID).Cols("pass").Update(&entity.User{Pass: pass})
|
||||
_, err := ur.data.DB.Context(ctx).Where("id = ?", userID).Cols("pass").Update(&entity.User{Pass: pass})
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -130,7 +128,7 @@ func (ur *userRepo) UpdatePass(ctx context.Context, userID, pass string) error {
|
|||
}
|
||||
|
||||
func (ur *userRepo) UpdateEmail(ctx context.Context, userID, email string) (err error) {
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Update(&entity.User{EMail: email})
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Update(&entity.User{EMail: email})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -138,7 +136,7 @@ func (ur *userRepo) UpdateEmail(ctx context.Context, userID, email string) (err
|
|||
}
|
||||
|
||||
func (ur *userRepo) UpdateLanguage(ctx context.Context, userID, language string) (err error) {
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Update(&entity.User{Language: language})
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Update(&entity.User{Language: language})
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -147,7 +145,7 @@ func (ur *userRepo) UpdateLanguage(ctx context.Context, userID, language string)
|
|||
|
||||
// UpdateInfo update user info
|
||||
func (ur *userRepo) UpdateInfo(ctx context.Context, userInfo *entity.User) (err error) {
|
||||
_, err = ur.data.DB.Where("id = ?", userInfo.ID).
|
||||
_, err = ur.data.DB.Context(ctx).Where("id = ?", userInfo.ID).
|
||||
Cols("username", "display_name", "avatar", "bio", "bio_html", "website", "location").Update(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -158,7 +156,7 @@ func (ur *userRepo) UpdateInfo(ctx context.Context, userInfo *entity.User) (err
|
|||
// GetByUserID get user info by user id
|
||||
func (ur *userRepo) GetByUserID(ctx context.Context, userID string) (userInfo *entity.User, exist bool, err error) {
|
||||
userInfo = &entity.User{}
|
||||
exist, err = ur.data.DB.Where("id = ?", userID).Get(userInfo)
|
||||
exist, err = ur.data.DB.Context(ctx).Where("id = ?", userID).Get(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return
|
||||
|
@ -172,7 +170,7 @@ func (ur *userRepo) GetByUserID(ctx context.Context, userID string) (userInfo *e
|
|||
|
||||
func (ur *userRepo) BatchGetByID(ctx context.Context, ids []string) ([]*entity.User, error) {
|
||||
list := make([]*entity.User, 0)
|
||||
err := ur.data.DB.In("id", ids).Find(&list)
|
||||
err := ur.data.DB.Context(ctx).In("id", ids).Find(&list)
|
||||
if err != nil {
|
||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -183,7 +181,7 @@ func (ur *userRepo) BatchGetByID(ctx context.Context, ids []string) ([]*entity.U
|
|||
// GetByUsername get user by username
|
||||
func (ur *userRepo) GetByUsername(ctx context.Context, username string) (userInfo *entity.User, exist bool, err error) {
|
||||
userInfo = &entity.User{}
|
||||
exist, err = ur.data.DB.Where("username = ?", username).Get(userInfo)
|
||||
exist, err = ur.data.DB.Context(ctx).Where("username = ?", username).Get(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
return
|
||||
|
@ -209,7 +207,7 @@ func (ur *userRepo) GetByUsernames(ctx context.Context, usernames []string) ([]*
|
|||
// GetByEmail get user by email
|
||||
func (ur *userRepo) GetByEmail(ctx context.Context, email string) (userInfo *entity.User, exist bool, err error) {
|
||||
userInfo = &entity.User{}
|
||||
exist, err = ur.data.DB.Where("e_mail = ?", email).
|
||||
exist, err = ur.data.DB.Context(ctx).Where("e_mail = ?", email).
|
||||
Where("status != ?", entity.UserStatusDeleted).Get(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
|
@ -219,7 +217,7 @@ func (ur *userRepo) GetByEmail(ctx context.Context, email string) (userInfo *ent
|
|||
|
||||
func (ur *userRepo) GetUserCount(ctx context.Context) (count int64, err error) {
|
||||
list := make([]*entity.User, 0)
|
||||
count, err = ur.data.DB.Where("mail_status =?", entity.EmailStatusAvailable).And("status =?", entity.UserStatusAvailable).FindAndCount(&list)
|
||||
count, err = ur.data.DB.Context(ctx).Where("mail_status =?", entity.EmailStatusAvailable).And("status =?", entity.UserStatusAvailable).FindAndCount(&list)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -253,7 +251,7 @@ func tryToDecorateUserInfoFromUserCenter(ctx context.Context, data *data.Data, o
|
|||
}
|
||||
|
||||
userInfo := &entity.UserExternalLogin{}
|
||||
session := data.DB.Where("user_id = ?", original.ID)
|
||||
session := data.DB.Context(ctx).Where("user_id = ?", original.ID)
|
||||
session.Where("provider = ?", uc.Info().SlugName)
|
||||
exist, err := session.Get(userInfo)
|
||||
if err != nil {
|
||||
|
@ -291,7 +289,7 @@ func tryToDecorateUserListFromUserCenter(ctx context.Context, data *data.Data, o
|
|||
}
|
||||
|
||||
userExternalLoginList := make([]*entity.UserExternalLogin, 0)
|
||||
session := data.DB.Where("provider = ?", uc.Info().SlugName)
|
||||
session := data.DB.Context(ctx).Where("provider = ?", uc.Info().SlugName)
|
||||
session.In("user_id", ids)
|
||||
err := session.Find(&userExternalLoginList)
|
||||
if err != nil {
|
||||
|
|
|
@ -26,7 +26,7 @@ func NewUserExternalLoginRepo(data *data.Data) user_external_login.UserExternalL
|
|||
|
||||
// AddUserExternalLogin add external login information
|
||||
func (ur *userExternalLoginRepo) AddUserExternalLogin(ctx context.Context, user *entity.UserExternalLogin) (err error) {
|
||||
_, err = ur.data.DB.Insert(user)
|
||||
_, err = ur.data.DB.Context(ctx).Insert(user)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ func (ur *userExternalLoginRepo) AddUserExternalLogin(ctx context.Context, user
|
|||
|
||||
// UpdateInfo update user info
|
||||
func (ur *userExternalLoginRepo) UpdateInfo(ctx context.Context, userInfo *entity.UserExternalLogin) (err error) {
|
||||
_, err = ur.data.DB.ID(userInfo.ID).Update(userInfo)
|
||||
_, err = ur.data.DB.Context(ctx).ID(userInfo.ID).Update(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -46,7 +46,7 @@ func (ur *userExternalLoginRepo) UpdateInfo(ctx context.Context, userInfo *entit
|
|||
func (ur *userExternalLoginRepo) GetByExternalID(ctx context.Context, provider, externalID string) (
|
||||
userInfo *entity.UserExternalLogin, exist bool, err error) {
|
||||
userInfo = &entity.UserExternalLogin{}
|
||||
exist, err = ur.data.DB.Where("external_id = ?", externalID).Where("provider = ?", provider).Get(userInfo)
|
||||
exist, err = ur.data.DB.Context(ctx).Where("external_id = ?", externalID).Where("provider = ?", provider).Get(userInfo)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -57,7 +57,7 @@ func (ur *userExternalLoginRepo) GetByExternalID(ctx context.Context, provider,
|
|||
func (ur *userExternalLoginRepo) GetUserExternalLoginList(ctx context.Context, userID string) (
|
||||
resp []*entity.UserExternalLogin, err error) {
|
||||
resp = make([]*entity.UserExternalLogin, 0)
|
||||
err = ur.data.DB.Where("user_id = ?", userID).Find(&resp)
|
||||
err = ur.data.DB.Context(ctx).Where("user_id = ?", userID).Find(&resp)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
@ -67,7 +67,7 @@ func (ur *userExternalLoginRepo) GetUserExternalLoginList(ctx context.Context, u
|
|||
// DeleteUserExternalLogin delete external user login info
|
||||
func (ur *userExternalLoginRepo) DeleteUserExternalLogin(ctx context.Context, userID, externalID string) (err error) {
|
||||
cond := &entity.UserExternalLogin{}
|
||||
_, err = ur.data.DB.Where("user_id = ? AND external_id = ?", userID, externalID).Delete(cond)
|
||||
_, err = ur.data.DB.Context(ctx).Where("user_id = ? AND external_id = ?", userID, externalID).Delete(cond)
|
||||
if err != nil {
|
||||
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
|
|
|
@ -12,8 +12,8 @@ type GetRankPersonalWithPageReq struct {
|
|||
UserID string `json:"-"`
|
||||
}
|
||||
|
||||
// GetRankPersonalWithPageResp rank response
|
||||
type GetRankPersonalWithPageResp struct {
|
||||
// GetRankPersonalPageResp rank response
|
||||
type GetRankPersonalPageResp struct {
|
||||
// create time
|
||||
CreatedAt int64 `json:"created_at"`
|
||||
// object id
|
||||
|
|
|
@ -8,10 +8,10 @@ import (
|
|||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"github.com/answerdev/answer/internal/service/activity_common"
|
||||
"github.com/answerdev/answer/internal/service/comment_common"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/internal/service/meta"
|
||||
"github.com/answerdev/answer/internal/service/object_info"
|
||||
"github.com/answerdev/answer/internal/service/revision_common"
|
||||
|
@ -38,6 +38,7 @@ type ActivityService struct {
|
|||
commentCommonService *comment_common.CommentCommonService
|
||||
revisionService *revision_common.RevisionService
|
||||
metaService *meta.MetaService
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
// NewActivityService new activity service
|
||||
|
@ -50,6 +51,7 @@ func NewActivityService(
|
|||
commentCommonService *comment_common.CommentCommonService,
|
||||
revisionService *revision_common.RevisionService,
|
||||
metaService *meta.MetaService,
|
||||
configService *config.ConfigService,
|
||||
) *ActivityService {
|
||||
return &ActivityService{
|
||||
objectInfoService: objectInfoService,
|
||||
|
@ -60,6 +62,7 @@ func NewActivityService(
|
|||
commentCommonService: commentCommonService,
|
||||
revisionService: revisionService,
|
||||
metaService: metaService,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -97,14 +100,19 @@ func (as *ActivityService) GetObjectTimeline(ctx context.Context, req *schema.Ge
|
|||
item.ObjectID = uid.EnShortID(act.ObjectID)
|
||||
}
|
||||
|
||||
// database save activity type is number, change to activity type string is like "question.asked".
|
||||
// so we need to cut the front part of '.'
|
||||
_, item.ActivityType, _ = strings.Cut(config.ID2KeyMapping[act.ActivityType], ".")
|
||||
// format activity type string to show
|
||||
if isHidden, formattedActivityType := formatActivity(item.ActivityType); isHidden {
|
||||
continue
|
||||
cfg, err := as.configService.GetConfigByID(ctx, act.ActivityType)
|
||||
if err != nil {
|
||||
log.Errorf("fail to get config by id: %d, err: %v, act id is: %s", act.ActivityType, err, act.ID)
|
||||
} else {
|
||||
item.ActivityType = formattedActivityType
|
||||
// database save activity type is number, change to activity type string is like "question.asked".
|
||||
// so we need to cut the front part of '.', only need string like 'asked'
|
||||
_, item.ActivityType, _ = strings.Cut(cfg.Key, ".")
|
||||
// format activity type string to show
|
||||
if isHidden, formattedActivityType := formatActivity(item.ActivityType); isHidden {
|
||||
continue
|
||||
} else {
|
||||
item.ActivityType = formattedActivityType
|
||||
}
|
||||
}
|
||||
|
||||
// if activity is down vote, only admin can see who does it.
|
||||
|
|
|
@ -6,5 +6,5 @@ type FollowRepo interface {
|
|||
GetFollowIDs(ctx context.Context, userID, objectType string) (followIDs []string, err error)
|
||||
GetFollowAmount(ctx context.Context, objectID string) (followAmount int, err error)
|
||||
GetFollowUserIDs(ctx context.Context, objectID string) (userIDs []string, err error)
|
||||
IsFollowed(userId, objectId string) (bool, error)
|
||||
IsFollowed(ctx context.Context, userId, objectId string) (bool, error)
|
||||
}
|
||||
|
|
|
@ -1,60 +1,55 @@
|
|||
package activity_type
|
||||
|
||||
import (
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
)
|
||||
|
||||
const (
|
||||
QuestionVoteUp = "question.vote_up"
|
||||
QuestionVoteDown = "question.vote_down"
|
||||
AnswerVoteUp = "answer.vote_up"
|
||||
AnswerVoteDown = "answer.vote_down"
|
||||
CommentVoteUp = "comment.vote_up"
|
||||
CommentVoteDown = "comment.vote_down"
|
||||
AnswerAccepted = "answer.accepted"
|
||||
AnswerAccept = "answer.accept"
|
||||
QuestionVotedUp = "question.voted_up"
|
||||
QuestionVotedDown = "question.voted_down"
|
||||
AnswerVoteUp = "answer.vote_up"
|
||||
AnswerVoteDown = "answer.vote_down"
|
||||
AnswerVotedUp = "answer.voted_up"
|
||||
AnswerVotedDown = "answer.voted_down"
|
||||
AnswerAccepted = "answer.accepted"
|
||||
AnswerAccept = "answer.accept"
|
||||
CommentVoteUp = "comment.vote_up"
|
||||
)
|
||||
|
||||
var (
|
||||
ActivityTypeList = []string{
|
||||
QuestionVoteUp,
|
||||
QuestionVoteDown,
|
||||
AnswerVoteUp,
|
||||
AnswerVoteDown,
|
||||
CommentVoteUp,
|
||||
CommentVoteDown,
|
||||
AnswerAccepted,
|
||||
AnswerAccept,
|
||||
QuestionVotedUp,
|
||||
QuestionVotedDown,
|
||||
AnswerVoteUp,
|
||||
AnswerVoteDown,
|
||||
AnswerVotedUp,
|
||||
AnswerVotedDown,
|
||||
AnswerAccepted,
|
||||
AnswerAccept,
|
||||
CommentVoteUp,
|
||||
}
|
||||
activityTypeFlagMapping = map[string]string{
|
||||
QuestionVoteUp: "upvote",
|
||||
QuestionVoteDown: "downvote",
|
||||
AnswerVoteUp: "upvote",
|
||||
AnswerVoteDown: "downvote",
|
||||
CommentVoteUp: "upvote",
|
||||
CommentVoteDown: "downvote",
|
||||
AnswerAccepted: "accepted",
|
||||
AnswerAccept: "accept",
|
||||
QuestionVotedUp: "upvoted",
|
||||
QuestionVotedDown: "downvoted",
|
||||
AnswerVotedUp: "upvoted",
|
||||
AnswerVotedDown: "downvoted",
|
||||
VoteActivityTypeList = []string{
|
||||
QuestionVoteUp,
|
||||
QuestionVoteDown,
|
||||
QuestionVotedUp,
|
||||
QuestionVotedDown,
|
||||
AnswerVoteUp,
|
||||
AnswerVoteDown,
|
||||
AnswerVotedUp,
|
||||
AnswerVotedDown,
|
||||
CommentVoteUp,
|
||||
}
|
||||
ActivityTypeFlagMapping = map[string]string{
|
||||
QuestionVoteUp: "action_activity_type.upvote",
|
||||
QuestionVoteDown: "action_activity_type.downvote",
|
||||
QuestionVotedUp: "action_activity_type.upvoted",
|
||||
QuestionVotedDown: "action_activity_type.downvoted",
|
||||
AnswerVoteUp: "action_activity_type.upvote",
|
||||
AnswerVoteDown: "action_activity_type.downvote",
|
||||
AnswerVotedUp: "action_activity_type.upvoted",
|
||||
AnswerVotedDown: "action_activity_type.downvoted",
|
||||
AnswerAccepted: "action_activity_type.accepted",
|
||||
AnswerAccept: "action_activity_type.accept",
|
||||
CommentVoteUp: "action_activity_type.upvote",
|
||||
}
|
||||
)
|
||||
|
||||
func Format(activityTypeID int) string {
|
||||
activityTypeStr := config.ID2KeyMapping[activityTypeID]
|
||||
activityTypeFlag := activityTypeFlagMapping[activityTypeStr]
|
||||
if len(activityTypeFlag) == 0 {
|
||||
return "edit" // to edit
|
||||
}
|
||||
return activityTypeFlag // todo i18n support
|
||||
}
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
)
|
||||
|
||||
// ConfigRepo config repository
|
||||
type ConfigRepo interface {
|
||||
Get(key string) (interface{}, error)
|
||||
GetString(key string) (string, error)
|
||||
GetInt(key string) (int, error)
|
||||
GetArrayString(key string) ([]string, error)
|
||||
GetConfigType(key string) (int, error)
|
||||
GetJsonConfigByIDAndSetToObject(id int, value any) (err error)
|
||||
SetConfig(key, value string) (err error)
|
||||
GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error)
|
||||
GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error)
|
||||
UpdateConfig(ctx context.Context, key, value string) (err error)
|
||||
}
|
||||
|
||||
// ConfigService user service
|
||||
|
@ -16,8 +20,70 @@ type ConfigService struct {
|
|||
configRepo ConfigRepo
|
||||
}
|
||||
|
||||
// NewConfigService new config service
|
||||
func NewConfigService(configRepo ConfigRepo) *ConfigService {
|
||||
return &ConfigService{
|
||||
configRepo: configRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// GetIntValue get config int value
|
||||
func (cs *ConfigService) GetIntValue(ctx context.Context, key string) (val int, err error) {
|
||||
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return cf.GetIntValue(), nil
|
||||
}
|
||||
|
||||
// GetStringValue get config string value
|
||||
func (cs *ConfigService) GetStringValue(ctx context.Context, key string) (val string, err error) {
|
||||
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return cf.Value, nil
|
||||
}
|
||||
|
||||
// GetArrayStringValue get config array string value
|
||||
func (cs *ConfigService) GetArrayStringValue(ctx context.Context, key string) (val []string, err error) {
|
||||
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return cf.GetArrayStringValue(), nil
|
||||
}
|
||||
|
||||
func (cs *ConfigService) GetJsonConfigByIDAndSetToObject(ctx context.Context, id int, obj any) (err error) {
|
||||
cf, err := cs.configRepo.GetConfigByID(ctx, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = json.Unmarshal([]byte(cf.Value), obj)
|
||||
if err != nil {
|
||||
return fmt.Errorf("[%s] config value is not json format", cf.Key)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetConfigByID get config by id
|
||||
func (cs *ConfigService) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
|
||||
return cs.configRepo.GetConfigByID(ctx, id)
|
||||
}
|
||||
|
||||
func (cs *ConfigService) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
|
||||
return cs.configRepo.GetConfigByKey(ctx, key)
|
||||
}
|
||||
|
||||
// GetIDByKey get config id by key
|
||||
func (cs *ConfigService) GetIDByKey(ctx context.Context, key string) (id int, err error) {
|
||||
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return cf.ID, nil
|
||||
}
|
||||
|
||||
func (cs *ConfigService) UpdateConfig(ctx context.Context, key, value string) (err error) {
|
||||
return cs.configRepo.UpdateConfig(ctx, key, value)
|
||||
}
|
||||
|
|
|
@ -35,7 +35,7 @@ type DashboardService struct {
|
|||
voteRepo activity_common.VoteRepo
|
||||
userRepo usercommon.UserRepo
|
||||
reportRepo report_common.ReportRepo
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
siteInfoService *siteinfo_common.SiteInfoCommonService
|
||||
serviceConfig *service_config.ServiceConfig
|
||||
|
||||
|
@ -49,7 +49,7 @@ func NewDashboardService(
|
|||
voteRepo activity_common.VoteRepo,
|
||||
userRepo usercommon.UserRepo,
|
||||
reportRepo report_common.ReportRepo,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
siteInfoService *siteinfo_common.SiteInfoCommonService,
|
||||
serviceConfig *service_config.ServiceConfig,
|
||||
|
||||
|
@ -62,7 +62,7 @@ func NewDashboardService(
|
|||
voteRepo: voteRepo,
|
||||
userRepo: userRepo,
|
||||
reportRepo: reportRepo,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
siteInfoService: siteInfoService,
|
||||
serviceConfig: serviceConfig,
|
||||
|
||||
|
@ -131,12 +131,11 @@ func (ds *DashboardService) Statistical(ctx context.Context) (*schema.DashboardI
|
|||
var activityTypes []int
|
||||
|
||||
for _, typeKey := range typeKeys {
|
||||
var t int
|
||||
t, err = ds.configRepo.GetConfigType(typeKey)
|
||||
cfg, err := ds.configService.GetConfigByKey(ctx, typeKey)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
activityTypes = append(activityTypes, t)
|
||||
activityTypes = append(activityTypes, cfg.ID)
|
||||
}
|
||||
|
||||
voteCount, err := ds.voteRepo.GetVoteCount(ctx, activityTypes)
|
||||
|
@ -166,7 +165,7 @@ func (ds *DashboardService) Statistical(ctx context.Context) (*schema.DashboardI
|
|||
dashboardInfo.ReportCount = reportCount
|
||||
|
||||
dashboardInfo.UploadingFiles = true
|
||||
emailconfig, err := ds.GetEmailConfig()
|
||||
emailconfig, err := ds.GetEmailConfig(ctx)
|
||||
if err != nil {
|
||||
return dashboardInfo, err
|
||||
}
|
||||
|
@ -225,8 +224,8 @@ func (ds *DashboardService) RemoteVersion(ctx context.Context) string {
|
|||
return remoteVersion.Release.Version
|
||||
}
|
||||
|
||||
func (ds *DashboardService) GetEmailConfig() (ec *export.EmailConfig, err error) {
|
||||
emailConf, err := ds.configRepo.GetString("email.config")
|
||||
func (ds *DashboardService) GetEmailConfig(ctx context.Context) (ec *export.EmailConfig, err error) {
|
||||
emailConf, err := ds.configService.GetStringValue(ctx, "email.config")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
@ -24,9 +24,9 @@ import (
|
|||
|
||||
// EmailService kit service
|
||||
type EmailService struct {
|
||||
configRepo config.ConfigRepo
|
||||
emailRepo EmailRepo
|
||||
siteInfoRepo siteinfo_common.SiteInfoRepo
|
||||
configService *config.ConfigService
|
||||
emailRepo EmailRepo
|
||||
siteInfoRepo siteinfo_common.SiteInfoRepo
|
||||
}
|
||||
|
||||
// EmailRepo email repository
|
||||
|
@ -36,11 +36,11 @@ type EmailRepo interface {
|
|||
}
|
||||
|
||||
// NewEmailService email service
|
||||
func NewEmailService(configRepo config.ConfigRepo, emailRepo EmailRepo, siteInfoRepo siteinfo_common.SiteInfoRepo) *EmailService {
|
||||
func NewEmailService(configService *config.ConfigService, emailRepo EmailRepo, siteInfoRepo siteinfo_common.SiteInfoRepo) *EmailService {
|
||||
return &EmailService{
|
||||
configRepo: configRepo,
|
||||
emailRepo: emailRepo,
|
||||
siteInfoRepo: siteInfoRepo,
|
||||
configService: configService,
|
||||
emailRepo: emailRepo,
|
||||
siteInfoRepo: siteInfoRepo,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,7 +114,7 @@ func (es *EmailService) SendAndSaveCodeWithTime(
|
|||
// Send email send
|
||||
func (es *EmailService) Send(ctx context.Context, toEmailAddr, subject, body string) {
|
||||
log.Infof("try to send email to %s", toEmailAddr)
|
||||
ec, err := es.GetEmailConfig()
|
||||
ec, err := es.GetEmailConfig(ctx)
|
||||
if err != nil {
|
||||
log.Errorf("get email config failed: %s", err)
|
||||
return
|
||||
|
@ -298,8 +298,8 @@ func (es *EmailService) NewCommentTemplate(ctx context.Context, raw *schema.NewC
|
|||
return title, body, nil
|
||||
}
|
||||
|
||||
func (es *EmailService) GetEmailConfig() (ec *EmailConfig, err error) {
|
||||
emailConf, err := es.configRepo.GetString("email.config")
|
||||
func (es *EmailService) GetEmailConfig(ctx context.Context) (ec *EmailConfig, err error) {
|
||||
emailConf, err := es.configService.GetStringValue(ctx, "email.config")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -312,7 +312,7 @@ func (es *EmailService) GetEmailConfig() (ec *EmailConfig, err error) {
|
|||
}
|
||||
|
||||
// SetEmailConfig set email config
|
||||
func (es *EmailService) SetEmailConfig(ec *EmailConfig) (err error) {
|
||||
func (es *EmailService) SetEmailConfig(ctx context.Context, ec *EmailConfig) (err error) {
|
||||
data, _ := json.Marshal(ec)
|
||||
return es.configRepo.SetConfig("email.config", string(data))
|
||||
return es.configService.UpdateConfig(ctx, "email.config", string(data))
|
||||
}
|
||||
|
|
|
@ -21,17 +21,17 @@ type PluginConfigRepo interface {
|
|||
|
||||
// PluginCommonService user service
|
||||
type PluginCommonService struct {
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
pluginConfigRepo PluginConfigRepo
|
||||
}
|
||||
|
||||
// NewPluginCommonService new report service
|
||||
func NewPluginCommonService(
|
||||
pluginConfigRepo PluginConfigRepo,
|
||||
configRepo config.ConfigRepo) *PluginCommonService {
|
||||
configService *config.ConfigService) *PluginCommonService {
|
||||
|
||||
// init plugin status
|
||||
pluginStatus, err := configRepo.GetString(constant.PluginStatus)
|
||||
pluginStatus, err := configService.GetStringValue(context.TODO(), constant.PluginStatus)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
} else {
|
||||
|
@ -59,7 +59,7 @@ func NewPluginCommonService(
|
|||
}
|
||||
|
||||
return &PluginCommonService{
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
pluginConfigRepo: pluginConfigRepo,
|
||||
}
|
||||
}
|
||||
|
@ -70,7 +70,7 @@ func (ps *PluginCommonService) UpdatePluginStatus(ctx context.Context) (err erro
|
|||
if err != nil {
|
||||
return errors.InternalServer(reason.UnknownError).WithError(err)
|
||||
}
|
||||
return ps.configRepo.SetConfig(constant.PluginStatus, string(content))
|
||||
return ps.configService.UpdateConfig(ctx, constant.PluginStatus, string(content))
|
||||
}
|
||||
|
||||
// UpdatePluginConfig update plugin config
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
collectioncommon "github.com/answerdev/answer/internal/service/collection_common"
|
||||
"github.com/answerdev/answer/internal/service/comment"
|
||||
"github.com/answerdev/answer/internal/service/comment_common"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/internal/service/dashboard"
|
||||
"github.com/answerdev/answer/internal/service/export"
|
||||
"github.com/answerdev/answer/internal/service/follow"
|
||||
|
@ -84,4 +85,5 @@ var ProviderSetService = wire.NewSet(
|
|||
user_external_login.NewUserExternalLoginService,
|
||||
user_external_login.NewUserCenterLoginService,
|
||||
plugin_common.NewPluginCommonService,
|
||||
config.NewConfigService,
|
||||
)
|
||||
|
|
|
@ -6,6 +6,7 @@ import (
|
|||
"time"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/base/handler"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/service/activity_common"
|
||||
"github.com/answerdev/answer/internal/service/activity_queue"
|
||||
|
@ -62,7 +63,7 @@ type QuestionCommon struct {
|
|||
collectionCommon *collectioncommon.CollectionCommon
|
||||
AnswerCommon *answercommon.AnswerCommon
|
||||
metaService *meta.MetaService
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
func NewQuestionCommon(questionRepo QuestionRepo,
|
||||
|
@ -74,7 +75,7 @@ func NewQuestionCommon(questionRepo QuestionRepo,
|
|||
collectionCommon *collectioncommon.CollectionCommon,
|
||||
answerCommon *answercommon.AnswerCommon,
|
||||
metaService *meta.MetaService,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
) *QuestionCommon {
|
||||
return &QuestionCommon{
|
||||
questionRepo: questionRepo,
|
||||
|
@ -86,7 +87,7 @@ func NewQuestionCommon(questionRepo QuestionRepo,
|
|||
collectionCommon: collectionCommon,
|
||||
AnswerCommon: answerCommon,
|
||||
metaService: metaService,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -201,14 +202,16 @@ func (qs *QuestionCommon) Info(ctx context.Context, questionID string, loginUser
|
|||
if err != nil {
|
||||
log.Error("json.Unmarshal CloseQuestionMeta error", err.Error())
|
||||
} else {
|
||||
closeinfo := &schema.GetReportTypeResp{}
|
||||
err = qs.configRepo.GetJsonConfigByIDAndSetToObject(closemsg.CloseType, closeinfo)
|
||||
cfg, err := qs.configService.GetConfigByID(ctx, closemsg.CloseType)
|
||||
if err != nil {
|
||||
log.Error("json.Unmarshal QuestionCloseJson error", err.Error())
|
||||
} else {
|
||||
reasonItem := &schema.ReasonItem{}
|
||||
_ = json.Unmarshal(cfg.GetByteValue(), reasonItem)
|
||||
reasonItem.Translate(cfg.Key, handler.GetLangByCtx(ctx))
|
||||
operation := &schema.Operation{}
|
||||
operation.Type = closeinfo.Name
|
||||
operation.Description = closeinfo.Description
|
||||
operation.Type = reasonItem.Name
|
||||
operation.Description = reasonItem.Description
|
||||
operation.Msg = closemsg.CloseMsg
|
||||
operation.Time = metainfo.CreatedAt.Unix()
|
||||
operation.Level = schema.OperationLevelInfo
|
||||
|
@ -259,7 +262,7 @@ func (qs *QuestionCommon) Info(ctx context.Context, questionID string, loginUser
|
|||
showinfo.VoteStatus = qs.voteRepo.GetVoteStatus(ctx, questionID, loginUserID)
|
||||
|
||||
// // check is followed
|
||||
isFollowed, _ := qs.followCommon.IsFollowed(loginUserID, questionID)
|
||||
isFollowed, _ := qs.followCommon.IsFollowed(ctx, loginUserID, questionID)
|
||||
showinfo.IsFollowed = isFollowed
|
||||
|
||||
has, err = qs.AnswerCommon.SearchAnswered(ctx, loginUserID, dbinfo.ID)
|
||||
|
|
|
@ -304,11 +304,11 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
|
|||
// user add question count
|
||||
userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, question.UserID)
|
||||
if err != nil {
|
||||
log.Error("user GetUserQuestionCount error", err.Error())
|
||||
log.Errorf("get user question count error %v", err)
|
||||
} else {
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, userQuestionCount)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
log.Errorf("update user question count error %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -4,8 +4,10 @@ import (
|
|||
"context"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/base/handler"
|
||||
"github.com/answerdev/answer/internal/base/pager"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/base/translator"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
"github.com/answerdev/answer/internal/service/activity_type"
|
||||
|
@ -34,7 +36,7 @@ type UserRankRepo interface {
|
|||
// RankService rank service
|
||||
type RankService struct {
|
||||
userCommon *usercommon.UserCommon
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
userRankRepo UserRankRepo
|
||||
objectInfoService *object_info.ObjService
|
||||
roleService *role.UserRoleRelService
|
||||
|
@ -48,10 +50,10 @@ func NewRankService(
|
|||
objectInfoService *object_info.ObjService,
|
||||
roleService *role.UserRoleRelService,
|
||||
rolePowerService *role.RolePowerRelService,
|
||||
configRepo config.ConfigRepo) *RankService {
|
||||
configService *config.ConfigService) *RankService {
|
||||
return &RankService{
|
||||
userCommon: userCommon,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
userRankRepo: userRankRepo,
|
||||
objectInfoService: objectInfoService,
|
||||
roleService: roleService,
|
||||
|
@ -221,7 +223,7 @@ func (rs *RankService) getUserPowerMapping(ctx context.Context, userID string) (
|
|||
func (rs *RankService) checkUserRank(ctx context.Context, userID string, userRank int, action string) (
|
||||
can bool, rank int) {
|
||||
// get the amount of rank required for the current operation
|
||||
requireRank, err := rs.configRepo.GetInt(action)
|
||||
requireRank, err := rs.configService.GetIntValue(ctx, action)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return false, requireRank
|
||||
|
@ -234,8 +236,8 @@ func (rs *RankService) checkUserRank(ctx context.Context, userID string, userRan
|
|||
return true, requireRank
|
||||
}
|
||||
|
||||
// GetRankPersonalWithPage get personal comment list page
|
||||
func (rs *RankService) GetRankPersonalWithPage(ctx context.Context, req *schema.GetRankPersonalWithPageReq) (
|
||||
// GetRankPersonalPage get personal comment list page
|
||||
func (rs *RankService) GetRankPersonalPage(ctx context.Context, req *schema.GetRankPersonalWithPageReq) (
|
||||
pageModel *pager.PageModel, err error) {
|
||||
if plugin.RankAgentEnabled() {
|
||||
return pager.NewPageModel(0, []string{}), nil
|
||||
|
@ -258,32 +260,47 @@ func (rs *RankService) GetRankPersonalWithPage(ctx context.Context, req *schema.
|
|||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
resp := make([]*schema.GetRankPersonalWithPageResp, 0)
|
||||
|
||||
resp := rs.decorateRankPersonalPageResp(ctx, userRankPage)
|
||||
return pager.NewPageModel(total, resp), nil
|
||||
}
|
||||
|
||||
func (rs *RankService) decorateRankPersonalPageResp(
|
||||
ctx context.Context, userRankPage []*entity.Activity) []*schema.GetRankPersonalPageResp {
|
||||
resp := make([]*schema.GetRankPersonalPageResp, 0)
|
||||
lang := handler.GetLangByCtx(ctx)
|
||||
|
||||
for _, userRankInfo := range userRankPage {
|
||||
if len(userRankInfo.ObjectID) == 0 || userRankInfo.ObjectID == "0" {
|
||||
continue
|
||||
}
|
||||
commentResp := &schema.GetRankPersonalWithPageResp{
|
||||
objInfo, err := rs.objectInfoService.GetInfo(ctx, userRankInfo.ObjectID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
commentResp := &schema.GetRankPersonalPageResp{
|
||||
CreatedAt: userRankInfo.CreatedAt.Unix(),
|
||||
ObjectID: userRankInfo.ObjectID,
|
||||
Reputation: userRankInfo.Rank,
|
||||
}
|
||||
objInfo, err := rs.objectInfoService.GetInfo(ctx, userRankInfo.ObjectID)
|
||||
cfg, err := rs.configService.GetConfigByID(ctx, userRankInfo.ActivityType)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
} else {
|
||||
commentResp.RankType = activity_type.Format(userRankInfo.ActivityType)
|
||||
commentResp.ObjectType = objInfo.ObjectType
|
||||
commentResp.Title = objInfo.Title
|
||||
commentResp.UrlTitle = htmltext.UrlTitle(objInfo.Title)
|
||||
commentResp.Content = objInfo.Content
|
||||
if objInfo.QuestionStatus == entity.QuestionStatusDeleted {
|
||||
commentResp.Title = "Deleted question"
|
||||
}
|
||||
commentResp.QuestionID = objInfo.QuestionID
|
||||
commentResp.AnswerID = objInfo.AnswerID
|
||||
continue
|
||||
}
|
||||
commentResp.RankType = translator.Tr(lang, activity_type.ActivityTypeFlagMapping[cfg.Key])
|
||||
commentResp.ObjectType = objInfo.ObjectType
|
||||
commentResp.Title = objInfo.Title
|
||||
commentResp.UrlTitle = htmltext.UrlTitle(objInfo.Title)
|
||||
commentResp.Content = objInfo.Content
|
||||
if objInfo.QuestionStatus == entity.QuestionStatusDeleted {
|
||||
commentResp.Title = translator.Tr(lang, constant.DeletedQuestionTitleTrKey)
|
||||
}
|
||||
commentResp.QuestionID = objInfo.QuestionID
|
||||
commentResp.AnswerID = objInfo.AnswerID
|
||||
resp = append(resp, commentResp)
|
||||
}
|
||||
return pager.NewPageModel(total, resp), nil
|
||||
return resp
|
||||
}
|
||||
|
|
|
@ -2,17 +2,17 @@ package report_admin
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/handler"
|
||||
"github.com/answerdev/answer/internal/repo/config"
|
||||
configrepo "github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/internal/service/config"
|
||||
"github.com/answerdev/answer/internal/service/object_info"
|
||||
"github.com/answerdev/answer/pkg/htmltext"
|
||||
"github.com/segmentfault/pacman/log"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/pager"
|
||||
"github.com/answerdev/answer/internal/base/reason"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/repo/common"
|
||||
"github.com/answerdev/answer/internal/schema"
|
||||
answercommon "github.com/answerdev/answer/internal/service/answer_common"
|
||||
"github.com/answerdev/answer/internal/service/comment_common"
|
||||
|
@ -28,33 +28,33 @@ import (
|
|||
type ReportAdminService struct {
|
||||
reportRepo report_common.ReportRepo
|
||||
commonUser *usercommon.UserCommon
|
||||
commonRepo *common.CommonRepo
|
||||
answerRepo answercommon.AnswerRepo
|
||||
questionRepo questioncommon.QuestionRepo
|
||||
commentCommonRepo comment_common.CommentCommonRepo
|
||||
reportHandle *report_handle_admin.ReportHandle
|
||||
configRepo configrepo.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
objectInfoService *object_info.ObjService
|
||||
}
|
||||
|
||||
// NewReportAdminService new report service
|
||||
func NewReportAdminService(
|
||||
reportRepo report_common.ReportRepo,
|
||||
commonUser *usercommon.UserCommon,
|
||||
commonRepo *common.CommonRepo,
|
||||
answerRepo answercommon.AnswerRepo,
|
||||
questionRepo questioncommon.QuestionRepo,
|
||||
commentCommonRepo comment_common.CommentCommonRepo,
|
||||
reportHandle *report_handle_admin.ReportHandle,
|
||||
configRepo configrepo.ConfigRepo) *ReportAdminService {
|
||||
configService *config.ConfigService,
|
||||
objectInfoService *object_info.ObjService) *ReportAdminService {
|
||||
return &ReportAdminService{
|
||||
reportRepo: reportRepo,
|
||||
commonUser: commonUser,
|
||||
commonRepo: commonRepo,
|
||||
answerRepo: answerRepo,
|
||||
questionRepo: questionRepo,
|
||||
commentCommonRepo: commentCommonRepo,
|
||||
reportHandle: reportHandle,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
objectInfoService: objectInfoService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -98,9 +98,8 @@ func (rs *ReportAdminService) ListReportPage(ctx context.Context, dto schema.Get
|
|||
for _, r := range resp {
|
||||
r.ReportedUser = flaggedUsers[r.ReportedUserID]
|
||||
r.ReportUser = users[r.UserID]
|
||||
rs.decorateReportResp(ctx, r)
|
||||
}
|
||||
|
||||
rs.parseObject(ctx, &resp)
|
||||
return pager.NewPageModel(total, resp), nil
|
||||
}
|
||||
|
||||
|
@ -139,102 +138,38 @@ func (rs *ReportAdminService) HandleReported(ctx context.Context, req schema.Rep
|
|||
return
|
||||
}
|
||||
|
||||
func (rs *ReportAdminService) parseObject(ctx context.Context, resp *[]*schema.GetReportListPageResp) {
|
||||
func (rs *ReportAdminService) decorateReportResp(ctx context.Context, resp *schema.GetReportListPageResp) {
|
||||
lang := handler.GetLangByCtx(ctx)
|
||||
var (
|
||||
res = *resp
|
||||
)
|
||||
objectInfo, err := rs.objectInfoService.GetInfo(ctx, resp.ObjectID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
return
|
||||
}
|
||||
|
||||
for i, r := range res {
|
||||
var (
|
||||
objIds map[string]string
|
||||
exists,
|
||||
ok bool
|
||||
err error
|
||||
questionId,
|
||||
answerId,
|
||||
commentId string
|
||||
question *entity.Question
|
||||
answer *entity.Answer
|
||||
cmt *entity.Comment
|
||||
)
|
||||
resp.QuestionID = objectInfo.QuestionID
|
||||
resp.AnswerID = objectInfo.AnswerID
|
||||
resp.CommentID = objectInfo.CommentID
|
||||
resp.Title = objectInfo.Title
|
||||
resp.Excerpt = htmltext.FetchExcerpt(objectInfo.Content, "...", 240)
|
||||
|
||||
objIds, err = rs.commonRepo.GetObjectIDMap(r.ObjectID)
|
||||
if resp.ReportType > 0 {
|
||||
resp.Reason = &schema.ReasonItem{ReasonType: resp.ReportType}
|
||||
cf, err := rs.configService.GetConfigByID(ctx, resp.ReportType)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
} else {
|
||||
_ = json.Unmarshal([]byte(cf.Value), resp.Reason)
|
||||
resp.Reason.Translate(cf.Key, lang)
|
||||
}
|
||||
|
||||
questionId, ok = objIds["question"]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
if resp.FlaggedType > 0 {
|
||||
resp.FlaggedReason = &schema.ReasonItem{ReasonType: resp.FlaggedType}
|
||||
cf, err := rs.configService.GetConfigByID(ctx, resp.FlaggedType)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
} else {
|
||||
_ = json.Unmarshal([]byte(cf.Value), resp.Reason)
|
||||
resp.Reason.Translate(cf.Key, lang)
|
||||
}
|
||||
|
||||
question, exists, err = rs.questionRepo.GetQuestion(ctx, questionId)
|
||||
if err != nil || !exists {
|
||||
continue
|
||||
}
|
||||
|
||||
answerId, ok = objIds["answer"]
|
||||
if ok {
|
||||
answer, _, err = rs.answerRepo.GetAnswer(ctx, answerId)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
commentId, ok = objIds["comment"]
|
||||
if ok {
|
||||
cmt, _, err = rs.commentCommonRepo.GetComment(ctx, commentId)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
switch r.OType {
|
||||
case "question":
|
||||
r.QuestionID = questionId
|
||||
r.Title = question.Title
|
||||
r.Excerpt = htmltext.FetchExcerpt(question.ParsedText, "...", 240)
|
||||
|
||||
case "answer":
|
||||
r.QuestionID = questionId
|
||||
r.AnswerID = answerId
|
||||
r.Title = question.Title
|
||||
r.Excerpt = htmltext.FetchExcerpt(answer.ParsedText, "...", 240)
|
||||
|
||||
case "comment":
|
||||
r.QuestionID = questionId
|
||||
r.AnswerID = answerId
|
||||
r.CommentID = commentId
|
||||
r.Title = question.Title
|
||||
r.Excerpt = htmltext.FetchExcerpt(cmt.ParsedText, "...", 240)
|
||||
}
|
||||
|
||||
// parse reason
|
||||
if r.ReportType > 0 {
|
||||
r.Reason = &schema.ReasonItem{
|
||||
ReasonType: r.ReportType,
|
||||
}
|
||||
err = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.ReportType, r.Reason)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
r.Reason.Translate(config.ID2KeyMapping[r.ReportType], lang)
|
||||
}
|
||||
if r.FlaggedType > 0 {
|
||||
r.FlaggedReason = &schema.ReasonItem{
|
||||
ReasonType: r.FlaggedType,
|
||||
}
|
||||
err = rs.configRepo.GetJsonConfigByIDAndSetToObject(r.FlaggedType, r.FlaggedReason)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
r.Reason.Translate(config.ID2KeyMapping[r.ReportType], lang)
|
||||
}
|
||||
|
||||
res[i] = r
|
||||
}
|
||||
}
|
||||
|
|
|
@ -17,28 +17,34 @@ import (
|
|||
type ReportHandle struct {
|
||||
questionCommon *questioncommon.QuestionCommon
|
||||
commentRepo comment.CommentRepo
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
func NewReportHandle(
|
||||
questionCommon *questioncommon.QuestionCommon,
|
||||
commentRepo comment.CommentRepo,
|
||||
configRepo config.ConfigRepo) *ReportHandle {
|
||||
configService *config.ConfigService) *ReportHandle {
|
||||
return &ReportHandle{
|
||||
questionCommon: questionCommon,
|
||||
commentRepo: commentRepo,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
// HandleObject this handle object status
|
||||
func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Report, req schema.ReportHandleReq) (err error) {
|
||||
reasonDeleteCfg, err := rh.configService.GetConfigByKey(ctx, "reason.needs_delete")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
reasonCloseCfg, err := rh.configService.GetConfigByKey(ctx, "reason.needs_close")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var (
|
||||
objectID = reported.ObjectID
|
||||
reportedUserID = reported.ReportedUserID
|
||||
objectKey string
|
||||
reasonDelete, _ = rh.configRepo.GetConfigType("reason.needs_delete")
|
||||
reasonClose, _ = rh.configRepo.GetConfigType("reason.needs_close")
|
||||
objectID = reported.ObjectID
|
||||
reportedUserID = reported.ReportedUserID
|
||||
objectKey string
|
||||
)
|
||||
|
||||
objectKey, err = obj.GetObjectTypeStrByObjectID(objectID)
|
||||
|
@ -48,9 +54,9 @@ func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Repor
|
|||
switch objectKey {
|
||||
case "question":
|
||||
switch req.FlaggedType {
|
||||
case reasonDelete:
|
||||
case reasonDeleteCfg.ID:
|
||||
err = rh.questionCommon.RemoveQuestion(ctx, &schema.RemoveQuestionReq{ID: objectID})
|
||||
case reasonClose:
|
||||
case reasonCloseCfg.ID:
|
||||
err = rh.questionCommon.CloseQuestion(ctx, &schema.CloseQuestionReq{
|
||||
ID: objectID,
|
||||
CloseType: req.FlaggedType,
|
||||
|
@ -59,12 +65,12 @@ func (rh *ReportHandle) HandleObject(ctx context.Context, reported *entity.Repor
|
|||
}
|
||||
case "answer":
|
||||
switch req.FlaggedType {
|
||||
case reasonDelete:
|
||||
case reasonDeleteCfg.ID:
|
||||
err = rh.questionCommon.RemoveAnswer(ctx, objectID)
|
||||
}
|
||||
case "comment":
|
||||
switch req.FlaggedType {
|
||||
case reasonDelete:
|
||||
case reasonCloseCfg.ID:
|
||||
err = rh.commentRepo.RemoveComment(ctx, objectID)
|
||||
rh.sendNotification(ctx, reportedUserID, objectID, constant.NotificationYourCommentWasDeleted)
|
||||
}
|
||||
|
|
|
@ -26,7 +26,7 @@ type SiteInfoService struct {
|
|||
siteInfoCommonService *siteinfo_common.SiteInfoCommonService
|
||||
emailService *export.EmailService
|
||||
tagCommonService *tagcommon.TagCommonService
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
}
|
||||
|
||||
func NewSiteInfoService(
|
||||
|
@ -34,7 +34,7 @@ func NewSiteInfoService(
|
|||
siteInfoCommonService *siteinfo_common.SiteInfoCommonService,
|
||||
emailService *export.EmailService,
|
||||
tagCommonService *tagcommon.TagCommonService,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
) *SiteInfoService {
|
||||
usersSiteInfo, _ := siteInfoCommonService.GetSiteUsers(context.Background())
|
||||
if usersSiteInfo != nil {
|
||||
|
@ -51,7 +51,7 @@ func NewSiteInfoService(
|
|||
siteInfoCommonService: siteInfoCommonService,
|
||||
emailService: emailService,
|
||||
tagCommonService: tagCommonService,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -239,7 +239,7 @@ func (s *SiteInfoService) SaveSiteUsers(ctx context.Context, req *schema.SiteUse
|
|||
func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
|
||||
resp *schema.GetSMTPConfigResp, err error,
|
||||
) {
|
||||
emailConfig, err := s.emailService.GetEmailConfig()
|
||||
emailConfig, err := s.emailService.GetEmailConfig(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -250,13 +250,13 @@ func (s *SiteInfoService) GetSMTPConfig(ctx context.Context) (
|
|||
|
||||
// UpdateSMTPConfig get smtp config
|
||||
func (s *SiteInfoService) UpdateSMTPConfig(ctx context.Context, req *schema.UpdateSMTPConfigReq) (err error) {
|
||||
oldEmailConfig, err := s.emailService.GetEmailConfig()
|
||||
oldEmailConfig, err := s.emailService.GetEmailConfig(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_ = copier.Copy(oldEmailConfig, req)
|
||||
|
||||
err = s.emailService.SetEmailConfig(oldEmailConfig)
|
||||
err = s.emailService.SetEmailConfig(ctx, oldEmailConfig)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -372,7 +372,7 @@ func (s *SiteInfoService) UpdatePrivilegesConfig(ctx context.Context, req *schem
|
|||
|
||||
// update privilege in config
|
||||
for _, privilege := range chooseOption.Privileges {
|
||||
err = s.configRepo.SetConfig(privilege.Key, fmt.Sprintf("%d", privilege.Value))
|
||||
err = s.configService.UpdateConfig(ctx, privilege.Key, fmt.Sprintf("%d", privilege.Value))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -380,7 +380,7 @@ func (ts *TagService) checkTagIsFollow(ctx context.Context, userID, tagID string
|
|||
if len(userID) == 0 {
|
||||
return false
|
||||
}
|
||||
followed, err := ts.followCommon.IsFollowed(userID, tagID)
|
||||
followed, err := ts.followCommon.IsFollowed(ctx, userID, tagID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
|
|
@ -3,7 +3,10 @@ package service
|
|||
import (
|
||||
"context"
|
||||
|
||||
"github.com/answerdev/answer/internal/base/constant"
|
||||
"github.com/answerdev/answer/internal/base/handler"
|
||||
"github.com/answerdev/answer/internal/base/pager"
|
||||
"github.com/answerdev/answer/internal/base/translator"
|
||||
"github.com/answerdev/answer/internal/entity"
|
||||
"github.com/answerdev/answer/internal/service/activity_type"
|
||||
"github.com/answerdev/answer/internal/service/comment_common"
|
||||
|
@ -35,7 +38,7 @@ type VoteRepo interface {
|
|||
type VoteService struct {
|
||||
voteRepo VoteRepo
|
||||
UniqueIDRepo unique.UniqueIDRepo
|
||||
configRepo config.ConfigRepo
|
||||
configService *config.ConfigService
|
||||
questionRepo questioncommon.QuestionRepo
|
||||
answerRepo answercommon.AnswerRepo
|
||||
commentCommonRepo comment_common.CommentCommonRepo
|
||||
|
@ -45,7 +48,7 @@ type VoteService struct {
|
|||
func NewVoteService(
|
||||
VoteRepo VoteRepo,
|
||||
uniqueIDRepo unique.UniqueIDRepo,
|
||||
configRepo config.ConfigRepo,
|
||||
configService *config.ConfigService,
|
||||
questionRepo questioncommon.QuestionRepo,
|
||||
answerRepo answercommon.AnswerRepo,
|
||||
commentCommonRepo comment_common.CommentCommonRepo,
|
||||
|
@ -54,7 +57,7 @@ func NewVoteService(
|
|||
return &VoteService{
|
||||
voteRepo: VoteRepo,
|
||||
UniqueIDRepo: uniqueIDRepo,
|
||||
configRepo: configRepo,
|
||||
configService: configService,
|
||||
questionRepo: questionRepo,
|
||||
answerRepo: answerRepo,
|
||||
commentCommonRepo: commentCommonRepo,
|
||||
|
@ -151,24 +154,22 @@ func (vs *VoteService) GetObjectUserID(ctx context.Context, objectID string) (us
|
|||
|
||||
// ListUserVotes list user's votes
|
||||
func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWithPageReq) (model *pager.PageModel, err error) {
|
||||
var (
|
||||
resp []schema.GetVoteWithPageResp
|
||||
typeKeys = []string{
|
||||
"question.vote_up",
|
||||
"question.vote_down",
|
||||
"answer.vote_up",
|
||||
"answer.vote_down",
|
||||
}
|
||||
activityTypes []int
|
||||
)
|
||||
typeKeys := []string{
|
||||
activity_type.QuestionVoteUp,
|
||||
activity_type.QuestionVoteDown,
|
||||
activity_type.AnswerVoteUp,
|
||||
activity_type.AnswerVoteDown,
|
||||
}
|
||||
activityTypes := make([]int, 0)
|
||||
activityTypeMapping := make(map[int]string, 0)
|
||||
|
||||
for _, typeKey := range typeKeys {
|
||||
var t int
|
||||
t, err = vs.configRepo.GetConfigType(typeKey)
|
||||
cfg, err := vs.configService.GetConfigByKey(ctx, typeKey)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
activityTypes = append(activityTypes, t)
|
||||
activityTypes = append(activityTypes, cfg.ID)
|
||||
activityTypeMapping[cfg.ID] = typeKey
|
||||
}
|
||||
|
||||
voteList, total, err := vs.voteRepo.ListUserVotes(ctx, req.UserID, req, activityTypes)
|
||||
|
@ -176,15 +177,17 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWith
|
|||
return
|
||||
}
|
||||
|
||||
lang := handler.GetLangByCtx(ctx)
|
||||
|
||||
resp := make([]*schema.GetVoteWithPageResp, 0)
|
||||
for _, voteInfo := range voteList {
|
||||
var objInfo *schema.SimpleObjectInfo
|
||||
objInfo, err = vs.objectService.GetInfo(ctx, voteInfo.ObjectID)
|
||||
objInfo, err := vs.objectService.GetInfo(ctx, voteInfo.ObjectID)
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
continue
|
||||
}
|
||||
|
||||
item := schema.GetVoteWithPageResp{
|
||||
item := &schema.GetVoteWithPageResp{
|
||||
CreatedAt: voteInfo.CreatedAt.Unix(),
|
||||
ObjectID: objInfo.ObjectID,
|
||||
QuestionID: objInfo.QuestionID,
|
||||
|
@ -193,13 +196,13 @@ func (vs *VoteService) ListUserVotes(ctx context.Context, req schema.GetVoteWith
|
|||
Title: objInfo.Title,
|
||||
UrlTitle: htmltext.UrlTitle(objInfo.Title),
|
||||
Content: objInfo.Content,
|
||||
VoteType: activity_type.Format(voteInfo.ActivityType),
|
||||
}
|
||||
item.VoteType = translator.Tr(lang,
|
||||
activity_type.ActivityTypeFlagMapping[activityTypeMapping[voteInfo.ActivityType]])
|
||||
if objInfo.QuestionStatus == entity.QuestionStatusDeleted {
|
||||
item.Title = "Deleted question"
|
||||
item.Title = translator.Tr(lang, constant.DeletedQuestionTitleTrKey)
|
||||
}
|
||||
resp = append(resp, item)
|
||||
}
|
||||
|
||||
return pager.NewPageModel(total, resp), err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue