From 59b368d91e4d656973c0d4fc809395dd8f5593c2 Mon Sep 17 00:00:00 2001 From: ditatompel Date: Mon, 6 May 2024 13:35:15 +0700 Subject: [PATCH] Add monero country list endpoint (backend) --- handler/response.go | 17 +++++++++++++++++ handler/routes.go | 1 + internal/repo/monero.go | 14 ++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/handler/response.go b/handler/response.go index c8eaa8e..559a33d 100644 --- a/handler/response.go +++ b/handler/response.go @@ -180,6 +180,23 @@ func NetFee(c *fiber.Ctx) error { }) } +func Countries(c *fiber.Ctx) error { + moneroRepo := repo.NewMoneroRepo(database.GetDB()) + countries, err := moneroRepo.Countries() + if err != nil { + return c.JSON(fiber.Map{ + "status": "error", + "message": err.Error(), + "data": nil, + }) + } + return c.JSON(fiber.Map{ + "status": "ok", + "message": "Success", + "data": countries, + }) +} + func GiveJob(c *fiber.Ctx) error { acceptTor := c.QueryInt("accept_tor", 0) diff --git a/handler/routes.go b/handler/routes.go index b9d71df..2403552 100644 --- a/handler/routes.go +++ b/handler/routes.go @@ -17,6 +17,7 @@ func V1Api(app *fiber.App) { v1.Get("/nodes", MoneroNodes) v1.Post("/nodes", AddNode) v1.Get("/fees", NetFee) + v1.Get("/countries", Countries) v1.Get("/job", CheckProber, GiveJob) v1.Post("/job", CheckProber, ProcessJob) v1.Get("/crons", Crons) diff --git a/internal/repo/monero.go b/internal/repo/monero.go index 868b01d..f62fab5 100644 --- a/internal/repo/monero.go +++ b/internal/repo/monero.go @@ -21,6 +21,7 @@ type MoneroRepository interface { GiveJob(acceptTor int) (MoneroNode, error) ProcessJob(report ProbeReport, proberId int64) error NetFee() []NetFee + Countries() ([]MoneroCountries, error) } type MoneroRepo struct { @@ -331,3 +332,16 @@ func (repo *MoneroRepo) NetFee() []NetFee { return netFees } + +type MoneroCountries struct { + TotalNodes int `json:"total_nodes" db:"total_nodes"` + Cc string `json:"cc" db:"country"` + Name string `json:"name" db:"country_name"` +} + +func (repo *MoneroRepo) Countries() ([]MoneroCountries, error) { + countries := []MoneroCountries{} + + err := repo.db.Select(&countries, `SELECT COUNT(id) AS total_nodes, country, country_name FROM tbl_node GROUP BY country ORDER BY country ASC`) + return countries, err +}