diff --git a/structs/storage/article.go b/structs/storage/article.go index ae2c35b..bba912a 100644 --- a/structs/storage/article.go +++ b/structs/storage/article.go @@ -6,10 +6,6 @@ import ( "gorm.io/gorm/clause" ) -type Storage struct { - ArticleApi -} - type ArticleApi interface { Upsert(article v2.Article) error Find(query FindArticleQuery) (articles []v2.Article, err error) diff --git a/structs/storage/article_test.go b/structs/storage/article_test.go new file mode 100644 index 0000000..036ed6c --- /dev/null +++ b/structs/storage/article_test.go @@ -0,0 +1,63 @@ +package storage + +import ( + "fmt" + "testing" + + v2 "gitea.timerzz.com/kedaya_haitao/common/structs/v2" + "gorm.io/driver/postgres" + "gorm.io/gorm" +) + +func GetDevDB() (*gorm.DB, error) { + dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432") + return gorm.Open(postgres.Open(dsn), &gorm.Config{}) +} + +var article = v2.Article{ + Name: "test1", + EnglishName: "test1", + Pid: "111", + Brand: "col", + Desc: "desc", + Image: "http://123", + Available: false, + CostPrice: 123, + Providers: []v2.ProviderArticle{ + { + Pid: "111", + Brand: "col1", + Link: "http://123", + ProviderId: "coach2", + SkuID: "sku1", + Cost: v2.ProviderPrice{ + OriginalPrice: 111, + FinalPrice: 111, + CalMark: "uuuuu", + }, + HistoryPrice: []v2.ProviderPrice{ + { + OriginalPrice: 111, + FinalPrice: 111, + CalMark: "uuuuu", + }, + }, + }, + }, + SellPrice: 111, + Sellers: []v2.SellerArticle{}, + Rate: 111, + Remark: "111", +} + +func TestUpsetArticle(t *testing.T) { + db, _ := GetDevDB() + storage := NewStorage(db) + err := db.AutoMigrate(&v2.Article{}, &v2.ProviderArticle{}, &v2.ProviderPrice{}, &v2.SellerArticle{}, &v2.SellerPrice{}) + if err != nil { + t.Fatal(err) + } + if err = storage.Article().Upsert(article); err != nil { + t.Fatal(err) + } +} diff --git a/structs/storage/storage.go b/structs/storage/storage.go new file mode 100644 index 0000000..6018571 --- /dev/null +++ b/structs/storage/storage.go @@ -0,0 +1,19 @@ +package storage + +import "gorm.io/gorm" + +type Storage struct { + articleApi ArticleApi +} + +func NewStorage(db *gorm.DB) *Storage { + return &Storage{ + articleApi: &articleApi{ + db: db, + }, + } +} + +func (s *Storage) Article() ArticleApi { + return s.articleApi +} diff --git a/structs/v2/model_test.go b/structs/v2/model_test.go deleted file mode 100644 index 2fc30b0..0000000 --- a/structs/v2/model_test.go +++ /dev/null @@ -1,61 +0,0 @@ -package v2 - -import ( - "fmt" - "testing" - - "gorm.io/driver/postgres" - "gorm.io/gorm" - "gorm.io/gorm/clause" -) - -func TestModel(t *testing.T) { - dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%s sslmode=disable TimeZone=Asia/Shanghai", "192.168.31.55", "timerzz", "zhhg1997", "kedaya_dev", "5432") - db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{}) - if err != nil { - t.Fatal(err) - } - err = db.AutoMigrate(&Article{}, &ProviderArticle{}, &ProviderPrice{}, &SellerArticle{}, &SellerPrice{}) - if err != nil { - t.Fatal(err) - } - var article = Article{ - Name: "test", - EnglishName: "test", - Pid: "1299999922", - Brand: "col", - Desc: "desc", - Image: "http://123", - Available: false, - CostPrice: 123, - Providers: []ProviderArticle{ - { - Pid: "999", - Brand: "col", - Link: "http://123", - ProviderId: "coach", - SkuID: "sku999999", - Cost: ProviderPrice{ - OriginalPrice: 000, - FinalPrice: 0000, - CalMark: "uuuuu", - }, - HistoryPrice: []ProviderPrice{ - { - OriginalPrice: 0000, - FinalPrice: 000, - CalMark: "uuuuu", - }, - }, - }, - }, - SellPrice: 321, - Sellers: []SellerArticle{}, - Rate: 123, - Remark: "jjjjj", - } - db.Clauses(clause.OnConflict{ - Columns: []clause.Column{{Name: "pid"}, {Name: "brand"}}, - DoUpdates: clause.AssignmentColumns([]string{"name", "english_name", "available", "updated_at"}), - }).Create(&article) -}