Skip to content

Commit

Permalink
update add db session
Browse files Browse the repository at this point in the history
  • Loading branch information
aichy126 committed Jan 30, 2023
1 parent 198d293 commit 1ee34b8
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 21 deletions.
5 changes: 0 additions & 5 deletions docs/docs.go
Expand Up @@ -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",
Expand Down
5 changes: 0 additions & 5 deletions docs/swagger.json
Expand Up @@ -7495,11 +7495,6 @@
"type": "string",
"maxLength": 4096
},
"bio_html": {
"description": "bio",
"type": "string",
"maxLength": 4096
},
"display_name": {
"description": "display_name",
"type": "string",
Expand Down
4 changes: 0 additions & 4 deletions docs/swagger.yaml
Expand Up @@ -1587,10 +1587,6 @@ definitions:
description: bio
maxLength: 4096
type: string
bio_html:
description: bio
maxLength: 4096
type: string
display_name:
description: display_name
maxLength: 30
Expand Down
28 changes: 21 additions & 7 deletions internal/repo/collection/collection_repo.go
Expand Up @@ -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
Expand All @@ -29,15 +30,28 @@ func NewCollectionRepo(data *data.Data, uniqueIDRepo unique.UniqueIDRepo) collec

// AddCollection add collection
func (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
Expand Down

0 comments on commit 1ee34b8

Please sign in to comment.