Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

feat(question): show pending question for user

LinkinStarsce9a34b8d5d3
diff --git a/internal/controller/question_controller.go b/internal/controller/question_controller.go
index db35c83f..072ffd74 100644
--- a/internal/controller/question_controller.go
+++ b/internal/controller/question_controller.go
@@ -817,6 +817,7 @@ func (qc *QuestionController) PersonalQuestionPage(ctx *gin.Context) {
}
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
+ req.IsAdmin = middleware.GetUserIsAdminModerator(ctx)
resp, err := qc.questionService.PersonalQuestionPage(ctx, req)
handler.HandleResponse(ctx, err, resp)
}
@@ -841,6 +842,7 @@ func (qc *QuestionController) PersonalAnswerPage(ctx *gin.Context) {
}
req.LoginUserID = middleware.GetLoginUserIDFromContext(ctx)
+ req.IsAdmin = middleware.GetUserIsAdminModerator(ctx)
resp, err := qc.questionService.PersonalAnswerPage(ctx, req)
handler.HandleResponse(ctx, err, resp)
}
diff --git a/internal/entity/answer_entity.go b/internal/entity/answer_entity.go
index ee5bbc2c..0d21f15b 100644
--- a/internal/entity/answer_entity.go
+++ b/internal/entity/answer_entity.go
@@ -63,6 +63,14 @@ type AnswerSearch struct {
PageSize int `json:"page_size" form:"page_size"` // Search page size
}
+type PersonalAnswerPageQueryCond struct {
+ Page int
+ PageSize int
+ UserID string
+ Order string
+ ShowPending bool
+}
+
// TableName answer table name
func (Answer) TableName() string {
return "answer"
diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go
index aca7517c..029a1970 100644
--- a/internal/repo/answer/answer_repo.go
+++ b/internal/repo/answer/answer_repo.go
@@ -363,6 +363,42 @@ func (ar *answerRepo) SearchList(ctx context.Context, search *entity.AnswerSearc
return rows, count, nil
}
+// GetPersonalAnswerPage personal answer page
+func (ar *answerRepo) GetPersonalAnswerPage(ctx context.Context, req *entity.PersonalAnswerPageQueryCond) (
+ resp []*entity.Answer, total int64, err error) {
+ cond := &entity.Answer{
+ UserID: req.UserID,
+ }
+ session := ar.data.DB.Context(ctx)
+ switch req.Order {
+ case entity.AnswerSearchOrderByTime:
+ session = session.OrderBy("created_at desc")
+ case entity.AnswerSearchOrderByTimeAsc:
+ session = session.OrderBy("created_at asc")
+ case entity.AnswerSearchOrderByVote:
+ session = session.OrderBy("vote_count desc")
+ default:
+ session = session.OrderBy("adopted desc,vote_count desc,created_at asc")
+ }
+ if req.ShowPending {
+ session = session.And("status != ?", entity.AnswerStatusDeleted)
+ } else {
+ session = session.And("status = ?", entity.AnswerStatusAvailable)
+ }
+ resp = make([]*entity.Answer, 0)
+ total, err = pager.Help(req.Page, req.PageSize, &resp, cond, session)
+ if err != nil {
+ return nil, 0, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ if handler.GetEnableShortID(ctx) {
+ for _, item := range resp {
+ item.ID = uid.EnShortID(item.ID)
+ item.QuestionID = uid.EnShortID(item.QuestionID)
+ }
+ }
+ return resp, total, nil
+}
+
func (ar *answerRepo) AdminSearchList(ctx context.Context, req *schema.AdminAnswerPageReq) (
resp []*entity.Answer, total int64, err error) {
cond := &entity.Answer{}
diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go
index ee30929f..920c0b08 100644
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@ -344,12 +344,16 @@ func (qr *questionRepo) SitemapQuestions(ctx context.Context, page, pageSize int
}
// GetQuestionPage query question page
-func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int, tagIDs []string, userID, orderCond string, inDays int, showHidden bool) (
+func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int,
+ tagIDs []string, userID, orderCond string, inDays int, showHidden, showPending bool) (
questionList []*entity.Question, total int64, err error) {
questionList = make([]*entity.Question, 0)
-
- session := qr.data.DB.Context(ctx).Where("question.status = ? OR question.status = ?",
- entity.QuestionStatusAvailable, entity.QuestionStatusClosed)
+ session := qr.data.DB.Context(ctx)
+ status := []int{entity.QuestionStatusAvailable, entity.QuestionStatusClosed}
+ if showPending {
+ status = append(status, entity.QuestionStatusPending)
+ }
+ session.In("question.status", status)
if len(tagIDs) > 0 {
session.Join("LEFT", "tag_rel", "question.id = tag_rel.object_id")
session.In("tag_rel.tag_id", tagIDs)
diff --git a/internal/schema/question_schema.go b/internal/schema/question_schema.go
index 4df89cf6..48fd97c5 100644
--- a/internal/schema/question_schema.go
+++ b/internal/schema/question_schema.go
@@ -356,6 +356,7 @@ type QuestionPageReq struct {
LoginUserID string `json:"-"`
UserIDBeSearched string `json:"-"`
TagID string `json:"-"`
+ ShowPending bool `json:"-"`
}
const (
@@ -473,6 +474,7 @@ type PersonalQuestionPageReq struct {
OrderCond string `validate:"omitempty,oneof=newest active frequent score unanswered" form:"order"`
Username string `validate:"omitempty,gt=0,lte=100" form:"username"`
LoginUserID string `json:"-"`
+ IsAdmin bool `json:"-"`
}
type PersonalAnswerPageReq struct {
@@ -481,6 +483,7 @@ type PersonalAnswerPageReq struct {
OrderCond string `validate:"omitempty,oneof=newest active frequent score unanswered" form:"order"`
Username string `validate:"omitempty,gt=0,lte=100" form:"username"`
LoginUserID string `json:"-"`
+ IsAdmin bool `json:"-"`
}
type PersonalCollectionPageReq struct {
diff --git a/internal/service/answer_common/answer.go b/internal/service/answer_common/answer.go
index 4d8959fe..af2be2f0 100644
--- a/internal/service/answer_common/answer.go
+++ b/internal/service/answer_common/answer.go
@@ -43,6 +43,8 @@ type AnswerRepo interface {
GetCountByUserID(ctx context.Context, userID string) (int64, error)
GetIDsByUserIDAndQuestionID(ctx context.Context, userID string, questionID string) ([]string, error)
SearchList(ctx context.Context, search *entity.AnswerSearch) ([]*entity.Answer, int64, error)
+ GetPersonalAnswerPage(ctx context.Context, cond *entity.PersonalAnswerPageQueryCond) (
+ resp []*entity.Answer, total int64, err error)
AdminSearchList(ctx context.Context, search *schema.AdminAnswerPageReq) ([]*entity.Answer, int64, error)
UpdateAnswerStatus(ctx context.Context, answerID string, status int) (err error)
GetAnswerCount(ctx context.Context) (count int64, err error)
@@ -88,6 +90,11 @@ func (as *AnswerCommon) Search(ctx context.Context, search *entity.AnswerSearch)
return list, count, err
}
+func (as *AnswerCommon) PersonalAnswerPage(ctx context.Context,
+ cond *entity.PersonalAnswerPageQueryCond) ([]*entity.Answer, int64, error) {
+ return as.answerRepo.GetPersonalAnswerPage(ctx, cond)
+}
+
func (as *AnswerCommon) ShowFormat(ctx context.Context, data *entity.Answer) *schema.AnswerInfo {
info := schema.AnswerInfo{}
info.ID = data.ID
diff --git a/internal/service/content/question_service.go b/internal/service/content/question_service.go
index db777eab..c5a172e0 100644
--- a/internal/service/content/question_service.go
+++ b/internal/service/content/question_service.go
@@ -934,8 +934,9 @@ func (qs *QuestionService) GetQuestion(ctx context.Context, questionID, userID s
if err != nil {
return
}
- // If the question is deleted, only the administrator and the author can view it
- if question.Status == entity.QuestionStatusDeleted && !per.CanReopen && question.UserID != userID {
+ // If the question is deleted or pending, only the administrator and the author can view it
+ if (question.Status == entity.QuestionStatusDeleted ||
+ question.Status == entity.QuestionStatusPending) && !per.CanReopen && question.UserID != userID {
return nil, errors.NotFound(reason.QuestionNotFound)
}
if question.Status != entity.QuestionStatusClosed {
@@ -1021,6 +1022,10 @@ func (qs *QuestionService) PersonalQuestionPage(ctx context.Context, req *schema
search.PageSize = req.PageSize
search.UserIDBeSearched = userinfo.ID
search.LoginUserID = req.LoginUserID
+ // Only author and administrator can view the pending question
+ if req.LoginUserID == userinfo.ID || req.IsAdmin {
+ search.ShowPending = true
+ }
questionList, total, err := qs.GetQuestionPage(ctx, search)
if err != nil {
return nil, err
@@ -1047,17 +1052,18 @@ func (qs *QuestionService) PersonalAnswerPage(ctx context.Context, req *schema.P
if !exist {
return nil, errors.BadRequest(reason.UserNotFound)
}
- answersearch := &entity.AnswerSearch{}
- answersearch.UserID = userinfo.ID
- answersearch.PageSize = req.PageSize
- answersearch.Page = req.Page
+ cond := &entity.PersonalAnswerPageQueryCond{}
+ cond.UserID = userinfo.ID
+ cond.Page = req.Page
+ cond.PageSize = req.PageSize
+ cond.ShowPending = req.IsAdmin || req.LoginUserID == cond.UserID
if req.OrderCond == "newest" {
- answersearch.Order = entity.AnswerSearchOrderByTime
+ cond.Order = entity.AnswerSearchOrderByTime
} else {
- answersearch.Order = entity.AnswerSearchOrderByDefault
+ cond.Order = entity.AnswerSearchOrderByDefault
}
questionIDs := make([]string, 0)
- answerList, total, err := qs.questioncommon.AnswerCommon.Search(ctx, answersearch)
+ answerList, total, err := qs.questioncommon.AnswerCommon.PersonalAnswerPage(ctx, cond)
if err != nil {
return nil, err
}
@@ -1301,7 +1307,7 @@ func (qs *QuestionService) GetQuestionPage(ctx context.Context, req *schema.Ques
}
questionList, total, err := qs.questionRepo.GetQuestionPage(ctx, req.Page, req.PageSize,
- tagIDs, req.UserIDBeSearched, req.OrderCond, req.InDays, showHidden)
+ tagIDs, req.UserIDBeSearched, req.OrderCond, req.InDays, showHidden, req.ShowPending)
if err != nil {
return nil, 0, err
}
diff --git a/internal/service/question_common/question.go b/internal/service/question_common/question.go
index 29b2a0c6..9f845c95 100644
--- a/internal/service/question_common/question.go
+++ b/internal/service/question_common/question.go
@@ -55,7 +55,7 @@ type QuestionRepo interface {
UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error)
GetQuestion(ctx context.Context, id string) (question *entity.Question, exist bool, err error)
GetQuestionList(ctx context.Context, question *entity.Question) (questions []*entity.Question, err error)
- GetQuestionPage(ctx context.Context, page, pageSize int, tagIDs []string, userID, orderCond string, inDays int, showHidden bool) (
+ GetQuestionPage(ctx context.Context, page, pageSize int, tagIDs []string, userID, orderCond string, inDays int, showHidden, showPending bool) (
questionList []*entity.Question, total int64, err error)
UpdateQuestionStatus(ctx context.Context, questionID string, status int) (err error)
UpdateQuestionStatusWithOutUpdateTime(ctx context.Context, question *entity.Question) (err error)