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 add db session
aichy1261ee34b884b90
diff --git a/docs/docs.go b/docs/docs.goindex 1660ee72..f0d447e2 100644--- a/docs/docs.go+++ b/docs/docs.go@@ -7507,11 +7507,6 @@ const docTemplate = `{"type": "string","maxLength": 4096},- "bio_html": {- "description": "bio",- "type": "string",- "maxLength": 4096- },"display_name": {"description": "display_name","type": "string",diff --git a/docs/swagger.json b/docs/swagger.jsonindex 91ca0f4c..ec2b0db0 100644--- a/docs/swagger.json+++ b/docs/swagger.json@@ -7495,11 +7495,6 @@"type": "string","maxLength": 4096},- "bio_html": {- "description": "bio",- "type": "string",- "maxLength": 4096- },"display_name": {"description": "display_name","type": "string",diff --git a/docs/swagger.yaml b/docs/swagger.yamlindex 232fe850..8e5ccf5b 100644--- a/docs/swagger.yaml+++ b/docs/swagger.yaml@@ -1587,10 +1587,6 @@ definitions:description: biomaxLength: 4096type: string- bio_html:- description: bio- maxLength: 4096- type: stringdisplay_name:description: display_namemaxLength: 30diff --git a/internal/repo/collection/collection_repo.go b/internal/repo/collection/collection_repo.goindex f73c74b7..396398c5 100644--- a/internal/repo/collection/collection_repo.go+++ b/internal/repo/collection/collection_repo.go@@ -11,6 +11,7 @@ import (collectioncommon "github.com/answerdev/answer/internal/service/collection_common""github.com/answerdev/answer/internal/service/unique""github.com/segmentfault/pacman/errors"+ "xorm.io/xorm")// collectionRepo collection repository@@ -29,15 +30,28 @@ func NewCollectionRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo) collec// AddCollection add collectionfunc (cr *collectionRepo) AddCollection(ctx context.Context, collection *entity.Collection) (err error) {- id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())- if err == nil {- collection.ID = id- _, err = cr.data.DB.Insert(collection)+ _, err = cr.data.DB.Transaction(func(session *xorm.Session) (result any, err error) {+ var has bool+ dbcollection := &entity.Collection{}+ result = nil+ has, err = session.Where("user_id = ? and object_id = ?", collection.UserID, collection.ObjectID).Get(dbcollection)if err != nil {- return errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ return}- }- return nil+ if has {+ return+ }+ id, err := cr.uniqueIDRepo.GenUniqueIDStr(ctx, collection.TableName())+ if err == nil {+ collection.ID = id+ _, err = session.Insert(collection)+ if err != nil {+ return result, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()+ }+ }+ return+ })+ return err}// RemoveCollection delete collection