Compare commits

..

No commits in common. "84e608f4bb709369c294f2eb649ae182b9e3a345" and "8e1293f4b735ebf679d9802c8a69f95f8f674315" have entirely different histories.

5 changed files with 40 additions and 58 deletions

View File

@ -48,8 +48,8 @@ type TagModifyParams struct {
// TagBindingRepo 定義與 tag binding (TagsBindingTable) 資料表相關的操作 // TagBindingRepo 定義與 tag binding (TagsBindingTable) 資料表相關的操作
type TagBindingRepo interface { type TagBindingRepo interface {
// BindTags 建立一筆 tag 與其他資料(例如專案)的綁定關係 // BindTag 建立一筆 tag 與其他資料(例如專案)的綁定關係
BindTags(ctx context.Context, binding []*entity.TagsBindingTable) error BindTag(ctx context.Context, binding *entity.TagsBindingTable) error
// UnbindTag 刪除一筆綁定資料 // UnbindTag 刪除一筆綁定資料
UnbindTag(ctx context.Context, tagID, referenceID string) error UnbindTag(ctx context.Context, tagID, referenceID string) error
// GetBindingsByReference 根據參照 ID 取得所有綁定資料 // GetBindingsByReference 根據參照 ID 取得所有綁定資料

View File

@ -12,22 +12,15 @@ import (
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
) )
func (repo *TagsRepository) BindTags(ctx context.Context, data []*entity.TagsBindingTable) error { func (repo *TagsRepository) BindTag(ctx context.Context, data *entity.TagsBindingTable) error {
now := time.Now().UTC().UnixNano() if data.ID.IsZero() {
docs := make([]any, 0, len(data)) now := time.Now().UTC().UnixNano()
data.ID = primitive.NewObjectID()
data.CreatedAt = now
data.UpdatedAt = now
}
for i := range data { _, err := repo.TageBinding.GetClient().InsertOne(ctx, data)
if data[i].ID.IsZero() {
data[i].ID = primitive.NewObjectID()
data[i].CreatedAt = now
data[i].UpdatedAt = now
}
docs = append(docs, data[i])
}
if len(docs) == 0 {
return nil
}
_, err := repo.TageBinding.GetClient().InsertMany(ctx, docs)
return err return err
} }

View File

@ -545,7 +545,7 @@ func TestBindTag(t *testing.T) {
for _, tc := range tests { for _, tc := range tests {
tc := tc // capture range variable tc := tc // capture range variable
t.Run(tc.name, func(t *testing.T) { t.Run(tc.name, func(t *testing.T) {
err := repo.BindTags(ctx, []*entity.TagsBindingTable{tc.inputBinding}) err := repo.BindTag(ctx, tc.inputBinding)
if tc.expectError { if tc.expectError {
require.Error(t, err) require.Error(t, err)
} else { } else {
@ -581,7 +581,11 @@ func TestGetBindingsByReference(t *testing.T) {
} }
// 插入資料 // 插入資料
err = repo.BindTags(ctx, []*entity.TagsBindingTable{binding1, binding2, binding3}) err = repo.BindTag(ctx, binding1)
require.NoError(t, err)
err = repo.BindTag(ctx, binding2)
require.NoError(t, err)
err = repo.BindTag(ctx, binding3)
require.NoError(t, err) require.NoError(t, err)
tests := []struct { tests := []struct {
@ -661,8 +665,14 @@ func TestListTagBinding(t *testing.T) {
UpdatedAt: 2500, UpdatedAt: 2500,
} }
// 插入資料 // 插入綁定資料
err = repo.BindTags(ctx, []*entity.TagsBindingTable{binding1, binding2, binding3, binding4}) err = repo.BindTag(ctx, binding1)
require.NoError(t, err)
err = repo.BindTag(ctx, binding2)
require.NoError(t, err)
err = repo.BindTag(ctx, binding3)
require.NoError(t, err)
err = repo.BindTag(ctx, binding4)
require.NoError(t, err) require.NoError(t, err)
// 測試案例 // 測試案例
@ -757,7 +767,7 @@ func TestUnbindTag(t *testing.T) {
ReferenceID: "ref-001", ReferenceID: "ref-001",
TagID: "tag-001", TagID: "tag-001",
} }
err = repo.BindTags(ctx, []*entity.TagsBindingTable{binding}) err = repo.BindTag(ctx, binding)
require.NoError(t, err) require.NoError(t, err)
tests := []struct { tests := []struct {

View File

@ -68,7 +68,6 @@ func SetupTestProductRepository(db string) (repository.ProductRepository, func()
func TestListProduct(t *testing.T) { func TestListProduct(t *testing.T) {
model, tearDown, err := SetupTestProductRepository("testDB") model, tearDown, err := SetupTestProductRepository("testDB")
defer tearDown() defer tearDown()
fmt.Println("ddddddddddddddddddddd", err.Error())
assert.NoError(t, err) assert.NoError(t, err)
now := time.Now() now := time.Now()

View File

@ -5,12 +5,14 @@ import (
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/repository"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase" "code.30cm.net/digimon/app-cloudep-product-service/pkg/domain/usecase"
"code.30cm.net/digimon/app-cloudep-product-service/pkg/utils" "code.30cm.net/digimon/app-cloudep-product-service/pkg/utils"
"code.30cm.net/digimon/library-go/errs"
"context" "context"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options" "go.mongodb.org/mongo-driver/mongo/options"
"go.mongodb.org/mongo-driver/mongo/readconcern" "go.mongodb.org/mongo-driver/mongo/readconcern"
"code.30cm.net/digimon/library-go/errs"
"code.30cm.net/digimon/library-go/errs/code"
) )
type ProductUseCaseParam struct { type ProductUseCaseParam struct {
@ -96,15 +98,8 @@ func (use *ProductUseCase) Create(ctx context.Context, product *usecase.Product)
err := use.ProductRepo.Transaction(ctx, func(sessCtx mongo.SessionContext) (any, error) { err := use.ProductRepo.Transaction(ctx, func(sessCtx mongo.SessionContext) (any, error) {
// 插入 Product // 插入 Product
if err := use.ProductRepo.Insert(sessCtx, insert); err != nil { if err := use.ProductRepo.Insert(sessCtx, insert); err != nil {
e := errs.DBErrorL(logx.WithContext(ctx), e := errs.DBError(code.CloudEPProduct, 0)
[]logx.LogField{ return nil, err
{Key: "req", Value: product},
{Key: "func", Value: "ProductRepo.Insert"},
{Key: "err", Value: err.Error()},
},
"failed to create product")
return nil, e
} }
// 插入 Product 統計資料 // 插入 Product 統計資料
@ -117,35 +112,20 @@ func (use *ProductUseCase) Create(ctx context.Context, product *usecase.Product)
FansCount: 0, FansCount: 0,
FansCountUpdateTime: 0, FansCountUpdateTime: 0,
}); err != nil { }); err != nil {
e := errs.DBErrorL(logx.WithContext(ctx), return nil, err
[]logx.LogField{
{Key: "ProductID", Value: insert.ID.Hex()},
{Key: "func", Value: "ProductStatisticsRepo.Create"},
{Key: "err", Value: err.Error()},
},
"failed to create product statistics")
return nil, e
} }
// 過濾 Tag // 過濾 Tag
// 綁定 Tags // 綁定 Tags
//for _, tag := range product.Tags { for _, tag := range product.Tags {
// if err := use.TagBinding.BindTag(sessCtx, &entity.TagsBindingTable{ if err := use.TagBinding.BindTag(sessCtx, &entity.TagsBindingTable{
// ReferenceID: insert.ID.Hex(), ReferenceID: insert.ID.Hex(),
// TagID: tag, TagID: tag,
// }); err != nil { }); err != nil {
// _ = errs.DBErrorL(logx.WithContext(ctx), logx.Errorf("failed to bind tag %s to product %s: %v", tag, insert.ID.Hex(), err)
// []logx.LogField{ return nil, err
// {Key: "ReferenceID", Value: insert.ID.Hex()}, }
// {Key: "TagID", Value: tag}, }
// {Key: "func", Value: "TagBinding.BindTag"},
// {Key: "err", Value: err.Error()},
// }, "")
//
// continue
// }
//}
return nil, nil return nil, nil
}, opts) }, opts)