answer/internal/repo/activity_common/vote.go

55 lines
1.6 KiB
Go
Raw Permalink Normal View History

2022-09-27 17:59:05 +08:00
package activity_common
import (
"context"
"github.com/answerdev/answer/internal/base/data"
2022-11-02 15:50:32 +08:00
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
"github.com/answerdev/answer/internal/service/activity_common"
2022-11-02 15:50:32 +08:00
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
2022-09-27 17:59:05 +08:00
)
// VoteRepo activity repository
type VoteRepo struct {
data *data.Data
activityRepo activity_common.ActivityRepo
}
// NewVoteRepo new repository
func NewVoteRepo(data *data.Data, activityRepo activity_common.ActivityRepo) activity_common.VoteRepo {
return &VoteRepo{
data: data,
activityRepo: activityRepo,
}
}
func (vr *VoteRepo) GetVoteStatus(ctx context.Context, objectID, userID string) (status string) {
2022-09-27 17:59:05 +08:00
for _, action := range []string{"vote_up", "vote_down"} {
at := &entity.Activity{}
2022-11-01 15:27:37 +08:00
activityType, _, _, err := vr.activityRepo.GetActivityTypeByObjID(ctx, objectID, action)
if err != nil {
return ""
}
has, err := vr.data.DB.Context(ctx).Where("object_id =? AND cancelled=0 AND activity_type=? AND user_id=?", objectID, activityType, userID).Get(at)
2022-09-27 17:59:05 +08:00
if err != nil {
log.Error(err)
2022-09-27 17:59:05 +08:00
return ""
}
if has {
return action
}
}
return ""
}
2022-11-02 15:50:32 +08:00
func (vr *VoteRepo) GetVoteCount(ctx context.Context, activityTypes []int) (count int64, err error) {
list := make([]*entity.Activity, 0)
count, err = vr.data.DB.Context(ctx).Where("cancelled =0").In("activity_type", activityTypes).FindAndCount(&list)
2022-11-02 15:50:32 +08:00
if err != nil {
return count, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}