Skip to content

Commit

Permalink
code refactored
Browse files Browse the repository at this point in the history
  • Loading branch information
guneyin committed Jun 22, 2023
1 parent c33788d commit 1678415
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 98 deletions.
25 changes: 13 additions & 12 deletions broker/broker.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package broker

type Broker struct {
Name string
Title string
Url string
Logo string
}

type TBroker string
type EnumBroker string

const (
Garanti TBroker = "garanti"
NCM TBroker = "ncm"
Garanti EnumBroker = "garanti"
NCM EnumBroker = "ncm"
)

func (b TBroker) String() string {
return string(b)
func (t EnumBroker) String() string {
return string(t)
}

type Info struct {
Enum EnumBroker
Name string
Title string
Url string
Logo string
}
28 changes: 18 additions & 10 deletions broker/garanti/adapter.go → broker/garanti/garanti.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@ const (
HISSE_SATIS = "Hisse Satış"
)

type Garanti broker.Broker

func (g Garanti) Get() *broker.Broker {
return &broker.Broker{
Name: broker.Garanti.String(),
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",
}
type Garanti struct {
info broker.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",
}}
}

func (b Garanti) Info() broker.Info {
return b.info
}

func (g Garanti) Parse(content []byte) (*entity.Transactions, error) {
func (b Garanti) Parse(content []byte) (*entity.Transactions, error) {
data, err := reader.ReadCSV(content, true)
if err != nil {
return nil, err
Expand Down
26 changes: 17 additions & 9 deletions broker/ncm/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,25 @@ import (
"github.com/guneyin/gobist-broker/entity"
)

type NCM broker.Broker
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 (g NCM) Get() *broker.Broker {
return &broker.Broker{
Name: broker.NCM.String(),
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 (g NCM) Parse(content []byte) (*entity.Transactions, error) {
func (b NCM) Parse(content []byte) (*entity.Transactions, error) {
return nil, nil
}
56 changes: 0 additions & 56 deletions importer/importer.go

This file was deleted.

53 changes: 53 additions & 0 deletions module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package module

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/entity"
"sync"
)

var (
_ Broker = (*garanti.Garanti)(nil)
_ Broker = (*ncm.NCM)(nil)
)

type Broker interface {
Info() broker.Info
Parse(content []byte) (*entity.Transactions, error)
}

type Brokers map[broker.EnumBroker]Broker

var brokers Brokers

func init() {
once := &sync.Once{}

once.Do(func() {
brokers = Brokers{
broker.Garanti: garanti.New(),
broker.NCM: ncm.New(),
}
})
}

func GetBrokers() Brokers {
return brokers
}

func GetBroker(b broker.EnumBroker) Broker {
return brokers[b]
}

func GetBrokerByName(name string) (Broker, error) {
if ok := broker.EnumBroker(name); ok == "" {
return nil, errors.New("UNSPPORTED_BROKER")
}

b := broker.EnumBroker(name)

return GetBroker(b), nil
}
27 changes: 16 additions & 11 deletions importer/importer_test.go → module_test.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
package importer_test
package module_test

import (
"fmt"
"github.com/guneyin/gobist-broker"
"github.com/guneyin/gobist-broker/entity"
"github.com/guneyin/gobist-broker/importer"
"os"
"reflect"
"testing"
)

func TestImporter(t *testing.T) {
brokers := importer.GetBrokers()
brokers := module.GetBrokers()
assertNotNil(t, brokers)

fmt.Println("Supported Brokers:")

for i, b := range brokers {
fmt.Printf(" %d- %s\n", i+1, b.Get().Title)
i := 0
for _, v := range brokers {
i++
fmt.Printf(" %d- %s\n", i, v.Info().Name)
}

b, err := importer.GetBrokerByName("garanti")
fmt.Println()

b, err := module.GetBrokerByName("garanti")
assertError(t, err)
assertNotNil(t, b)

Expand All @@ -35,20 +39,21 @@ func TestImporter(t *testing.T) {
assertError(t, err)
assertNotNil(t, ts)

fmt.Println("Imported Transactions:")
for _, item := range ts.Items {
fmt.Printf("%-10s %-35s %-5d %-10.2f %-15s\n", item.Symbol, item.Date, item.Quantity, item.Price, item.Type.String())
fmt.Printf(" %-10s %-35s %-5d %-10.2f %-15s\n", item.Symbol, item.Date, item.Quantity, item.Price, item.Type.String())
}
}

func importFile(b *importer.BrokerAdapter, t string) (*entity.Transactions, error) {
fPath := fmt.Sprintf("testdata/%s/%s.csv", b.Get().Name, t)
func importFile(b module.Broker, t string) (*entity.Transactions, error) {
fPath := fmt.Sprintf("testdata/%s/%s.csv", b.Info().Enum.String(), t)

file, err := os.ReadFile(fPath)
fileContent, err := os.ReadFile(fPath)
if err != nil {
return nil, err
}

return b.Parse(file)
return b.Parse(fileContent)
}

func assertError(t *testing.T, err error) {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 1678415

Please sign in to comment.