Skip to content

Commit

Permalink
Import records from a CSV file
Browse files Browse the repository at this point in the history
  • Loading branch information
suminb committed Apr 30, 2017
1 parent 0419114 commit bdc0824
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 3 deletions.
39 changes: 36 additions & 3 deletions goport/src/finance/importer.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,37 @@ func ImportAccounts(filePath string) error {
return nil
}

func ImportStockValues(filePath string, symbol string) {
// Imports trading records from a CSV file.
func ImportRecords(db *DB, filePath string) error {
ch := make(chan []string)
go ReadCSV(filePath, ch)
for row := range ch {
// "Test Account","AMZN",DEPOSIT,2017-02-24,1
accountName := strings.TrimSpace(row[0])
assetName := strings.TrimSpace(row[1])
recordType := RecordType(strings.TrimSpace(row[2]))
createdAt, _ := time.Parse("2006-01-02", strings.TrimSpace(row[3]))
quantity, _ := strconv.ParseInt(strings.TrimSpace(row[4]), 10, 64)

account := db.GetAccountByName(accountName)
asset := db.GetAssetByName(assetName)

record := Record{
Account: account,
Asset: asset,
Type: recordType,
CreatedAt: createdAt,
Quantity: int(quantity),
}
res := db.Raw.Create(&record)
if res.Error != nil {
return res.Error
}
}
return nil
}

func ImportStockValues(filePath string, symbol string) error {
db := ConnectDatabase()
defer db.Raw.Close()

Expand All @@ -109,7 +139,10 @@ func ImportStockValues(filePath string, symbol string) {
ch := make(chan AssetValue)
go ReadStockValues(filePath, ch)
for v := range ch {
fmt.Println("Processing", v)
db.Raw.Create(&v)
res := db.Raw.Create(&v)
if res.Error != nil {
return res.Error
}
}
return nil
}
12 changes: 12 additions & 0 deletions goport/src/finance/importer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,15 @@ func TestImportAccounts(t *testing.T) {
}
}
}

func TestImportRecords(t *testing.T) {
db := ConnectDatabase()
defer db.Raw.Close()

filePath := "test-data/records.csv"

err := ImportRecords(db, filePath)
if err != nil {
t.Error(err)
}
}
8 changes: 8 additions & 0 deletions goport/src/finance/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,18 @@ type DB struct {
// Returns an `Asset` instance matching the given name.
func (db *DB) GetAssetByName(name string) Asset {
var asset Asset
// FIXME: Deal with cases where no matching record found
db.Raw.First(&asset, "name = ?", name)
return asset
}

func (db *DB) GetAccountByName(name string) Account {
var account Account
// FIXME: Deal with cases where no matching record found
db.Raw.First(&account, "name = ?", name)
return account
}

func (db *DB) InsertAsset(name string, description string) (Asset, []error) {
asset := Asset{
Name: name,
Expand Down
6 changes: 6 additions & 0 deletions goport/src/finance/test-data/records.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
"Test Account","AMZN",deposit,2017-02-24,1
"Test Account","USD",withdraw,2017-02-24,800
"Test Account","AMZN",deposit,2017-03-24,1
"Test Account","USD",withdraw,2017-03-24,850
"Test Account","AMZN",deposit,2017-04-24,1
"Test Account","USD",withdraw,2017-04-24,900

0 comments on commit bdc0824

Please sign in to comment.