generated from nelsonmarro/go-pro-proj-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repository pattern and db conn finished
- Loading branch information
1 parent
e2ff2a7
commit 09e3407
Showing
29 changed files
with
660 additions
and
40 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package models | ||
|
||
import "time" | ||
|
||
type Holding struct { | ||
ID int64 `json:"id"` | ||
Amount float64 `json:"amount"` | ||
PurchaseDate time.Time `json:"purchase_date"` | ||
PurchasePrice int `json:"purchase_price"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package repository | ||
|
||
import "database/sql" | ||
|
||
type DBInitializer interface { | ||
Migrate() error | ||
} | ||
|
||
type dbInitializer struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewDbInitializer(db *sql.DB) *dbInitializer { | ||
return &dbInitializer{db: db} | ||
} | ||
|
||
func (dbi *dbInitializer) Migrate() error { | ||
_, err := dbi.db.Exec(`CREATE TABLE IF NOT EXISTS holdings ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
amount REAL NOT NULL, | ||
purchase_date INTEGER NOT NULL, | ||
purchase_price INTEGER NOT NULL | ||
);`) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package repository | ||
|
||
import "testing" | ||
|
||
func TestDBInitializer_Migrate(t *testing.T) { | ||
err := testDBInitalizer.Migrate() | ||
if err != nil { | ||
t.Errorf("Error migrating database: %v", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
package repository | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"time" | ||
|
||
"github.com/nelsonmarro/gold-watcher/internal/models" | ||
) | ||
|
||
var ( | ||
errUpdateFailed = errors.New("failed to update the holding") | ||
errDeleteFailed = errors.New("failed to delete the holding") | ||
) | ||
|
||
type HoldingRepository interface { | ||
Create(holding models.Holding) (*models.Holding, error) | ||
GetAll() ([]models.Holding, error) | ||
GetByID(id int64) (*models.Holding, error) | ||
Update(id int64, holding models.Holding) error | ||
Delete(id int64) error | ||
} | ||
|
||
type holdingRepository struct { | ||
db *sql.DB | ||
} | ||
|
||
func NewHoldingRepository(db *sql.DB) *holdingRepository { | ||
return &holdingRepository{db: db} | ||
} | ||
|
||
func (r *holdingRepository) Create(holding models.Holding) (*models.Holding, error) { | ||
cmd := `INSERT INTO holdings (amount, purchase_date, purchase_price) VALUES (?, ?, ?);` | ||
|
||
result, err := r.db.Exec(cmd, holding.Amount, holding.PurchaseDate.Unix(), holding.PurchasePrice) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
id, err := result.LastInsertId() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
holding.ID = id | ||
return &holding, nil | ||
} | ||
|
||
func (r *holdingRepository) GetAll() ([]models.Holding, error) { | ||
query := `SELECT * FROM holdings;` | ||
|
||
rows, err := r.db.Query(query) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer rows.Close() | ||
|
||
holdings := make([]models.Holding, 0) | ||
unixTime := int64(0) | ||
|
||
for rows.Next() { | ||
holding := models.Holding{} | ||
err := rows.Scan(&holding.ID, &holding.Amount, &unixTime, &holding.PurchasePrice) | ||
if err != nil { | ||
return nil, err | ||
} | ||
holding.PurchaseDate = time.Unix(unixTime, 0) | ||
holdings = append(holdings, holding) | ||
} | ||
|
||
return holdings, nil | ||
} | ||
|
||
func (r *holdingRepository) GetByID(id int64) (*models.Holding, error) { | ||
row := r.db.QueryRow(`SELECT * FROM holdings WHERE id = ?;`, id) | ||
|
||
var h models.Holding | ||
var unixTime int64 | ||
err := row.Scan(&h.ID, &h.Amount, &unixTime, &h.PurchasePrice) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
h.PurchaseDate = time.Unix(unixTime, 0) | ||
return &h, nil | ||
} | ||
|
||
func (r *holdingRepository) Update(id int64, holding models.Holding) error { | ||
if id == 0 { | ||
return errors.New("invalid updated id") | ||
} | ||
|
||
cmd := `UPDATE holdings SET amount = ?, purchase_date = ?, purchase_price = ? WHERE id = ?;` | ||
|
||
res, err := r.db.Exec(cmd, holding.Amount, holding.PurchaseDate.Unix(), holding.PurchasePrice, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rowsAffected, err := res.RowsAffected() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if rowsAffected == 0 { | ||
return errUpdateFailed | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (r *holdingRepository) Delete(id int64) error { | ||
if id == 0 { | ||
return errors.New("invalid deleted id") | ||
} | ||
|
||
cmd := `DELETE FROM holdings WHERE id = ?;` | ||
|
||
res, err := r.db.Exec(cmd, id) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rowsAffected, err := res.RowsAffected() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if rowsAffected == 0 { | ||
return errDeleteFailed | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.