refactor: Use moneroRepo struct instead of interface

No need to use interface when calling `monero.New()`.
This commit is contained in:
Christian Ditaputratama 2024-07-07 03:13:11 +07:00
parent ef553dab9e
commit 4d1a2da49c
No known key found for this signature in database
GPG key ID: 31D3D06D77950979
2 changed files with 14 additions and 25 deletions

View file

@ -16,23 +16,12 @@ import (
"github.com/jmoiron/sqlx/types" "github.com/jmoiron/sqlx/types"
) )
type MoneroRepository interface { type moneroRepo struct {
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 {
db *database.DB db *database.DB
} }
func New() MoneroRepository { func New() *moneroRepo {
return &MoneroRepo{db: database.GetDB()} return &moneroRepo{db: database.GetDB()}
} }
// Node represents a single remote node // Node represents a single remote node
@ -68,7 +57,7 @@ type Node struct {
} }
// Get node from database by id // 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 var node Node
err := r.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id) err := r.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id)
if err != nil && err != sql.ErrNoRows { if err != nil && err != sql.ErrNoRows {
@ -162,7 +151,7 @@ type Nodes struct {
} }
// Get nodes from database // 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() args, where, sortBy, sortDirection := q.toSQL()
var nodes Nodes var nodes Nodes
@ -198,7 +187,7 @@ func (r *MoneroRepo) Nodes(q QueryNodes) (Nodes, error) {
return nodes, err 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" { if protocol != "http" && protocol != "https" {
return errors.New("Invalid protocol, must one of or HTTP/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() ip = hostIp.String()
} }
row, err := repo.db.Query(` row, err := r.db.Query(`
SELECT SELECT
id id
FROM FROM
@ -252,7 +241,7 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error {
return errors.New("Node already monitored") return errors.New("Node already monitored")
} }
statusDb, _ := json.Marshal([5]int{2, 2, 2, 2, 2}) statusDb, _ := json.Marshal([5]int{2, 2, 2, 2, 2})
_, err = repo.db.Exec(` _, err = r.db.Exec(`
INSERT INTO tbl_node ( INSERT INTO tbl_node (
protocol, protocol,
hostname, hostname,
@ -296,7 +285,7 @@ func (repo *MoneroRepo) Add(protocol string, hostname string, port uint) error {
return nil 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 { if _, err := r.db.Exec(`DELETE FROM tbl_node WHERE id = ?`, id); err != nil {
return err return err
} }
@ -314,7 +303,7 @@ type NetFee struct {
} }
// Get majority net fee from table tbl_fee // Get majority net fee from table tbl_fee
func (r *MoneroRepo) NetFees() []NetFee { func (r *moneroRepo) NetFees() []NetFee {
var netFees []NetFee var netFees []NetFee
err := r.db.Select(&netFees, ` err := r.db.Select(&netFees, `
SELECT SELECT
@ -338,7 +327,7 @@ type Countries struct {
} }
// Get list of countries (count by nodes) // Get list of countries (count by nodes)
func (r *MoneroRepo) Countries() ([]Countries, error) { func (r *moneroRepo) Countries() ([]Countries, error) {
var c []Countries var c []Countries
err := r.db.Select(&c, ` err := r.db.Select(&c, `
SELECT SELECT

View file

@ -78,7 +78,7 @@ type FetchLogs struct {
} }
// Logs returns list of fetched log result for given query // 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() args, where, sortBy, sortDirection := q.toSQL()
var fetchLogs FetchLogs 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 // 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{}{} args := []interface{}{}
wq := []string{} wq := []string{}
where := "" where := ""
@ -196,7 +196,7 @@ func (p *ProbeReport) parseStatuses() string {
} }
// Process report data from probers // 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 { if report.Node.ID == 0 {
return errors.New("Invalid node") return errors.New("Invalid node")
} }