CheckCanUpdateQuestion

This commit is contained in:
aichy126 2022-11-23 15:19:17 +08:00
parent 1bf0d6365e
commit a680944168
10 changed files with 173 additions and 0 deletions

View File

@ -2575,6 +2575,44 @@ const docTemplate = `{
}
}
},
"/answer/api/v1/question/edit/check": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "check can update question",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"api-question"
],
"summary": "check can update question",
"parameters": [
{
"type": "string",
"default": "string",
"description": "id",
"name": "id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/answer/api/v1/question/info": {
"get": {
"security": [

View File

@ -2563,6 +2563,44 @@
}
}
},
"/answer/api/v1/question/edit/check": {
"get": {
"security": [
{
"ApiKeyAuth": []
}
],
"description": "check can update question",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"api-question"
],
"summary": "check can update question",
"parameters": [
{
"type": "string",
"default": "string",
"description": "id",
"name": "id",
"in": "query",
"required": true
}
],
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/handler.RespBody"
}
}
}
}
},
"/answer/api/v1/question/info": {
"get": {
"security": [

View File

@ -3185,6 +3185,30 @@ paths:
summary: close question msg list
tags:
- api-question
/answer/api/v1/question/edit/check:
get:
consumes:
- application/json
description: check can update question
parameters:
- default: string
description: id
in: query
name: id
required: true
type: string
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/handler.RespBody'
security:
- ApiKeyAuth: []
summary: check can update question
tags:
- api-question
/answer/api/v1/question/info:
get:
consumes:

View File

@ -229,6 +229,34 @@ func (qc *QuestionController) UpdateQuestion(ctx *gin.Context) {
handler.HandleResponse(ctx, err, resp)
}
// CheckCanUpdateQuestion check can update question
// @Summary check can update question
// @Description check can update question
// @Tags api-question
// @Accept json
// @Produce json
// @Security ApiKeyAuth
// @Param id query string true "id" default(string)
// @Success 200 {object} handler.RespBody
// @Router /answer/api/v1/question/edit/check [get]
func (qc *QuestionController) CheckCanUpdateQuestion(ctx *gin.Context) {
id := ctx.Query("id")
req := &schema.CheckCanQuestionUpdate{}
req.ID = id
req.UserID = middleware.GetLoginUserIDFromContext(ctx)
userinfo := middleware.GetUserInfoFromContext(ctx)
req.IsAdmin = userinfo.IsAdmin
if can, err := qc.rankService.CheckRankPermission(ctx, req.UserID, rank.QuestionEditRank); err != nil || !can {
handler.HandleResponse(ctx, err, errors.Forbidden(reason.RankFailToMeetTheCondition))
return
}
resp, err := qc.questionService.CheckCanUpdateQuestion(ctx, req)
handler.HandleResponse(ctx, err, gin.H{
"unreviewed": resp,
})
}
// CloseMsgList close question msg list
// @Summary close question msg list
// @Description close question msg list

View File

@ -102,6 +102,16 @@ func (rr *revisionRepo) GetRevisionByID(ctx context.Context, revisionID string)
return
}
func (rr *revisionRepo) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (
revision *entity.Revision, exist bool, err error) {
revision = &entity.Revision{}
exist, err = rr.data.DB.Where("object_id = ?", objectID).And("status = ?", entity.RevisionUnreviewedStatus).Get(revision)
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
return
}
// GetLastRevisionByObjectID get object's last revision by object TagID
func (rr *revisionRepo) GetLastRevisionByObjectID(ctx context.Context, objectID string) (
revision *entity.Revision, exist bool, err error,

View File

@ -172,6 +172,7 @@ func (a *AnswerAPIRouter) RegisterAnswerAPIRouter(r *gin.RouterGroup) {
// question
r.POST("/question", a.questionController.AddQuestion)
r.PUT("/question", a.questionController.UpdateQuestion)
r.GET("/question/edit/check", a.questionController.CheckCanUpdateQuestion)
r.DELETE("/question", a.questionController.RemoveQuestion)
r.PUT("/question/status", a.questionController.CloseQuestion)
r.GET("/question/similar", a.questionController.SearchByTitleLike)

View File

@ -34,6 +34,14 @@ type QuestionAdd struct {
UserID string `json:"-"`
}
type CheckCanQuestionUpdate struct {
// question id
ID string `validate:"required" json:"id"`
// user id
UserID string `json:"-"`
IsAdmin bool `json:"-"`
}
type QuestionUpdate struct {
// question id
ID string `validate:"required" json:"id"`

View File

@ -262,9 +262,28 @@ func (qs *QuestionService) RemoveQuestion(ctx context.Context, req *schema.Remov
return nil
}
func (qs *QuestionService) CheckCanUpdateQuestion(ctx context.Context, req *schema.CheckCanQuestionUpdate) (exist bool, err error) {
_, existUnreviewed, err := qs.revisionService.ExistUnreviewedByObjectID(ctx, req.ID)
if err != nil {
return false, err
}
return existUnreviewed, nil
}
// UpdateQuestion update question
func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.QuestionUpdate) (questionInfo any, err error) {
questionInfo = &schema.QuestionInfo{}
_, existUnreviewed, err := qs.revisionService.ExistUnreviewedByObjectID(ctx, req.ID)
if err != nil {
return
}
if existUnreviewed {
err = errors.BadRequest(reason.QuestionCannotUpdate)
return
}
now := time.Now()
question := &entity.Question{}
question.UserID = req.UserID

View File

@ -14,4 +14,5 @@ type RevisionRepo interface {
GetLastRevisionByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error)
GetRevisionList(ctx context.Context, revision *entity.Revision) (revisionList []entity.Revision, err error)
UpdateObjectRevisionId(ctx context.Context, revision *entity.Revision, session *xorm.Session) (err error)
ExistUnreviewedByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error)
}

View File

@ -54,3 +54,9 @@ func (rs *RevisionService) GetRevision(ctx context.Context, revisionID string) (
}
return revisionInfo, nil
}
// ExistUnreviewedByObjectID
func (rs *RevisionService) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error) {
revision, exist, err = rs.revisionRepo.ExistUnreviewedByObjectID(ctx, objectID)
return revision, exist, err
}