From c4e71fca93bf0ce385dbe8085f52a124f9e4f823 Mon Sep 17 00:00:00 2001 From: dilap54 Date: Wed, 3 Jan 2024 01:01:56 +0300 Subject: [PATCH] add good_id --- cmd/cli/plati.go | 1 + gorm/good.go | 1 + migrations/20240103005325_goods_id.sql | 14 +++++++++++++ plati/goodscategory.go | 10 ++++++++++ plati/goodscategory_test.go | 27 ++++++++++++++++++++++++++ 5 files changed, 53 insertions(+) create mode 100644 migrations/20240103005325_goods_id.sql diff --git a/cmd/cli/plati.go b/cmd/cli/plati.go index 354cea3..851e228 100644 --- a/cmd/cli/plati.go +++ b/cmd/cli/plati.go @@ -86,6 +86,7 @@ func convertGoodsToGorm(cat *plati.Category, goods []*plati.Good) []*gorm.Good { for _, g := range goods { gormGood := gorm.Good{ ID: uuid.NewV4().String(), + GoodID: g.GoodID(), Name: g.Name, IDC: cat.ID, IDR: cat.ParentID, diff --git a/gorm/good.go b/gorm/good.go index e510958..9ae0189 100644 --- a/gorm/good.go +++ b/gorm/good.go @@ -4,6 +4,7 @@ import "time" type Good struct { ID string + GoodID int Name string IDC int IDR int diff --git a/migrations/20240103005325_goods_id.sql b/migrations/20240103005325_goods_id.sql new file mode 100644 index 0000000..e075ecd --- /dev/null +++ b/migrations/20240103005325_goods_id.sql @@ -0,0 +1,14 @@ +-- +goose Up +-- +goose StatementBegin +ALTER TABLE goods +ADD COLUMN good_id INTEGER NOT NULL DEFAULT 0; + +UPDATE goods +SET good_id = split_part(goodlink, '/', 4)::integer; +-- +goose StatementEnd + +-- +goose Down +-- +goose StatementBegin +ALTER TABLE goods +DROP COLUMN good_id; +-- +goose StatementEnd diff --git a/plati/goodscategory.go b/plati/goodscategory.go index 9f4274c..edb44bd 100644 --- a/plati/goodscategory.go +++ b/plati/goodscategory.go @@ -181,6 +181,16 @@ type Good struct { Price float64 } +func (g *Good) GoodID() int { + linkArr := strings.Split(g.GoodLink, "/") + if len(linkArr) < 3 { + return 0 + } + + id, _ := strconv.Atoi(linkArr[3]) + return id +} + func parseGoodsCategory(r io.Reader) ([]*Good, error) { doc, err := goquery.NewDocumentFromReader(r) if err != nil { diff --git a/plati/goodscategory_test.go b/plati/goodscategory_test.go index 370ebbb..1a4eb30 100644 --- a/plati/goodscategory_test.go +++ b/plati/goodscategory_test.go @@ -51,3 +51,30 @@ func Test_parseGoodsCategory(t *testing.T) { }) } } + +func TestGood_GoodID(t *testing.T) { + type fields struct { + GoodLink string + } + tests := []struct { + fields fields + want int + }{ + { + fields: fields{ + GoodLink: "/itm/mortal-kombat-1-ua-kz-auto-24-7/4107232", + }, + want: 4107232, + }, + } + for _, tt := range tests { + t.Run(tt.fields.GoodLink, func(t *testing.T) { + g := &Good{ + GoodLink: tt.fields.GoodLink, + } + if got := g.GoodID(); got != tt.want { + t.Errorf("Good.GoodID() = %v, want %v", got, tt.want) + } + }) + } +}