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.

update short id

aichy126789cf6bd8b0a
diff --git a/cmd/answer/wire_gen.go b/cmd/answer/wire_gen.go
index 515efa46..a20f694d 100644
--- a/cmd/answer/wire_gen.go
+++ b/cmd/answer/wire_gen.go
@@ -128,7 +128,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
answerRepo := answer.NewAnswerRepo(dataData, uniqueIDRepo, userRankRepo, activityRepo)
questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo)
tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo)
- tagRelRepo := tag.NewTagRelRepo(dataData)
+ tagRelRepo := tag.NewTagRelRepo(dataData, uniqueIDRepo)
tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo)
revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo)
revisionService := revision_common.NewRevisionService(revisionRepo, userRepo)
diff --git a/docs/docs.go b/docs/docs.go
index d6741817..f458768e 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"
@@ -6986,7 +6985,11 @@ const docTemplate = `{
},
"user_info": {
"description": "user info",
- "$ref": "#/definitions/schema.UserBasicInfo"
+ "allOf": [
+ {
+ "$ref": "#/definitions/schema.UserBasicInfo"
+ }
+ ]
},
"vote_count": {
"type": "integer"
@@ -6998,7 +7001,11 @@ const docTemplate = `{
"properties": {
"object": {
"description": "this object",
- "$ref": "#/definitions/schema.SearchObject"
+ "allOf": [
+ {
+ "$ref": "#/definitions/schema.SearchObject"
+ }
+ ]
},
"object_type": {
"description": "object_type",
@@ -7511,7 +7518,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 8433f7c5..b90efa55 100644
--- a/docs/swagger.json
+++ b/docs/swagger.json
@@ -6974,7 +6974,11 @@
},
"user_info": {
"description": "user info",
- "$ref": "#/definitions/schema.UserBasicInfo"
+ "allOf": [
+ {
+ "$ref": "#/definitions/schema.UserBasicInfo"
+ }
+ ]
},
"vote_count": {
"type": "integer"
@@ -6986,7 +6990,11 @@
"properties": {
"object": {
"description": "this object",
- "$ref": "#/definitions/schema.SearchObject"
+ "allOf": [
+ {
+ "$ref": "#/definitions/schema.SearchObject"
+ }
+ ]
},
"object_type": {
"description": "object_type",
@@ -7499,7 +7507,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 f3663b28..b267b711 100644
--- a/docs/swagger.yaml
+++ b/docs/swagger.yaml
@@ -1201,7 +1201,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 +1210,8 @@ definitions:
schema.SearchResp:
properties:
object:
- $ref: '#/definitions/schema.SearchObject'
+ allOf:
+ - $ref: '#/definitions/schema.SearchObject'
description: this object
object_type:
description: object_type
@@ -1563,7 +1565,8 @@ definitions:
schema.UpdateInfoRequest:
properties:
avatar:
- $ref: '#/definitions/schema.AvatarInfo'
+ allOf:
+ - $ref: '#/definitions/schema.AvatarInfo'
description: avatar
bio:
description: bio
diff --git a/internal/repo/question/question_repo.go b/internal/repo/question/question_repo.go
index 84d89987..f634a6b4 100644
--- a/internal/repo/question/question_repo.go
+++ b/internal/repo/question/question_repo.go
@@ -18,6 +18,7 @@ import (
questioncommon "github.com/answerdev/answer/internal/service/question_common"
"github.com/answerdev/answer/internal/service/unique"
"github.com/answerdev/answer/pkg/htmltext"
+ "github.com/answerdev/answer/pkg/uid"
"github.com/segmentfault/pacman/errors"
)
@@ -49,11 +50,13 @@ func (qr *questionRepo) AddQuestion(ctx context.Context, question *entity.Questi
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ question.ID = uid.EnShortID(question.ID)
return
}
// RemoveQuestion delete question
func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err error) {
+ id = uid.DeShortID(id)
_, err = qr.data.DB.Where("id =?", id).Delete(&entity.Question{})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -63,14 +66,17 @@ func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err erro
// UpdateQuestion update question
func (qr *questionRepo) UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error) {
+ question.ID = uid.DeShortID(question.ID)
_, err = qr.data.DB.Where("id =?", question.ID).Cols(Cols...).Update(question)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ question.ID = uid.EnShortID(question.ID)
return
}
func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (err error) {
+ questionID = uid.DeShortID(questionID)
question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("view_count", 1).Update(question)
if err != nil {
@@ -80,6 +86,7 @@ func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (e
}
func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error) {
+ questionID = uid.DeShortID(questionID)
question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("answer_count", num).Update(question)
if err != nil {
@@ -89,6 +96,7 @@ func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string
}
func (qr *questionRepo) UpdateCollectionCount(ctx context.Context, questionID string, num int) (err error) {
+ questionID = uid.DeShortID(questionID)
question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("collection_count", num).Update(question)
if err != nil {
@@ -98,6 +106,7 @@ func (qr *questionRepo) UpdateCollectionCount(ctx context.Context, questionID st
}
func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *entity.Question) (err error) {
+ question.ID = uid.DeShortID(question.ID)
now := time.Now()
question.UpdatedAt = now
_, err = qr.data.DB.Where("id =?", question.ID).Cols("status", "updated_at").Update(question)
@@ -108,6 +117,7 @@ func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *enti
}
func (qr *questionRepo) UpdateQuestionStatusWithOutUpdateTime(ctx context.Context, question *entity.Question) (err error) {
+ question.ID = uid.DeShortID(question.ID)
_, err = qr.data.DB.Where("id =?", question.ID).Cols("status").Update(question)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -116,6 +126,7 @@ func (qr *questionRepo) UpdateQuestionStatusWithOutUpdateTime(ctx context.Contex
}
func (qr *questionRepo) UpdateAccepted(ctx context.Context, question *entity.Question) (err error) {
+ question.ID = uid.DeShortID(question.ID)
_, err = qr.data.DB.Where("id =?", question.ID).Cols("accepted_answer_id").Update(question)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -124,6 +135,7 @@ func (qr *questionRepo) UpdateAccepted(ctx context.Context, question *entity.Que
}
func (qr *questionRepo) UpdateLastAnswer(ctx context.Context, question *entity.Question) (err error) {
+ question.ID = uid.DeShortID(question.ID)
_, err = qr.data.DB.Where("id =?", question.ID).Cols("last_answer_id").Update(question)
if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -135,12 +147,14 @@ func (qr *questionRepo) UpdateLastAnswer(ctx context.Context, question *entity.Q
func (qr *questionRepo) GetQuestion(ctx context.Context, id string) (
question *entity.Question, exist bool, err error,
) {
+ id = uid.DeShortID(id)
question = &entity.Question{}
question.ID = id
exist, err = qr.data.DB.Where("id = ?", id).Get(question)
if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ question.ID = uid.EnShortID(question.ID)
return
}
@@ -151,25 +165,38 @@ func (qr *questionRepo) SearchByTitleLike(ctx context.Context, title string) (qu
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range questionList {
+ item.ID = uid.EnShortID(item.ID)
+ }
return
}
func (qr *questionRepo) FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error) {
+ for key, itemID := range id {
+ id[key] = uid.EnShortID(itemID)
+ }
questionList = make([]*entity.Question, 0)
err = qr.data.DB.Table("question").In("id", id).Find(&questionList)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range questionList {
+ item.ID = uid.DeShortID(item.ID)
+ }
return
}
// GetQuestionList get question list all
func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Question) (questionList []*entity.Question, err error) {
+ question.ID = uid.DeShortID(question.ID)
questionList = make([]*entity.Question, 0)
err = qr.data.DB.Find(questionList, question)
if err != nil {
return questionList, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range questionList {
+ item.ID = uid.DeShortID(item.ID)
+ }
return
}
@@ -205,7 +232,7 @@ func (qr *questionRepo) GetQuestionIDsPage(ctx context.Context, page, pageSize i
}
for _, question := range rows {
item := &schema.SiteMapQuestionInfo{}
- item.ID = question.ID
+ item.ID = uid.EnShortID(question.ID)
item.Title = htmltext.UrlTitle(question.Title)
item.UpdateTime = fmt.Sprintf("%v", question.PostUpdateTime.Format(time.RFC3339))
questionIDList = append(questionIDList, item)
@@ -246,6 +273,9 @@ func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int,
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range questionList {
+ item.ID = uid.EnShortID(item.ID)
+ }
return questionList, total, err
}
@@ -280,6 +310,7 @@ func (qr *questionRepo) AdminSearchList(ctx context.Context, search *schema.Admi
if strings.Contains(search.Query, "question:") {
idSearch = true
id = strings.TrimSpace(strings.TrimPrefix(search.Query, "question:"))
+ id = uid.DeShortID(id)
for _, r := range id {
if !unicode.IsDigit(r) {
idSearch = false
diff --git a/internal/repo/tag/tag_rel_repo.go b/internal/repo/tag/tag_rel_repo.go
index edb25d47..22d26621 100644
--- a/internal/repo/tag/tag_rel_repo.go
+++ b/internal/repo/tag/tag_rel_repo.go
@@ -7,32 +7,44 @@ import (
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity"
tagcommon "github.com/answerdev/answer/internal/service/tag_common"
+ "github.com/answerdev/answer/internal/service/unique"
+ "github.com/answerdev/answer/pkg/uid"
"github.com/segmentfault/pacman/errors"
)
// tagRelRepo tag rel repository
type tagRelRepo struct {
- data *data.Data
+ data *data.Data
+ uniqueIDRepo unique.UniqueIDRepo
}
// NewTagRelRepo new repository
-func NewTagRelRepo(data *data.Data) tagcommon.TagRelRepo {
+func NewTagRelRepo(data *data.Data,
+ uniqueIDRepo unique.UniqueIDRepo) tagcommon.TagRelRepo {
return &tagRelRepo{
- data: data,
+ data: data,
+ uniqueIDRepo: uniqueIDRepo,
}
}
// AddTagRelList add tag list
func (tr *tagRelRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) {
+ for _, item := range tagList {
+ item.ObjectID = uid.DeShortID(item.ObjectID)
+ }
_, err = tr.data.DB.Insert(tagList)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range tagList {
+ item.ObjectID = uid.EnShortID(item.ObjectID)
+ }
return
}
// RemoveTagRelListByObjectID delete tag list
func (tr *tagRelRepo) RemoveTagRelListByObjectID(ctx context.Context, objectID string) (err error) {
+ objectID = uid.DeShortID(objectID)
_, err = tr.data.DB.Where("object_id = ?", objectID).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
@@ -53,12 +65,14 @@ func (tr *tagRelRepo) RemoveTagRelListByIDs(ctx context.Context, ids []int64) (e
func (tr *tagRelRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectID, tagID string) (
tagRel *entity.TagRel, exist bool, err error,
) {
+ objectID = uid.DeShortID(objectID)
tagRel = &entity.TagRel{}
session := tr.data.DB.Where("object_id = ?", objectID).And("tag_id = ?", tagID)
exist, err = session.Get(tagRel)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ tagRel.ObjectID = uid.EnShortID(tagRel.ObjectID)
return
}
@@ -73,6 +87,7 @@ func (tr *tagRelRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err e
// GetObjectTagRelList get object tag relation list all
func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string) (tagListList []*entity.TagRel, err error) {
+ objectID = uid.DeShortID(objectID)
tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.Where("object_id = ?", objectID)
session.Where("status = ?", entity.TagRelStatusAvailable)
@@ -80,11 +95,17 @@ func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string)
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range tagListList {
+ item.ObjectID = uid.EnShortID(item.ObjectID)
+ }
return
}
// BatchGetObjectTagRelList get object tag relation list all
func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []string) (tagListList []*entity.TagRel, err error) {
+ for num, item := range objectIds {
+ objectIds[num] = uid.DeShortID(item)
+ }
tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.In("object_id", objectIds)
session.Where("status = ?", entity.TagRelStatusAvailable)
@@ -92,6 +113,9 @@ func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []
if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
+ for _, item := range tagListList {
+ item.ObjectID = uid.EnShortID(item.ObjectID)
+ }
return
}
diff --git a/internal/service/revision_common/revision_service.go b/internal/service/revision_common/revision_service.go
index cf33e9d9..900cdda2 100644
--- a/internal/service/revision_common/revision_service.go
+++ b/internal/service/revision_common/revision_service.go
@@ -6,6 +6,7 @@ import (
"github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/service/revision"
usercommon "github.com/answerdev/answer/internal/service/user_common"
+ "github.com/answerdev/answer/pkg/uid"
"github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log"
@@ -20,7 +21,9 @@ type RevisionService struct {
userRepo usercommon.UserRepo
}
-func NewRevisionService(revisionRepo revision.RevisionRepo, userRepo usercommon.UserRepo) *RevisionService {
+func NewRevisionService(revisionRepo revision.RevisionRepo,
+ userRepo usercommon.UserRepo,
+) *RevisionService {
return &RevisionService{
revisionRepo: revisionRepo,
userRepo: userRepo,
@@ -42,6 +45,7 @@ func (rs *RevisionService) GetUnreviewedRevisionCount(ctx context.Context, req *
// example: user can edit the object, but need audit, the revision_id will be updated when admin approved
func (rs *RevisionService) AddRevision(ctx context.Context, req *schema.AddRevisionDTO, autoUpdateRevisionID bool) (
revisionID string, err error) {
+ req.ObjectID = uid.DeShortID(req.ObjectID)
rev := &entity.Revision{}
_ = copier.Copy(rev, req)
err = rs.revisionRepo.AddRevision(ctx, rev, autoUpdateRevisionID)
@@ -67,6 +71,7 @@ func (rs *RevisionService) GetRevision(ctx context.Context, revisionID string) (
// ExistUnreviewedByObjectID
func (rs *RevisionService) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error) {
+ objectID = uid.DeShortID(objectID)
revision, exist, err = rs.revisionRepo.ExistUnreviewedByObjectID(ctx, objectID)
return revision, exist, err
}
diff --git a/pkg/uid/sid.go b/pkg/uid/sid.go
index 5643d333..5ca4488a 100644
--- a/pkg/uid/sid.go
+++ b/pkg/uid/sid.go
@@ -1,7 +1,6 @@
package uid
import (
- "fmt"
"strconv"
)
@@ -40,7 +39,7 @@ func NumToShortID(id int64) string {
return ""
}
id = id + salt
- fmt.Println("[EN1]", typeCode, id)
+ // fmt.Println("[EN1]", typeCode, id)
var code []rune
var tcode []rune
for id > 0 {
@@ -53,7 +52,7 @@ func NumToShortID(id int64) string {
tcode = append(tcode, AlphanumericSet[idx])
typeCode = typeCode / int64(len(AlphanumericSet))
}
- fmt.Println("[EN2]", string(tcode), string(code))
+ // fmt.Println("[EN2]", string(tcode), string(code))
return string(tcode) + string(code)
}
@@ -64,7 +63,7 @@ func ShortIDToNum(code string) int64 {
}
scodeType := code[0:1]
code = code[1:int32(len(code))]
- fmt.Println("[DE1]", scodeType, code)
+ // fmt.Println("[DE1]", scodeType, code)
var id, codeType int64
runes := []rune(code)
codeRunes := []rune(scodeType)
@@ -80,7 +79,7 @@ func ShortIDToNum(code string) int64 {
codeType = codeType*int64(len(AlphanumericSet)) + int64(idx)
}
id = id - salt
- fmt.Println("[DE2]", codeType, id)
+ // fmt.Println("[DE2]", codeType, id)
return 10000000000000000 + codeType*10000000000000 + id
}
@@ -93,6 +92,12 @@ func EnShortID(id string) string {
return NumToShortID(num)
}
func DeShortID(sid string) string {
- num := ShortIDToNum(sid)
- return strconv.FormatInt(num, 10)
+ num, err := strconv.ParseInt(sid, 10, 64)
+ if err != nil {
+ return strconv.FormatInt(ShortIDToNum(sid), 10)
+ }
+ if num < 10000000000000000 {
+ return strconv.FormatInt(ShortIDToNum(sid), 10)
+ }
+ return sid
}