2022-09-27 17:59:05 +08:00
|
|
|
package activity_common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/data"
|
2022-11-02 15:50:32 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/reason"
|
2022-10-24 16:51:05 +08:00
|
|
|
"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"
|
2023-05-26 16:48:40 +08:00
|
|
|
"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,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
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)
|
2022-10-30 21:34:18 +08:00
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
2023-05-25 11:17:10 +08:00
|
|
|
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 {
|
2023-05-26 16:48:40 +08:00
|
|
|
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)
|
2023-05-25 11:17:10 +08:00
|
|
|
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
|
|
|
|
}
|