From 99a367f04b1393bfb4f34d9478861cfd271446d2 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Mon, 27 May 2024 05:13:03 +0700 Subject: [PATCH] chore: Renamed `MoneroQueryParams` The `MoneroQueryParams` renamed to `QueryNodes`. It become much more readable when calling it. Also added some comment in some functions. --- handler/response.go | 2 +- internal/monero/monero.go | 23 ++++++++++++++--------- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/handler/response.go b/handler/response.go index ed9a557..cb29ac0 100644 --- a/handler/response.go +++ b/handler/response.go @@ -45,7 +45,7 @@ func MoneroNode(c *fiber.Ctx) error { func MoneroNodes(c *fiber.Ctx) error { moneroRepo := monero.NewMoneroRepo(database.GetDB()) - query := monero.MoneroQueryParams{ + query := monero.QueryNodes{ RowsPerPage: c.QueryInt("limit", 10), Page: c.QueryInt("page", 1), SortBy: c.Query("sort_by", "id"), diff --git a/internal/monero/monero.go b/internal/monero/monero.go index 3f9a013..09a19dd 100644 --- a/internal/monero/monero.go +++ b/internal/monero/monero.go @@ -20,7 +20,7 @@ import ( type MoneroRepository interface { Node(id int) (Node, error) Add(protocol string, host string, port uint) error - Nodes(q MoneroQueryParams) (Nodes, error) + Nodes(QueryNodes) (Nodes, error) GiveJob(acceptTor int) (Node, error) ProcessJob(report ProbeReport, proberId int64) error NetFee() []NetFee @@ -36,6 +36,7 @@ func NewMoneroRepo(db *database.DB) MoneroRepository { return &MoneroRepo{db} } +// Node represents a single remote node type Node struct { ID uint `json:"id,omitempty" db:"id"` Hostname string `json:"hostname" db:"hostname"` @@ -67,6 +68,7 @@ type Node struct { CORSCapable bool `json:"cors" db:"cors_capable"` } +// Get node from database by id func (repo *MoneroRepo) Node(id int) (Node, error) { var node Node err := repo.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id) @@ -80,26 +82,29 @@ func (repo *MoneroRepo) Node(id int) (Node, error) { return node, err } +// Nodes represents a list of nodes type Nodes struct { TotalRows int `json:"total_rows"` RowsPerPage int `json:"rows_per_page"` Items []*Node `json:"items"` } -type MoneroQueryParams struct { - Host string - Nettype string - Protocol string - CC string // 2 letter country code - Status int - CORS int +type QueryNodes struct { + Host string + Nettype string + Protocol string + CC string // 2 letter country code + Status int + CORS int + + // pagination RowsPerPage int Page int SortBy string SortDirection string } -func (repo *MoneroRepo) Nodes(q MoneroQueryParams) (Nodes, error) { +func (repo *MoneroRepo) Nodes(q QueryNodes) (Nodes, error) { queryParams := []interface{}{} whereQueries := []string{} where := ""