From c3d8d308923d5c6fbf43aec58a4fdb15ca9dfe06 Mon Sep 17 00:00:00 2001 From: LinkinStars Date: Wed, 1 Mar 2023 17:28:16 +0800 Subject: [PATCH 1/2] fix(answer): Only the admin and author can view the deleted answer. --- internal/entity/answer_entity.go | 8 +++++--- internal/repo/answer/answer_repo.go | 4 +++- internal/schema/answer_schema.go | 1 + internal/service/answer_common/answer.go | 1 + internal/service/answer_service.go | 1 + 5 files changed, 11 insertions(+), 4 deletions(-) diff --git a/internal/entity/answer_entity.go b/internal/entity/answer_entity.go index 8c3bcdb1..43567efd 100644 --- a/internal/entity/answer_entity.go +++ b/internal/entity/answer_entity.go @@ -35,9 +35,11 @@ type Answer struct { type AnswerSearch struct { Answer - Order string `json:"order_by" ` // default or updated - Page int `json:"page" form:"page"` // Query number of pages - PageSize int `json:"page_size" form:"page_size"` // Search page size + IncludeDeleted bool `json:"include_deleted"` + LoginUserID string `json:"login_user_id"` + Order string `json:"order_by"` // default or updated + Page int `json:"page" form:"page"` // Query number of pages + PageSize int `json:"page_size" form:"page_size"` // Search page size } type AdminAnswerSearch struct { diff --git a/internal/repo/answer/answer_repo.go b/internal/repo/answer/answer_repo.go index 97734343..046d22dc 100644 --- a/internal/repo/answer/answer_repo.go +++ b/internal/repo/answer/answer_repo.go @@ -206,7 +206,9 @@ func (ar *answerRepo) SearchList(ctx context.Context, search *entity.AnswerSearc default: session = session.OrderBy("adopted desc,vote_count desc,created_at asc") } - session = session.And("status = ?", entity.AnswerStatusAvailable) + if !search.IncludeDeleted { + session = session.And("status = ? OR user_id = ?", entity.AnswerStatusAvailable, search.UserID) + } session = session.Limit(search.PageSize, offset) count, err = session.FindAndCount(&rows) diff --git a/internal/schema/answer_schema.go b/internal/schema/answer_schema.go index 7fc6a12d..3a67f2bf 100644 --- a/internal/schema/answer_schema.go +++ b/internal/schema/answer_schema.go @@ -83,6 +83,7 @@ type AnswerInfo struct { VoteStatus string `json:"vote_status"` VoteCount int `json:"vote_count"` QuestionInfo *QuestionInfo `json:"question_info,omitempty"` + Status int `json:"status"` // MemberActions MemberActions []*PermissionMemberAction `json:"member_actions"` diff --git a/internal/service/answer_common/answer.go b/internal/service/answer_common/answer.go index 758908e1..357484f2 100644 --- a/internal/service/answer_common/answer.go +++ b/internal/service/answer_common/answer.go @@ -73,6 +73,7 @@ func (as *AnswerCommon) ShowFormat(ctx context.Context, data *entity.Answer) *sc } info.UserID = data.UserID info.UpdateUserID = data.LastEditUserID + info.Status = data.Status return &info } diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go index 974482c9..d4d49076 100644 --- a/internal/service/answer_service.go +++ b/internal/service/answer_service.go @@ -473,6 +473,7 @@ func (as *AnswerService) SearchList(ctx context.Context, req *schema.AnswerListR dbSearch.Page = req.Page dbSearch.PageSize = req.PageSize dbSearch.Order = req.Order + dbSearch.IncludeDeleted = req.CanDelete answerOriginalList, count, err := as.answerRepo.SearchList(ctx, &dbSearch) if err != nil { return list, count, err From ba7891b0979ca0a4826bce13d657f4ebf79c872e Mon Sep 17 00:00:00 2001 From: aichy126 <16996097+aichy126@users.noreply.github.com> Date: Wed, 1 Mar 2023 17:49:55 +0800 Subject: [PATCH 2/2] update question answer del --- docs/docs.go | 27 +++++++++++++++++++-------- docs/swagger.json | 24 ++++++++++++++++++------ docs/swagger.yaml | 13 ++++++++----- internal/service/answer_service.go | 5 +++++ internal/service/question_service.go | 4 ++++ 5 files changed, 54 insertions(+), 19 deletions(-) diff --git a/docs/docs.go b/docs/docs.go index 1dedea4d..fc2a989a 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -1,5 +1,4 @@ -// Package docs GENERATED BY SWAG; DO NOT EDIT -// This file was generated by swaggo/swag +// Code generated by swaggo/swag. DO NOT EDIT package docs import "github.com/swaggo/swag" @@ -5984,9 +5983,6 @@ const docTemplate = `{ "schema.GetOtherUserInfoResp": { "type": "object", "properties": { - "has": { - "type": "boolean" - }, "info": { "$ref": "#/definitions/schema.GetOtherUserInfoByUsernameResp" } @@ -6986,7 +6982,11 @@ const docTemplate = `{ }, "user_info": { "description": "user info", - "$ref": "#/definitions/schema.UserBasicInfo" + "allOf": [ + { + "$ref": "#/definitions/schema.UserBasicInfo" + } + ] }, "vote_count": { "type": "integer" @@ -6998,7 +6998,11 @@ const docTemplate = `{ "properties": { "object": { "description": "this object", - "$ref": "#/definitions/schema.SearchObject" + "allOf": [ + { + "$ref": "#/definitions/schema.SearchObject" + } + ] }, "object_type": { "description": "object_type", @@ -7173,6 +7177,9 @@ const docTemplate = `{ }, "theme": { "$ref": "#/definitions/schema.SiteThemeResp" + }, + "version": { + "type": "string" } } }, @@ -7508,7 +7515,11 @@ const docTemplate = `{ "properties": { "avatar": { "description": "avatar", - "$ref": "#/definitions/schema.AvatarInfo" + "allOf": [ + { + "$ref": "#/definitions/schema.AvatarInfo" + } + ] }, "bio": { "description": "bio", diff --git a/docs/swagger.json b/docs/swagger.json index ca8d737d..3452fa23 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -5972,9 +5972,6 @@ "schema.GetOtherUserInfoResp": { "type": "object", "properties": { - "has": { - "type": "boolean" - }, "info": { "$ref": "#/definitions/schema.GetOtherUserInfoByUsernameResp" } @@ -6974,7 +6971,11 @@ }, "user_info": { "description": "user info", - "$ref": "#/definitions/schema.UserBasicInfo" + "allOf": [ + { + "$ref": "#/definitions/schema.UserBasicInfo" + } + ] }, "vote_count": { "type": "integer" @@ -6986,7 +6987,11 @@ "properties": { "object": { "description": "this object", - "$ref": "#/definitions/schema.SearchObject" + "allOf": [ + { + "$ref": "#/definitions/schema.SearchObject" + } + ] }, "object_type": { "description": "object_type", @@ -7161,6 +7166,9 @@ }, "theme": { "$ref": "#/definitions/schema.SiteThemeResp" + }, + "version": { + "type": "string" } } }, @@ -7496,7 +7504,11 @@ "properties": { "avatar": { "description": "avatar", - "$ref": "#/definitions/schema.AvatarInfo" + "allOf": [ + { + "$ref": "#/definitions/schema.AvatarInfo" + } + ] }, "bio": { "description": "bio", diff --git a/docs/swagger.yaml b/docs/swagger.yaml index da69a617..77f6b495 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -488,8 +488,6 @@ definitions: type: object schema.GetOtherUserInfoResp: properties: - has: - type: boolean info: $ref: '#/definitions/schema.GetOtherUserInfoByUsernameResp' type: object @@ -1201,7 +1199,8 @@ definitions: title: type: string user_info: - $ref: '#/definitions/schema.UserBasicInfo' + allOf: + - $ref: '#/definitions/schema.UserBasicInfo' description: user info vote_count: type: integer @@ -1209,7 +1208,8 @@ definitions: schema.SearchResp: properties: object: - $ref: '#/definitions/schema.SearchObject' + allOf: + - $ref: '#/definitions/schema.SearchObject' description: this object object_type: description: object_type @@ -1335,6 +1335,8 @@ definitions: $ref: '#/definitions/schema.SiteSeoReq' theme: $ref: '#/definitions/schema.SiteThemeResp' + version: + type: string type: object schema.SiteInterfaceReq: properties: @@ -1561,7 +1563,8 @@ definitions: schema.UpdateInfoRequest: properties: avatar: - $ref: '#/definitions/schema.AvatarInfo' + allOf: + - $ref: '#/definitions/schema.AvatarInfo' description: avatar bio: description: bio diff --git a/internal/service/answer_service.go b/internal/service/answer_service.go index d4d49076..1b60ce87 100644 --- a/internal/service/answer_service.go +++ b/internal/service/answer_service.go @@ -232,6 +232,11 @@ func (as *AnswerService) Update(ctx context.Context, req *schema.AnswerUpdateReq return "", nil } + if answerInfo.Status == entity.AnswerStatusDeleted { + err = errors.BadRequest(reason.AnswerCannotUpdate) + return "", err + } + //If the content is the same, ignore it if answerInfo.OriginalText == req.Content { return "", nil diff --git a/internal/service/question_service.go b/internal/service/question_service.go index ccf49454..659a4d55 100644 --- a/internal/service/question_service.go +++ b/internal/service/question_service.go @@ -470,6 +470,10 @@ func (qs *QuestionService) UpdateQuestion(ctx context.Context, req *schema.Quest if !has { return } + if dbinfo.Status == entity.QuestionStatusDeleted { + err = errors.BadRequest(reason.QuestionCannotUpdate) + return + } now := time.Now() question := &entity.Question{}