From 4d1a2da49c6a261933205505ee341560b425ad70 Mon Sep 17 00:00:00 2001 From: Christian Ditaputratama Date: Sun, 7 Jul 2024 03:13:11 +0700 Subject: [PATCH] refactor: Use moneroRepo struct instead of interface No need to use interface when calling `monero.New()`. --- internal/monero/monero.go | 33 +++++++++++---------------------- internal/monero/report.go | 6 +++--- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/internal/monero/monero.go b/internal/monero/monero.go index d661605..14ce586 100644 --- a/internal/monero/monero.go +++ b/internal/monero/monero.go @@ -16,23 +16,12 @@ import ( "github.com/jmoiron/sqlx/types" ) -type MoneroRepository interface { - Node(id int) (Node, error) - Add(protocol string, host string, port uint) error - Nodes(QueryNodes) (Nodes, error) - NetFees() []NetFee - Countries() ([]Countries, error) - GiveJob(acceptTor int) (Node, error) - ProcessJob(report ProbeReport, proberId int64) error - Logs(QueryLogs) (FetchLogs, error) -} - -type MoneroRepo struct { +type moneroRepo struct { db *database.DB } -func New() MoneroRepository { - return &MoneroRepo{db: database.GetDB()} +func New() *moneroRepo { + return &moneroRepo{db: database.GetDB()} } // Node represents a single remote node @@ -68,7 +57,7 @@ type Node struct { } // Get node from database by id -func (r *MoneroRepo) Node(id int) (Node, error) { +func (r *moneroRepo) Node(id int) (Node, error) { var node Node err := r.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id) if err != nil && err != sql.ErrNoRows { @@ -162,7 +151,7 @@ type Nodes struct { } // Get nodes from database -func (r *MoneroRepo) Nodes(q QueryNodes) (Nodes, error) { +func (r *moneroRepo) Nodes(q QueryNodes) (Nodes, error) { args, where, sortBy, sortDirection := q.toSQL() var nodes Nodes @@ -198,7 +187,7 @@ func (r *MoneroRepo) Nodes(q QueryNodes) (Nodes, error) { return nodes, err } -func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error { +func (r *moneroRepo) Add(protocol string, hostname string, port uint) error { if protocol != "http" && protocol != "https" { return errors.New("Invalid protocol, must one of or HTTP/HTTPS") } @@ -233,7 +222,7 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error { ip = hostIp.String() } - row, err := repo.db.Query(` + row, err := r.db.Query(` SELECT id FROM @@ -252,7 +241,7 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error { return errors.New("Node already monitored") } statusDb, _ := json.Marshal([5]int{2, 2, 2, 2, 2}) - _, err = repo.db.Exec(` + _, err = r.db.Exec(` INSERT INTO tbl_node ( protocol, hostname, @@ -296,7 +285,7 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error { return nil } -func (r *MoneroRepo) Delete(id uint) error { +func (r *moneroRepo) Delete(id uint) error { if _, err := r.db.Exec(`DELETE FROM tbl_node WHERE id = ?`, id); err != nil { return err } @@ -314,7 +303,7 @@ type NetFee struct { } // Get majority net fee from table tbl_fee -func (r *MoneroRepo) NetFees() []NetFee { +func (r *moneroRepo) NetFees() []NetFee { var netFees []NetFee err := r.db.Select(&netFees, ` SELECT @@ -338,7 +327,7 @@ type Countries struct { } // Get list of countries (count by nodes) -func (r *MoneroRepo) Countries() ([]Countries, error) { +func (r *moneroRepo) Countries() ([]Countries, error) { var c []Countries err := r.db.Select(&c, ` SELECT diff --git a/internal/monero/report.go b/internal/monero/report.go index e6b78c6..2525f41 100644 --- a/internal/monero/report.go +++ b/internal/monero/report.go @@ -78,7 +78,7 @@ type FetchLogs struct { } // Logs returns list of fetched log result for given query -func (r *MoneroRepo) Logs(q QueryLogs) (FetchLogs, error) { +func (r *moneroRepo) Logs(q QueryLogs) (FetchLogs, error) { args, where, sortBy, sortDirection := q.toSQL() var fetchLogs FetchLogs @@ -108,7 +108,7 @@ func (r *MoneroRepo) Logs(q QueryLogs) (FetchLogs, error) { } // GiveJob returns node that should be probed for the next time -func (r *MoneroRepo) GiveJob(acceptTor int) (Node, error) { +func (r *moneroRepo) GiveJob(acceptTor int) (Node, error) { args := []interface{}{} wq := []string{} where := "" @@ -196,7 +196,7 @@ func (p *ProbeReport) parseStatuses() string { } // Process report data from probers -func (r *MoneroRepo) ProcessJob(report ProbeReport, proberId int64) error { +func (r *moneroRepo) ProcessJob(report ProbeReport, proberId int64) error { if report.Node.ID == 0 { return errors.New("Invalid node") }