Skip to content

Commit

Permalink
Fix/修改cloudmind content.file.find and insert查询不存在bug (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
Love-YeLin authored Feb 11, 2024
1 parent 736f420 commit c21e4fb
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 3 deletions.
3 changes: 2 additions & 1 deletion biz/application/service/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,12 +562,13 @@ func (s *FileService) SaveFileToPrivateSpace(ctx context.Context, req *genconten
var err1 error
file := convertor.FileInfoToFileMapper(req.File)
tx := s.FileMongoMapper.StartClient()

err = tx.UseSession(ctx, func(sessionContext mongo.SessionContext) error {
if err1 = sessionContext.StartTransaction(); err1 != nil {
return err1
}
if resp.FileId, err1 = s.FileMongoMapper.FindAndInsert(sessionContext, &filemapper.File{ // 创建根文件
UserId: file.UserId,
UserId: req.UserId,
Name: file.Name,
Type: file.Type,
Path: req.NewPath,
Expand Down
6 changes: 5 additions & 1 deletion biz/infrastructure/mapper/file/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package file
import (
"context"
errorx "errors"
"fmt"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/config"
"github.com/CloudStriver/cloudmind-content/biz/infrastructure/consts"
"github.com/CloudStriver/go-pkg/utils/pagination"
Expand Down Expand Up @@ -148,9 +149,11 @@ func (m *MongoMapper) FindAndInsert(ctx context.Context, data *File) (string, er
_, span := tracer.Start(ctx, "mongo.FindAndInsert", oteltrace.WithSpanKind(oteltrace.SpanKindConsumer))
defer span.End()

fmt.Printf("\n[%v]\n", *data)

var res File
if err := m.conn.FindOneNoCache(ctx, &res, bson.M{consts.FatherId: data.FatherId, consts.Name: data.Name, consts.IsDel: data.IsDel}); err != nil {
if errorx.Is(err, consts.ErrNotFound) {
if errorx.Is(err, monc.ErrNotFound) {
return m.Insert(ctx, data)
}
return "", err
Expand Down Expand Up @@ -231,6 +234,7 @@ func (m *MongoMapper) Insert(ctx context.Context, data *File) (string, error) {
data.UpdateAt = time.Now()
data.Path = data.Path + "/" + data.ID.Hex()
key := prefixFileCacheKey + data.ID.Hex()
fmt.Printf("\n[%v]\n", *data)
ID, err := m.conn.InsertOne(ctx, key, data)
if err != nil {
data.Name = data.Name + "_" + strconv.FormatInt(time.Now().Unix(), 10)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211013020-a79ff79a2608
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211052850-770a3944778c
github.com/bytedance/sonic v1.10.2
github.com/cloudwego/kitex v0.8.0
github.com/elastic/go-elasticsearch/v8 v8.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d h1:c5M3637HJJN
github.com/CloudStriver/go-pkg v0.0.0-20231229114943-910edcb8788d/go.mod h1:Oj6+J8ixQxqNunSpl9v64q6tAQ83wY/rRFI+3rhHVCk=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211013020-a79ff79a2608 h1:2bHkcHJ/rU8SrwRhujqyO6f9e65RJVTQAM2zH0ueo1A=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211013020-a79ff79a2608/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211052850-770a3944778c h1:eGqGFWc4cW8CEPUYbvVbaYecBcqr77bfx9d/NIseT7Y=
github.com/CloudStriver/service-idl-gen-go v0.0.0-20240211052850-770a3944778c/go.mod h1:chtR82RvfrjUujTGWROSCNAwF9Lh/U959k34bXIDvBI=
github.com/ajstarks/deck v0.0.0-20200831202436-30c9fc6549a9/go.mod h1:JynElWSGnm/4RlzPXRlREEwqTHAN3T56Bv2ITsFT3gY=
github.com/ajstarks/deck/generate v0.0.0-20210309230005-c3f852c02e19/go.mod h1:T13YZdzov6OU0A1+RfKZiZN9ca6VeKdBdyDV+BY97Tk=
github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3/J6wwsYMMT4xOr94bZjxIelGM0+d/wbFw=
Expand Down

0 comments on commit c21e4fb

Please sign in to comment.