update short id

This commit is contained in:
aichy126 2023-03-03 15:47:56 +08:00
parent 4b1e63b695
commit 789cf6bd8b
8 changed files with 115 additions and 24 deletions

View File

@ -128,7 +128,7 @@ func initApplication(debug bool, serverConf *conf.Server, dbConf *data.Database,
answerRepo := answer.NewAnswerRepo(dataData, uniqueIDRepo, userRankRepo, activityRepo) answerRepo := answer.NewAnswerRepo(dataData, uniqueIDRepo, userRankRepo, activityRepo)
questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo) questionRepo := question.NewQuestionRepo(dataData, uniqueIDRepo)
tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo) tagCommonRepo := tag_common.NewTagCommonRepo(dataData, uniqueIDRepo)
tagRelRepo := tag.NewTagRelRepo(dataData) tagRelRepo := tag.NewTagRelRepo(dataData, uniqueIDRepo)
tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo) tagRepo := tag.NewTagRepo(dataData, uniqueIDRepo)
revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo) revisionRepo := revision.NewRevisionRepo(dataData, uniqueIDRepo)
revisionService := revision_common.NewRevisionService(revisionRepo, userRepo) revisionService := revision_common.NewRevisionService(revisionRepo, userRepo)

View File

@ -1,5 +1,4 @@
// Package docs GENERATED BY SWAG; DO NOT EDIT // Code generated by swaggo/swag. DO NOT EDIT
// This file was generated by swaggo/swag
package docs package docs
import "github.com/swaggo/swag" import "github.com/swaggo/swag"
@ -6986,7 +6985,11 @@ const docTemplate = `{
}, },
"user_info": { "user_info": {
"description": "user info", "description": "user info",
"$ref": "#/definitions/schema.UserBasicInfo" "allOf": [
{
"$ref": "#/definitions/schema.UserBasicInfo"
}
]
}, },
"vote_count": { "vote_count": {
"type": "integer" "type": "integer"
@ -6998,7 +7001,11 @@ const docTemplate = `{
"properties": { "properties": {
"object": { "object": {
"description": "this object", "description": "this object",
"$ref": "#/definitions/schema.SearchObject" "allOf": [
{
"$ref": "#/definitions/schema.SearchObject"
}
]
}, },
"object_type": { "object_type": {
"description": "object_type", "description": "object_type",
@ -7511,7 +7518,11 @@ const docTemplate = `{
"properties": { "properties": {
"avatar": { "avatar": {
"description": "avatar", "description": "avatar",
"$ref": "#/definitions/schema.AvatarInfo" "allOf": [
{
"$ref": "#/definitions/schema.AvatarInfo"
}
]
}, },
"bio": { "bio": {
"description": "bio", "description": "bio",

View File

@ -6974,7 +6974,11 @@
}, },
"user_info": { "user_info": {
"description": "user info", "description": "user info",
"$ref": "#/definitions/schema.UserBasicInfo" "allOf": [
{
"$ref": "#/definitions/schema.UserBasicInfo"
}
]
}, },
"vote_count": { "vote_count": {
"type": "integer" "type": "integer"
@ -6986,7 +6990,11 @@
"properties": { "properties": {
"object": { "object": {
"description": "this object", "description": "this object",
"$ref": "#/definitions/schema.SearchObject" "allOf": [
{
"$ref": "#/definitions/schema.SearchObject"
}
]
}, },
"object_type": { "object_type": {
"description": "object_type", "description": "object_type",
@ -7499,7 +7507,11 @@
"properties": { "properties": {
"avatar": { "avatar": {
"description": "avatar", "description": "avatar",
"$ref": "#/definitions/schema.AvatarInfo" "allOf": [
{
"$ref": "#/definitions/schema.AvatarInfo"
}
]
}, },
"bio": { "bio": {
"description": "bio", "description": "bio",

View File

@ -1201,7 +1201,8 @@ definitions:
title: title:
type: string type: string
user_info: user_info:
$ref: '#/definitions/schema.UserBasicInfo' allOf:
- $ref: '#/definitions/schema.UserBasicInfo'
description: user info description: user info
vote_count: vote_count:
type: integer type: integer
@ -1209,7 +1210,8 @@ definitions:
schema.SearchResp: schema.SearchResp:
properties: properties:
object: object:
$ref: '#/definitions/schema.SearchObject' allOf:
- $ref: '#/definitions/schema.SearchObject'
description: this object description: this object
object_type: object_type:
description: object_type description: object_type
@ -1563,7 +1565,8 @@ definitions:
schema.UpdateInfoRequest: schema.UpdateInfoRequest:
properties: properties:
avatar: avatar:
$ref: '#/definitions/schema.AvatarInfo' allOf:
- $ref: '#/definitions/schema.AvatarInfo'
description: avatar description: avatar
bio: bio:
description: bio description: bio

View File

@ -18,6 +18,7 @@ import (
questioncommon "github.com/answerdev/answer/internal/service/question_common" questioncommon "github.com/answerdev/answer/internal/service/question_common"
"github.com/answerdev/answer/internal/service/unique" "github.com/answerdev/answer/internal/service/unique"
"github.com/answerdev/answer/pkg/htmltext" "github.com/answerdev/answer/pkg/htmltext"
"github.com/answerdev/answer/pkg/uid"
"github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/errors"
) )
@ -49,11 +50,13 @@ func (qr *questionRepo) AddQuestion(ctx context.Context, question *entity.Questi
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
question.ID = uid.EnShortID(question.ID)
return return
} }
// RemoveQuestion delete question // RemoveQuestion delete question
func (qr *questionRepo) RemoveQuestion(ctx context.Context, id string) (err error) { 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{}) _, err = qr.data.DB.Where("id =?", id).Delete(&entity.Question{})
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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 // UpdateQuestion update question
func (qr *questionRepo) UpdateQuestion(ctx context.Context, question *entity.Question, Cols []string) (err error) { 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) _, err = qr.data.DB.Where("id =?", question.ID).Cols(Cols...).Update(question)
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
question.ID = uid.EnShortID(question.ID)
return return
} }
func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (err error) { func (qr *questionRepo) UpdatePvCount(ctx context.Context, questionID string) (err error) {
questionID = uid.DeShortID(questionID)
question := &entity.Question{} question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("view_count", 1).Update(question) _, err = qr.data.DB.Where("id =?", questionID).Incr("view_count", 1).Update(question)
if err != nil { 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) { func (qr *questionRepo) UpdateAnswerCount(ctx context.Context, questionID string, num int) (err error) {
questionID = uid.DeShortID(questionID)
question := &entity.Question{} question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("answer_count", num).Update(question) _, err = qr.data.DB.Where("id =?", questionID).Incr("answer_count", num).Update(question)
if err != nil { 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) { func (qr *questionRepo) UpdateCollectionCount(ctx context.Context, questionID string, num int) (err error) {
questionID = uid.DeShortID(questionID)
question := &entity.Question{} question := &entity.Question{}
_, err = qr.data.DB.Where("id =?", questionID).Incr("collection_count", num).Update(question) _, err = qr.data.DB.Where("id =?", questionID).Incr("collection_count", num).Update(question)
if err != nil { 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) { func (qr *questionRepo) UpdateQuestionStatus(ctx context.Context, question *entity.Question) (err error) {
question.ID = uid.DeShortID(question.ID)
now := time.Now() now := time.Now()
question.UpdatedAt = now question.UpdatedAt = now
_, err = qr.data.DB.Where("id =?", question.ID).Cols("status", "updated_at").Update(question) _, 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) { 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) _, err = qr.data.DB.Where("id =?", question.ID).Cols("status").Update(question)
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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) { 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) _, err = qr.data.DB.Where("id =?", question.ID).Cols("accepted_answer_id").Update(question)
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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) { 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) _, err = qr.data.DB.Where("id =?", question.ID).Cols("last_answer_id").Update(question)
if err != nil { if err != nil {
return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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) ( func (qr *questionRepo) GetQuestion(ctx context.Context, id string) (
question *entity.Question, exist bool, err error, question *entity.Question, exist bool, err error,
) { ) {
id = uid.DeShortID(id)
question = &entity.Question{} question = &entity.Question{}
question.ID = id question.ID = id
exist, err = qr.data.DB.Where("id = ?", id).Get(question) exist, err = qr.data.DB.Where("id = ?", id).Get(question)
if err != nil { if err != nil {
return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return nil, false, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
question.ID = uid.EnShortID(question.ID)
return return
} }
@ -151,25 +165,38 @@ func (qr *questionRepo) SearchByTitleLike(ctx context.Context, title string) (qu
if err != nil { if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range questionList {
item.ID = uid.EnShortID(item.ID)
}
return return
} }
func (qr *questionRepo) FindByID(ctx context.Context, id []string) (questionList []*entity.Question, err error) { 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) questionList = make([]*entity.Question, 0)
err = qr.data.DB.Table("question").In("id", id).Find(&questionList) err = qr.data.DB.Table("question").In("id", id).Find(&questionList)
if err != nil { if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range questionList {
item.ID = uid.DeShortID(item.ID)
}
return return
} }
// GetQuestionList get question list all // GetQuestionList get question list all
func (qr *questionRepo) GetQuestionList(ctx context.Context, question *entity.Question) (questionList []*entity.Question, err error) { 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) questionList = make([]*entity.Question, 0)
err = qr.data.DB.Find(questionList, question) err = qr.data.DB.Find(questionList, question)
if err != nil { if err != nil {
return questionList, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() return questionList, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range questionList {
item.ID = uid.DeShortID(item.ID)
}
return return
} }
@ -205,7 +232,7 @@ func (qr *questionRepo) GetQuestionIDsPage(ctx context.Context, page, pageSize i
} }
for _, question := range rows { for _, question := range rows {
item := &schema.SiteMapQuestionInfo{} item := &schema.SiteMapQuestionInfo{}
item.ID = question.ID item.ID = uid.EnShortID(question.ID)
item.Title = htmltext.UrlTitle(question.Title) item.Title = htmltext.UrlTitle(question.Title)
item.UpdateTime = fmt.Sprintf("%v", question.PostUpdateTime.Format(time.RFC3339)) item.UpdateTime = fmt.Sprintf("%v", question.PostUpdateTime.Format(time.RFC3339))
questionIDList = append(questionIDList, item) questionIDList = append(questionIDList, item)
@ -246,6 +273,9 @@ func (qr *questionRepo) GetQuestionPage(ctx context.Context, page, pageSize int,
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range questionList {
item.ID = uid.EnShortID(item.ID)
}
return questionList, total, err return questionList, total, err
} }
@ -280,6 +310,7 @@ func (qr *questionRepo) AdminSearchList(ctx context.Context, search *schema.Admi
if strings.Contains(search.Query, "question:") { if strings.Contains(search.Query, "question:") {
idSearch = true idSearch = true
id = strings.TrimSpace(strings.TrimPrefix(search.Query, "question:")) id = strings.TrimSpace(strings.TrimPrefix(search.Query, "question:"))
id = uid.DeShortID(id)
for _, r := range id { for _, r := range id {
if !unicode.IsDigit(r) { if !unicode.IsDigit(r) {
idSearch = false idSearch = false

View File

@ -7,32 +7,44 @@ import (
"github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/entity" "github.com/answerdev/answer/internal/entity"
tagcommon "github.com/answerdev/answer/internal/service/tag_common" 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" "github.com/segmentfault/pacman/errors"
) )
// tagRelRepo tag rel repository // tagRelRepo tag rel repository
type tagRelRepo struct { type tagRelRepo struct {
data *data.Data data *data.Data
uniqueIDRepo unique.UniqueIDRepo
} }
// NewTagRelRepo new repository // NewTagRelRepo new repository
func NewTagRelRepo(data *data.Data) tagcommon.TagRelRepo { func NewTagRelRepo(data *data.Data,
uniqueIDRepo unique.UniqueIDRepo) tagcommon.TagRelRepo {
return &tagRelRepo{ return &tagRelRepo{
data: data, data: data,
uniqueIDRepo: uniqueIDRepo,
} }
} }
// AddTagRelList add tag list // AddTagRelList add tag list
func (tr *tagRelRepo) AddTagRelList(ctx context.Context, tagList []*entity.TagRel) (err error) { 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) _, err = tr.data.DB.Insert(tagList)
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range tagList {
item.ObjectID = uid.EnShortID(item.ObjectID)
}
return return
} }
// RemoveTagRelListByObjectID delete tag list // RemoveTagRelListByObjectID delete tag list
func (tr *tagRelRepo) RemoveTagRelListByObjectID(ctx context.Context, objectID string) (err error) { 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}) _, err = tr.data.DB.Where("object_id = ?", objectID).Update(&entity.TagRel{Status: entity.TagRelStatusDeleted})
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() 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) ( func (tr *tagRelRepo) GetObjectTagRelWithoutStatus(ctx context.Context, objectID, tagID string) (
tagRel *entity.TagRel, exist bool, err error, tagRel *entity.TagRel, exist bool, err error,
) { ) {
objectID = uid.DeShortID(objectID)
tagRel = &entity.TagRel{} tagRel = &entity.TagRel{}
session := tr.data.DB.Where("object_id = ?", objectID).And("tag_id = ?", tagID) session := tr.data.DB.Where("object_id = ?", objectID).And("tag_id = ?", tagID)
exist, err = session.Get(tagRel) exist, err = session.Get(tagRel)
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
tagRel.ObjectID = uid.EnShortID(tagRel.ObjectID)
return return
} }
@ -73,6 +87,7 @@ func (tr *tagRelRepo) EnableTagRelByIDs(ctx context.Context, ids []int64) (err e
// GetObjectTagRelList get object tag relation list all // GetObjectTagRelList get object tag relation list all
func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string) (tagListList []*entity.TagRel, err error) { func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string) (tagListList []*entity.TagRel, err error) {
objectID = uid.DeShortID(objectID)
tagListList = make([]*entity.TagRel, 0) tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.Where("object_id = ?", objectID) session := tr.data.DB.Where("object_id = ?", objectID)
session.Where("status = ?", entity.TagRelStatusAvailable) session.Where("status = ?", entity.TagRelStatusAvailable)
@ -80,11 +95,17 @@ func (tr *tagRelRepo) GetObjectTagRelList(ctx context.Context, objectID string)
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range tagListList {
item.ObjectID = uid.EnShortID(item.ObjectID)
}
return return
} }
// BatchGetObjectTagRelList get object tag relation list all // BatchGetObjectTagRelList get object tag relation list all
func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []string) (tagListList []*entity.TagRel, err error) { 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) tagListList = make([]*entity.TagRel, 0)
session := tr.data.DB.In("object_id", objectIds) session := tr.data.DB.In("object_id", objectIds)
session.Where("status = ?", entity.TagRelStatusAvailable) session.Where("status = ?", entity.TagRelStatusAvailable)
@ -92,6 +113,9 @@ func (tr *tagRelRepo) BatchGetObjectTagRelList(ctx context.Context, objectIds []
if err != nil { if err != nil {
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack() err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
} }
for _, item := range tagListList {
item.ObjectID = uid.EnShortID(item.ObjectID)
}
return return
} }

View File

@ -6,6 +6,7 @@ import (
"github.com/answerdev/answer/internal/base/reason" "github.com/answerdev/answer/internal/base/reason"
"github.com/answerdev/answer/internal/service/revision" "github.com/answerdev/answer/internal/service/revision"
usercommon "github.com/answerdev/answer/internal/service/user_common" usercommon "github.com/answerdev/answer/internal/service/user_common"
"github.com/answerdev/answer/pkg/uid"
"github.com/segmentfault/pacman/errors" "github.com/segmentfault/pacman/errors"
"github.com/segmentfault/pacman/log" "github.com/segmentfault/pacman/log"
@ -20,7 +21,9 @@ type RevisionService struct {
userRepo usercommon.UserRepo userRepo usercommon.UserRepo
} }
func NewRevisionService(revisionRepo revision.RevisionRepo, userRepo usercommon.UserRepo) *RevisionService { func NewRevisionService(revisionRepo revision.RevisionRepo,
userRepo usercommon.UserRepo,
) *RevisionService {
return &RevisionService{ return &RevisionService{
revisionRepo: revisionRepo, revisionRepo: revisionRepo,
userRepo: userRepo, 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 // 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) ( func (rs *RevisionService) AddRevision(ctx context.Context, req *schema.AddRevisionDTO, autoUpdateRevisionID bool) (
revisionID string, err error) { revisionID string, err error) {
req.ObjectID = uid.DeShortID(req.ObjectID)
rev := &entity.Revision{} rev := &entity.Revision{}
_ = copier.Copy(rev, req) _ = copier.Copy(rev, req)
err = rs.revisionRepo.AddRevision(ctx, rev, autoUpdateRevisionID) err = rs.revisionRepo.AddRevision(ctx, rev, autoUpdateRevisionID)
@ -67,6 +71,7 @@ func (rs *RevisionService) GetRevision(ctx context.Context, revisionID string) (
// ExistUnreviewedByObjectID // ExistUnreviewedByObjectID
func (rs *RevisionService) ExistUnreviewedByObjectID(ctx context.Context, objectID string) (revision *entity.Revision, exist bool, err error) { 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) revision, exist, err = rs.revisionRepo.ExistUnreviewedByObjectID(ctx, objectID)
return revision, exist, err return revision, exist, err
} }

View File

@ -1,7 +1,6 @@
package uid package uid
import ( import (
"fmt"
"strconv" "strconv"
) )
@ -40,7 +39,7 @@ func NumToShortID(id int64) string {
return "" return ""
} }
id = id + salt id = id + salt
fmt.Println("[EN1]", typeCode, id) // fmt.Println("[EN1]", typeCode, id)
var code []rune var code []rune
var tcode []rune var tcode []rune
for id > 0 { for id > 0 {
@ -53,7 +52,7 @@ func NumToShortID(id int64) string {
tcode = append(tcode, AlphanumericSet[idx]) tcode = append(tcode, AlphanumericSet[idx])
typeCode = typeCode / int64(len(AlphanumericSet)) typeCode = typeCode / int64(len(AlphanumericSet))
} }
fmt.Println("[EN2]", string(tcode), string(code)) // fmt.Println("[EN2]", string(tcode), string(code))
return string(tcode) + string(code) return string(tcode) + string(code)
} }
@ -64,7 +63,7 @@ func ShortIDToNum(code string) int64 {
} }
scodeType := code[0:1] scodeType := code[0:1]
code = code[1:int32(len(code))] code = code[1:int32(len(code))]
fmt.Println("[DE1]", scodeType, code) // fmt.Println("[DE1]", scodeType, code)
var id, codeType int64 var id, codeType int64
runes := []rune(code) runes := []rune(code)
codeRunes := []rune(scodeType) codeRunes := []rune(scodeType)
@ -80,7 +79,7 @@ func ShortIDToNum(code string) int64 {
codeType = codeType*int64(len(AlphanumericSet)) + int64(idx) codeType = codeType*int64(len(AlphanumericSet)) + int64(idx)
} }
id = id - salt id = id - salt
fmt.Println("[DE2]", codeType, id) // fmt.Println("[DE2]", codeType, id)
return 10000000000000000 + codeType*10000000000000 + id return 10000000000000000 + codeType*10000000000000 + id
} }
@ -93,6 +92,12 @@ func EnShortID(id string) string {
return NumToShortID(num) return NumToShortID(num)
} }
func DeShortID(sid string) string { func DeShortID(sid string) string {
num := ShortIDToNum(sid) num, err := strconv.ParseInt(sid, 10, 64)
return strconv.FormatInt(num, 10) if err != nil {
return strconv.FormatInt(ShortIDToNum(sid), 10)
}
if num < 10000000000000000 {
return strconv.FormatInt(ShortIDToNum(sid), 10)
}
return sid
} }