mirror of https://gitee.com/answerdev/answer.git
update user question count
This commit is contained in:
parent
9bba51b2e2
commit
299b95b2f2
|
@ -219,6 +219,24 @@ func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err
|
|||
return
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (qr *questionRepo) GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error) {
|
||||
questionList := make([]*entity.Question, 0)
|
||||
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).In("id = ?", ids).Count(&questionList)
|
||||
if err != nil {
|
||||
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (qr *questionRepo) GetQuestionIDsPage(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error) {
|
||||
questionIDList = make([]*schema.SiteMapQuestionInfo, 0)
|
||||
rows := make([]*entity.Question, 0)
|
||||
|
|
|
@ -55,6 +55,26 @@ func (ur *userRepo) IncreaseQuestionCount(ctx context.Context, userID string, am
|
|||
return nil
|
||||
}
|
||||
|
||||
func (ur *userRepo) UpdateQuestionCount(ctx context.Context, userID string, count int64) (err error) {
|
||||
user := &entity.User{}
|
||||
user.QuestionCount = int(count)
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Cols("question_count").Update(user)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ur *userRepo) UpdateAnswerCount(ctx context.Context, userID string, count int) (err error) {
|
||||
user := &entity.User{}
|
||||
user.AnswerCount = count
|
||||
_, err = ur.data.DB.Where("id = ?", userID).Cols("answer_count").Update(user)
|
||||
if err != nil {
|
||||
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// UpdateLastLoginDate update last login date
|
||||
func (ur *userRepo) UpdateLastLoginDate(ctx context.Context, userID string) (err error) {
|
||||
user := &entity.User{LastLoginDate: time.Now()}
|
||||
|
|
|
@ -46,6 +46,8 @@ type QuestionRepo interface {
|
|||
FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error)
|
||||
AdminSearchList(ctx context.Context, search *schema.AdminQuestionSearch) ([]*entity.Question, int64, error)
|
||||
GetQuestionCount(ctx context.Context) (count int64, err error)
|
||||
GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error)
|
||||
GetQuestionCountByIDs(ctx context.Context, ids []string) (count int64, err error)
|
||||
GetQuestionIDsPage(ctx context.Context, page, pageSize int) (questionIDList []*schema.SiteMapQuestionInfo, err error)
|
||||
}
|
||||
|
||||
|
@ -88,6 +90,10 @@ func NewQuestionCommon(questionRepo QuestionRepo,
|
|||
}
|
||||
}
|
||||
|
||||
func (qs *QuestionCommon) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {
|
||||
return qs.questionRepo.GetUserQuestionCount(ctx, userID)
|
||||
}
|
||||
|
||||
func (qs *QuestionCommon) UpdatePv(ctx context.Context, questionID string) error {
|
||||
return qs.questionRepo.UpdatePvCount(ctx, questionID)
|
||||
}
|
||||
|
@ -431,14 +437,16 @@ func (qs *QuestionCommon) RemoveQuestion(ctx context.Context, req *schema.Remove
|
|||
return err
|
||||
}
|
||||
|
||||
// user add question count
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
|
||||
userQuestionCount, err := qs.GetUserQuestionCount(ctx, questionInfo.UserID)
|
||||
if err != nil {
|
||||
log.Error("user UpdateQuestionCount error", err.Error())
|
||||
log.Error("user GetUserQuestionCount error", err.Error())
|
||||
} else {
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// todo rank remove
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
@ -304,9 +304,14 @@ func (qs *QuestionService) AddQuestion(ctx context.Context, req *schema.Question
|
|||
}
|
||||
|
||||
// user add question count
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, 1)
|
||||
userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, question.UserID)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
log.Error("user GetUserQuestionCount error", err.Error())
|
||||
} else {
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, question.UserID, userQuestionCount)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
activity_queue.AddActivity(&schema.ActivityMsg{
|
||||
|
@ -419,10 +424,14 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
|
|||
return err
|
||||
}
|
||||
|
||||
// user add question count
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, -1)
|
||||
userQuestionCount, err := qs.questioncommon.GetUserQuestionCount(ctx, questionInfo.UserID)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
log.Error("user GetUserQuestionCount error", err.Error())
|
||||
} else {
|
||||
err = qs.userCommon.UpdateQuestionCount(ctx, questionInfo.UserID, userQuestionCount)
|
||||
if err != nil {
|
||||
log.Error("user IncreaseQuestionCount error", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
err = qs.answerActivityService.DeleteQuestion(ctx, questionInfo.ID, questionInfo.CreatedAt, questionInfo.VoteCount)
|
||||
|
|
|
@ -18,6 +18,8 @@ type UserRepo interface {
|
|||
AddUser(ctx context.Context, user *entity.User) (err error)
|
||||
IncreaseAnswerCount(ctx context.Context, userID string, amount int) (err error)
|
||||
IncreaseQuestionCount(ctx context.Context, userID string, amount int) (err error)
|
||||
UpdateQuestionCount(ctx context.Context, userID string, count int64) (err error)
|
||||
UpdateAnswerCount(ctx context.Context, userID string, count int) (err error)
|
||||
UpdateLastLoginDate(ctx context.Context, userID string) (err error)
|
||||
UpdateEmailStatus(ctx context.Context, userID string, emailStatus int) error
|
||||
UpdateNoticeStatus(ctx context.Context, userID string, noticeStatus int) error
|
||||
|
@ -37,7 +39,9 @@ type UserCommon struct {
|
|||
userRepo UserRepo
|
||||
}
|
||||
|
||||
func NewUserCommon(userRepo UserRepo) *UserCommon {
|
||||
func NewUserCommon(
|
||||
userRepo UserRepo,
|
||||
) *UserCommon {
|
||||
return &UserCommon{
|
||||
userRepo: userRepo,
|
||||
}
|
||||
|
@ -63,11 +67,11 @@ func (us *UserCommon) GetUserBasicInfoByUserName(ctx context.Context, username s
|
|||
}
|
||||
|
||||
func (us *UserCommon) UpdateAnswerCount(ctx context.Context, userID string, num int) error {
|
||||
return us.userRepo.IncreaseAnswerCount(ctx, userID, num)
|
||||
return us.userRepo.UpdateAnswerCount(ctx, userID, num)
|
||||
}
|
||||
|
||||
func (us *UserCommon) UpdateQuestionCount(ctx context.Context, userID string, num int) error {
|
||||
return us.userRepo.IncreaseQuestionCount(ctx, userID, num)
|
||||
func (us *UserCommon) UpdateQuestionCount(ctx context.Context, userID string, num int64) error {
|
||||
return us.userRepo.UpdateQuestionCount(ctx, userID, num)
|
||||
}
|
||||
|
||||
func (us *UserCommon) BatchUserBasicInfoByID(ctx context.Context, IDs []string) (map[string]*schema.UserBasicInfo, error) {
|
||||
|
|
Loading…
Reference in New Issue