From 4dd1c3feeb4b4e273d127aa01d251f013add4583 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20G=C3=BCney?= Date: Fri, 23 Jun 2023 14:37:15 +0300 Subject: [PATCH] code refactored --- broker/ncm/adapter.go | 29 -------------------------- {broker => brokers}/garanti/garanti.go | 18 ++++++++-------- brokers/ncm/ncm.go | 28 +++++++++++++++++++++++++ {broker => entity}/broker.go | 12 +++++------ entity/{entity.go => transaction.go} | 0 module.go | 21 +++++++++---------- module_test.go | 12 +++++------ 7 files changed, 59 insertions(+), 61 deletions(-) delete mode 100644 broker/ncm/adapter.go rename {broker => brokers}/garanti/garanti.go (80%) create mode 100644 brokers/ncm/ncm.go rename {broker => entity}/broker.go (62%) rename entity/{entity.go => transaction.go} (100%) diff --git a/broker/ncm/adapter.go b/broker/ncm/adapter.go deleted file mode 100644 index 17f51cb..0000000 --- a/broker/ncm/adapter.go +++ /dev/null @@ -1,29 +0,0 @@ -package ncm - -import ( - "github.com/guneyin/gobist-broker/broker" - "github.com/guneyin/gobist-broker/entity" -) - -type NCM struct { - info broker.Info -} - -func New() *NCM { - return &NCM{ - info: broker.Info{ - Enum: broker.NCM, - Name: "NCM Investment", - Title: "NCM Investment Menkul Değerler A.Ş.", - Url: "https://ncminvest.com.tr/", - Logo: "https://ncminvest.com.tr/Resimler/5/5-logo.webp", - }} -} - -func (b NCM) Info() broker.Info { - return b.info -} - -func (b NCM) Parse(content []byte) (*entity.Transactions, error) { - return nil, nil -} diff --git a/broker/garanti/garanti.go b/brokers/garanti/garanti.go similarity index 80% rename from broker/garanti/garanti.go rename to brokers/garanti/garanti.go index cdf9bc9..6bfd567 100644 --- a/broker/garanti/garanti.go +++ b/brokers/garanti/garanti.go @@ -2,7 +2,7 @@ package garanti import ( "errors" - "github.com/guneyin/gobist-broker/broker" + "github.com/guneyin/gobist-broker/entity" "github.com/guneyin/gobist-broker/lib" "github.com/guneyin/gobist-broker/lib/reader" @@ -19,21 +19,21 @@ const ( ) type Garanti struct { - info broker.Info + info entity.Info } func New() *Garanti { return &Garanti{ - info: broker.Info{ - Enum: broker.Garanti, - Name: "Garanti Yatırım", - Title: "Garanti Yatırım Menkul Kıymetler A.Ş.", - Url: "https://www.garantibbvayatirim.com.tr/", - Logo: "https://www.garantibbvayatirim.com.tr/_assets/img/logo.svg", + info: entity.Info{ + Name: entity.Garanti.String(), + Title: "Garanti Yatırım", + TitleLong: "Garanti Yatırım Menkul Kıymetler A.Ş.", + Url: "https://www.garantibbvayatirim.com.tr/", + Logo: "https://www.garantibbvayatirim.com.tr/_assets/img/logo.svg", }} } -func (b Garanti) Info() broker.Info { +func (b Garanti) Info() entity.Info { return b.info } diff --git a/brokers/ncm/ncm.go b/brokers/ncm/ncm.go new file mode 100644 index 0000000..3999e48 --- /dev/null +++ b/brokers/ncm/ncm.go @@ -0,0 +1,28 @@ +package ncm + +import ( + "github.com/guneyin/gobist-broker/entity" +) + +type NCM struct { + info entity.Info +} + +func New() *NCM { + return &NCM{ + info: entity.Info{ + Name: entity.NCM.String(), + Title: "NCM Investment", + TitleLong: "NCM Investment Menkul Değerler A.Ş.", + Url: "https://ncminvest.com.tr/", + Logo: "https://ncminvest.com.tr/Resimler/5/5-logo.webp", + }} +} + +func (b NCM) Info() entity.Info { + return b.info +} + +func (b NCM) Parse(content []byte) (*entity.Transactions, error) { + return nil, nil +} diff --git a/broker/broker.go b/entity/broker.go similarity index 62% rename from broker/broker.go rename to entity/broker.go index 4e20a38..4b3df25 100644 --- a/broker/broker.go +++ b/entity/broker.go @@ -1,4 +1,4 @@ -package broker +package entity type EnumBroker string @@ -12,9 +12,9 @@ func (t EnumBroker) String() string { } type Info struct { - Enum EnumBroker - Name string - Title string - Url string - Logo string + Name string + Title string + TitleLong string + Url string + Logo string } diff --git a/entity/entity.go b/entity/transaction.go similarity index 100% rename from entity/entity.go rename to entity/transaction.go diff --git a/module.go b/module.go index fc7fd96..c3a2ea7 100644 --- a/module.go +++ b/module.go @@ -1,10 +1,9 @@ -package module +package broker import ( "errors" - "github.com/guneyin/gobist-broker/broker" - "github.com/guneyin/gobist-broker/broker/garanti" - "github.com/guneyin/gobist-broker/broker/ncm" + "github.com/guneyin/gobist-broker/brokers/garanti" + "github.com/guneyin/gobist-broker/brokers/ncm" "github.com/guneyin/gobist-broker/entity" "sync" ) @@ -15,11 +14,11 @@ var ( ) type Broker interface { - Info() broker.Info + Info() entity.Info Parse(content []byte) (*entity.Transactions, error) } -type Brokers map[broker.EnumBroker]Broker +type Brokers map[entity.EnumBroker]Broker var brokers Brokers @@ -28,8 +27,8 @@ func init() { once.Do(func() { brokers = Brokers{ - broker.Garanti: garanti.New(), - broker.NCM: ncm.New(), + entity.Garanti: garanti.New(), + entity.NCM: ncm.New(), } }) } @@ -38,16 +37,16 @@ func GetBrokers() Brokers { return brokers } -func GetBroker(b broker.EnumBroker) Broker { +func GetBroker(b entity.EnumBroker) Broker { return brokers[b] } func GetBrokerByName(name string) (Broker, error) { - if ok := broker.EnumBroker(name); ok == "" { + if ok := entity.EnumBroker(name); ok == "" { return nil, errors.New("UNSPPORTED_BROKER") } - b := broker.EnumBroker(name) + b := entity.EnumBroker(name) return GetBroker(b), nil } diff --git a/module_test.go b/module_test.go index b9ac8a4..b4ad708 100644 --- a/module_test.go +++ b/module_test.go @@ -1,4 +1,4 @@ -package module_test +package broker_test import ( "fmt" @@ -10,7 +10,7 @@ import ( ) func TestImporter(t *testing.T) { - brokers := module.GetBrokers() + brokers := broker.GetBrokers() assertNotNil(t, brokers) fmt.Println("Supported Brokers:") @@ -18,12 +18,12 @@ func TestImporter(t *testing.T) { i := 0 for _, v := range brokers { i++ - fmt.Printf(" %d- %s\n", i, v.Info().Name) + fmt.Printf(" %d- %s\n", i, v.Info().TitleLong) } fmt.Println() - b, err := module.GetBrokerByName("garanti") + b, err := broker.GetBrokerByName("garanti") assertError(t, err) assertNotNil(t, b) @@ -45,8 +45,8 @@ func TestImporter(t *testing.T) { } } -func importFile(b module.Broker, t string) (*entity.Transactions, error) { - fPath := fmt.Sprintf("testdata/%s/%s.csv", b.Info().Enum.String(), t) +func importFile(b broker.Broker, t string) (*entity.Transactions, error) { + fPath := fmt.Sprintf("testdata/%s/%s.csv", b.Info().Name, t) fileContent, err := os.ReadFile(fPath) if err != nil {