2022-10-27 15:40:11 +08:00
|
|
|
package question
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-06-28 10:52:36 +08:00
|
|
|
"encoding/json"
|
2022-12-13 10:40:07 +08:00
|
|
|
"fmt"
|
2023-06-28 10:52:36 +08:00
|
|
|
"github.com/segmentfault/pacman/log"
|
2022-11-01 09:12:12 +08:00
|
|
|
"strings"
|
2022-09-28 10:21:04 +08:00
|
|
|
"time"
|
2022-11-01 09:12:12 +08:00
|
|
|
"unicode"
|
2022-11-02 15:50:32 +08:00
|
|
|
|
2023-06-02 16:53:04 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/handler"
|
2022-11-01 09:12:12 +08:00
|
|
|
"xorm.io/builder"
|
2022-09-27 17:59:05 +08:00
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/constant"
|
|
|
|
"github.com/answerdev/answer/internal/base/data"
|
|
|
|
"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/schema"
|
|
|
|
questioncommon "github.com/answerdev/answer/internal/service/question_common"
|
|
|
|
"github.com/answerdev/answer/internal/service/unique"
|
2022-12-12 18:21:03 +08:00
|
|
|
"github.com/answerdev/answer/pkg/htmltext"
|
2023-03-03 15:47:56 +08:00
|
|
|
"github.com/answerdev/answer/pkg/uid"
|
2022-09-27 17:59:05 +08:00
|
|
|
|
|
|
|
"github.com/segmentfault/pacman/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// questionRepo question repository
|
|
|
|
type questionRepo struct {
|
|
|
|
data *data.Data
|
|
|
|
uniqueIDRepo unique.UniqueIDRepo
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewQuestionRepo new repository
|
|
|
|
func NewQuestionRepo(
|
|
|
|
data *data.Data,
|
|
|
|
uniqueIDRepo unique.UniqueIDRepo,
|
|
|
|
) questioncommon.QuestionRepo {
|
|
|
|
return &questionRepo{
|
|
|
|
data: data,
|
|
|
|
uniqueIDRepo: uniqueIDRepo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddQuestion add question
|
|
|
|
func (qr *questionRepo) AddQuestion(ctx context.Context, question *entity.Question) (err error) {
|
|
|
|
question.ID, err = qr.uniqueIDRepo.GenUniqueIDStr(ctx, question.TableName())
|
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Insert(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
question.ID = uid.EnShortID(question.ID)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveQuestion delete question
|
|
|
|
func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
id = uid.DeShortID(id)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", id).Delete(&entity.Question{})
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateQuestion update question
|
|
|
|
func (qr *questionRepo) UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols(Cols...).Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
question.ID = uid.EnShortID(question.ID)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
questionID = uid.DeShortID(questionID)
|
2022-09-27 17:59:05 +08:00
|
|
|
question := &entity.Question{}
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("view_count", 1).Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
questionID = uid.DeShortID(questionID)
|
2022-09-27 17:59:05 +08:00
|
|
|
question := &entity.Question{}
|
2023-06-06 15:43:32 +08:00
|
|
|
question.AnswerCount = num
|
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Cols("answer_count").Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-11-01 15:25:44 +08:00
|
|
|
func (qr *questionRepo) UpdateCollectionCount(ctx context.Context, questionID string, num int) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
questionID = uid.DeShortID(questionID)
|
2022-09-27 17:59:05 +08:00
|
|
|
question := &entity.Question{}
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", questionID).Incr("collection_count", num).Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *entity.Question) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2022-09-28 10:21:04 +08:00
|
|
|
now := time.Now()
|
|
|
|
question.UpdatedAt = now
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("status", "updated_at").Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-20 15:14:39 +08:00
|
|
|
func (qr *questionRepo) UpdateQuestionStatusWithOutUpdateTime(ctx context.Context, question *entity.Question) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("status").Update(question)
|
2023-02-20 15:14:39 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-13 17:34:02 +08:00
|
|
|
func (qr *questionRepo) UpdateQuestionOperation(ctx context.Context, question *entity.Question) (err error) {
|
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("pin", "show").Update(question)
|
2023-04-13 17:34:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
func (qr *questionRepo) UpdateAccepted(ctx context.Context, question *entity.Question) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("accepted_answer_id").Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qr *questionRepo) UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2023-05-25 11:17:10 +08:00
|
|
|
_, err = qr.data.DB.Context(ctx).Where("id =?", question.ID).Cols("last_answer_id").Update(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuestion get question one
|
|
|
|
func (qr *questionRepo) GetQuestion(ctx context.Context, id string) (
|
2022-11-01 15:25:44 +08:00
|
|
|
question *entity.Question, exist bool, err error,
|
|
|
|
) {
|
2023-03-03 15:47:56 +08:00
|
|
|
id = uid.DeShortID(id)
|
2022-09-27 17:59:05 +08:00
|
|
|
question = &entity.Question{}
|
|
|
|
question.ID = id
|
2023-05-25 11:17:10 +08:00
|
|
|
exist, err = qr.data.DB.Context(ctx).Where("id = ?", id).Get(question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
question.ID = uid.EnShortID(question.ID)
|
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2023-05-25 11:17:10 +08:00
|
|
|
err = qr.data.DB.Context(ctx).Table("question").Where("title like ?", "%"+title+"%").Limit(10, 0).Find(&questionList)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
for _, item := range questionList {
|
|
|
|
item.ID = uid.EnShortID(item.ID)
|
|
|
|
}
|
2023-03-03 15:47:56 +08:00
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qr *questionRepo) FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
for key, itemID := range id {
|
2023-03-09 14:59:07 +08:00
|
|
|
id[key] = uid.DeShortID(itemID)
|
2023-03-03 15:47:56 +08:00
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
questionList = make([]*entity.Question, 0)
|
2023-05-25 11:17:10 +08:00
|
|
|
err = qr.data.DB.Context(ctx).Table("question").In("id", id).Find(&questionList)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
for _, item := range questionList {
|
|
|
|
item.ID = uid.EnShortID(item.ID)
|
|
|
|
}
|
2023-03-03 15:47:56 +08:00
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetQuestionList get question list all
|
|
|
|
func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Question) (questionList []*entity.Question, err error) {
|
2023-03-03 15:47:56 +08:00
|
|
|
question.ID = uid.DeShortID(question.ID)
|
2022-09-27 17:59:05 +08:00
|
|
|
questionList = make([]*entity.Question, 0)
|
2023-05-25 11:17:10 +08:00
|
|
|
err = qr.data.DB.Context(ctx).Find(questionList, question)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
return questionList, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-11-02 15:50:32 +08:00
|
|
|
}
|
2023-03-03 15:47:56 +08:00
|
|
|
for _, item := range questionList {
|
|
|
|
item.ID = uid.DeShortID(item.ID)
|
|
|
|
}
|
2022-11-02 15:50:32 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (qr *questionRepo) GetQuestionCount(ctx context.Context) (count int64, err error) {
|
2023-06-28 10:52:36 +08:00
|
|
|
session := qr.data.DB.Context(ctx)
|
|
|
|
session.Where("status = ? OR status = ?", entity.QuestionStatusAvailable, entity.QuestionStatusClosed)
|
|
|
|
count, err = session.Count(&entity.Question{Show: entity.QuestionShow})
|
2022-11-02 15:50:32 +08:00
|
|
|
if err != nil {
|
2023-06-28 10:52:36 +08:00
|
|
|
return 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2023-06-28 10:52:36 +08:00
|
|
|
return count, nil
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2023-05-16 16:02:41 +08:00
|
|
|
func (qr *questionRepo) GetUserQuestionCount(ctx context.Context, userID string) (count int64, err error) {
|
|
|
|
questionList := make([]*entity.Question, 0)
|
2023-05-26 18:21:16 +08:00
|
|
|
count, err = qr.data.DB.In("question.status", []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}).And("question.user_id = ?", userID).FindAndCount(&questionList)
|
2023-05-16 16:02:41 +08:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-06-28 10:52:36 +08:00
|
|
|
func (qr *questionRepo) SitemapQuestions(ctx context.Context, page, pageSize int) (
|
|
|
|
questionIDList []*schema.SiteMapQuestionInfo, err error) {
|
2023-06-28 11:43:13 +08:00
|
|
|
page = page - 1
|
2022-12-12 18:21:03 +08:00
|
|
|
questionIDList = make([]*schema.SiteMapQuestionInfo, 0)
|
2023-06-28 10:52:36 +08:00
|
|
|
|
|
|
|
// try to get sitemap data from cache
|
|
|
|
cacheKey := fmt.Sprintf(constant.SiteMapQuestionCacheKeyPrefix, page)
|
|
|
|
cacheData, err := qr.data.Cache.GetString(ctx, cacheKey)
|
|
|
|
if err == nil && len(cacheKey) > 0 {
|
|
|
|
_ = json.Unmarshal([]byte(cacheData), &questionIDList)
|
|
|
|
return questionIDList, nil
|
2022-12-12 18:21:03 +08:00
|
|
|
}
|
2023-06-28 10:52:36 +08:00
|
|
|
|
|
|
|
// get sitemap data from db
|
|
|
|
rows := make([]*entity.Question, 0)
|
|
|
|
session := qr.data.DB.Context(ctx)
|
|
|
|
session.Select("id,title,created_at,post_update_time")
|
|
|
|
session.Where("`show` = ?", entity.QuestionShow)
|
|
|
|
session.Where("status = ? OR status = ?", entity.QuestionStatusAvailable, entity.QuestionStatusClosed)
|
|
|
|
session.Limit(pageSize, page*pageSize)
|
|
|
|
session.Asc("created_at")
|
|
|
|
err = session.Find(&rows)
|
2022-12-12 18:21:03 +08:00
|
|
|
if err != nil {
|
|
|
|
return questionIDList, err
|
|
|
|
}
|
2023-06-28 10:52:36 +08:00
|
|
|
|
|
|
|
// warp data
|
2022-12-12 18:21:03 +08:00
|
|
|
for _, question := range rows {
|
2023-06-28 11:43:13 +08:00
|
|
|
item := &schema.SiteMapQuestionInfo{ID: question.ID}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
item.ID = uid.EnShortID(question.ID)
|
|
|
|
}
|
2022-12-12 18:21:03 +08:00
|
|
|
item.Title = htmltext.UrlTitle(question.Title)
|
2023-06-28 10:52:36 +08:00
|
|
|
if question.PostUpdateTime.IsZero() {
|
|
|
|
item.UpdateTime = question.CreatedAt.Format(time.RFC3339)
|
|
|
|
} else {
|
|
|
|
item.UpdateTime = question.PostUpdateTime.Format(time.RFC3339)
|
2023-03-03 16:40:24 +08:00
|
|
|
}
|
2022-12-12 18:21:03 +08:00
|
|
|
questionIDList = append(questionIDList, item)
|
|
|
|
}
|
2023-06-28 10:52:36 +08:00
|
|
|
|
|
|
|
// set sitemap data to cache
|
|
|
|
cacheDataByte, _ := json.Marshal(questionIDList)
|
|
|
|
if err := qr.data.Cache.SetString(ctx, cacheKey, string(cacheDataByte), constant.SiteMapQuestionCacheTime); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
2022-12-12 18:21:03 +08:00
|
|
|
return questionIDList, nil
|
|
|
|
}
|
|
|
|
|
2022-12-30 17:09:32 +08:00
|
|
|
// GetQuestionPage query question page
|
2023-04-23 15:10:44 +08:00
|
|
|
func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int, userID, tagID, orderCond string, inDays int) (
|
2022-12-30 17:09:32 +08:00
|
|
|
questionList []*entity.Question, total int64, err error) {
|
2022-09-27 17:59:05 +08:00
|
|
|
questionList = make([]*entity.Question, 0)
|
|
|
|
|
2023-05-25 11:17:10 +08:00
|
|
|
session := qr.data.DB.Context(ctx).Where("question.status = ? OR question.status = ?",
|
2022-12-30 17:09:32 +08:00
|
|
|
entity.QuestionStatusAvailable, entity.QuestionStatusClosed)
|
|
|
|
if len(tagID) > 0 {
|
|
|
|
session.Join("LEFT", "tag_rel", "question.id = tag_rel.object_id")
|
|
|
|
session.And("tag_rel.tag_id = ?", tagID)
|
|
|
|
session.And("tag_rel.status = ?", entity.TagRelStatusAvailable)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-12-30 17:09:32 +08:00
|
|
|
if len(userID) > 0 {
|
|
|
|
session.And("question.user_id = ?", userID)
|
2023-04-13 15:46:36 +08:00
|
|
|
} else {
|
|
|
|
session.And("question.show = ?", entity.QuestionShow)
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2023-04-24 14:17:52 +08:00
|
|
|
if inDays > 0 {
|
|
|
|
session.And("question.created_at > ?", time.Now().AddDate(0, 0, -inDays))
|
|
|
|
}
|
2023-04-13 15:46:36 +08:00
|
|
|
|
2022-12-30 17:09:32 +08:00
|
|
|
switch orderCond {
|
2022-09-27 17:59:05 +08:00
|
|
|
case "newest":
|
2023-04-13 15:46:36 +08:00
|
|
|
session.OrderBy("question.pin desc,question.created_at DESC")
|
2022-09-27 17:59:05 +08:00
|
|
|
case "active":
|
2023-04-13 15:46:36 +08:00
|
|
|
session.OrderBy("question.pin desc,question.post_update_time DESC, question.updated_at DESC")
|
2022-09-27 17:59:05 +08:00
|
|
|
case "frequent":
|
2023-04-13 15:46:36 +08:00
|
|
|
session.OrderBy("question.pin desc,question.view_count DESC")
|
2022-09-27 17:59:05 +08:00
|
|
|
case "score":
|
2023-04-13 15:46:36 +08:00
|
|
|
session.OrderBy("question.pin desc,question.vote_count DESC, question.view_count DESC")
|
2022-09-27 17:59:05 +08:00
|
|
|
case "unanswered":
|
2022-12-30 17:09:32 +08:00
|
|
|
session.Where("question.last_answer_id = 0")
|
2023-04-13 15:46:36 +08:00
|
|
|
session.OrderBy("question.pin desc,question.created_at DESC")
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-12-30 17:09:32 +08:00
|
|
|
|
|
|
|
total, err = pager.Help(page, pageSize, &questionList, &entity.Question{}, session)
|
2022-09-27 17:59:05 +08:00
|
|
|
if err != nil {
|
|
|
|
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
for _, item := range questionList {
|
|
|
|
item.ID = uid.EnShortID(item.ID)
|
|
|
|
}
|
2023-03-03 15:47:56 +08:00
|
|
|
}
|
2022-12-30 17:09:32 +08:00
|
|
|
return questionList, total, err
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
|
|
|
|
2023-06-20 16:55:43 +08:00
|
|
|
func (qr *questionRepo) AdminQuestionPage(ctx context.Context, search *schema.AdminQuestionPageReq) ([]*entity.Question, int64, error) {
|
2022-11-01 09:12:12 +08:00
|
|
|
var (
|
|
|
|
count int64
|
|
|
|
err error
|
2023-05-25 11:17:10 +08:00
|
|
|
session = qr.data.DB.Context(ctx).Table("question")
|
2022-11-01 09:12:12 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
session.Where(builder.Eq{
|
|
|
|
"status": search.Status,
|
|
|
|
})
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
rows := make([]*entity.Question, 0)
|
|
|
|
if search.Page > 0 {
|
|
|
|
search.Page = search.Page - 1
|
|
|
|
} else {
|
|
|
|
search.Page = 0
|
|
|
|
}
|
|
|
|
if search.PageSize == 0 {
|
2022-11-01 15:25:44 +08:00
|
|
|
search.PageSize = constant.DefaultPageSize
|
2022-09-27 17:59:05 +08:00
|
|
|
}
|
2022-11-01 09:12:12 +08:00
|
|
|
|
|
|
|
// search by question title like or question id
|
|
|
|
if len(search.Query) > 0 {
|
|
|
|
// check id search
|
|
|
|
var (
|
|
|
|
idSearch = false
|
|
|
|
id = ""
|
|
|
|
)
|
2023-03-03 17:47:48 +08:00
|
|
|
|
2022-11-01 16:51:01 +08:00
|
|
|
if strings.Contains(search.Query, "question:") {
|
2022-11-01 09:12:12 +08:00
|
|
|
idSearch = true
|
2022-11-01 16:51:01 +08:00
|
|
|
id = strings.TrimSpace(strings.TrimPrefix(search.Query, "question:"))
|
2023-03-03 15:47:56 +08:00
|
|
|
id = uid.DeShortID(id)
|
2022-11-01 09:12:12 +08:00
|
|
|
for _, r := range id {
|
|
|
|
if !unicode.IsDigit(r) {
|
|
|
|
idSearch = false
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idSearch {
|
|
|
|
session.And(builder.Eq{
|
|
|
|
"id": id,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
session.And(builder.Like{
|
|
|
|
"title", search.Query,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-27 17:59:05 +08:00
|
|
|
offset := search.Page * search.PageSize
|
2022-11-01 09:12:12 +08:00
|
|
|
|
2023-05-26 11:09:55 +08:00
|
|
|
session.OrderBy("created_at desc").
|
2022-11-01 09:12:12 +08:00
|
|
|
Limit(search.PageSize, offset)
|
2022-09-27 17:59:05 +08:00
|
|
|
count, err = session.FindAndCount(&rows)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
|
|
|
return rows, count, err
|
|
|
|
}
|
2023-06-02 16:53:04 +08:00
|
|
|
if handler.GetEnableShortID(ctx) {
|
|
|
|
for _, item := range rows {
|
|
|
|
item.ID = uid.EnShortID(item.ID)
|
|
|
|
}
|
2023-03-03 17:47:48 +08:00
|
|
|
}
|
2022-09-27 17:59:05 +08:00
|
|
|
return rows, count, nil
|
|
|
|
}
|