Add details node by ID endpoint

This commit is contained in:
Cristian Ditaputratama 2024-05-07 01:08:01 +07:00
parent 6e96a95ccd
commit d04473a807
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
3 changed files with 51 additions and 0 deletions

View file

@ -115,6 +115,41 @@ func Prober(c *fiber.Ctx) error {
}) })
} }
func MoneroNode(c *fiber.Ctx) error {
nodeId, err := c.ParamsInt("id", 0)
if err != nil {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
if nodeId == 0 {
return c.Status(fiber.StatusUnprocessableEntity).JSON(fiber.Map{
"status": "error",
"message": "Invalid node id",
"data": nil,
})
}
moneroRepo := repo.NewMoneroRepo(database.GetDB())
node, err := moneroRepo.Node(nodeId)
if err != nil {
return c.Status(fiber.StatusInternalServerError).JSON(fiber.Map{
"status": "error",
"message": err.Error(),
"data": nil,
})
}
return c.JSON(fiber.Map{
"status": "ok",
"message": "Success",
"data": node,
})
}
func MoneroNodes(c *fiber.Ctx) error { func MoneroNodes(c *fiber.Ctx) error {
moneroRepo := repo.NewMoneroRepo(database.GetDB()) moneroRepo := repo.NewMoneroRepo(database.GetDB())
query := repo.MoneroQueryParams{ query := repo.MoneroQueryParams{

View file

@ -16,6 +16,7 @@ func V1Api(app *fiber.App) {
v1.Post("/prober", Prober) v1.Post("/prober", Prober)
v1.Get("/nodes", MoneroNodes) v1.Get("/nodes", MoneroNodes)
v1.Post("/nodes", AddNode) v1.Post("/nodes", AddNode)
v1.Get("/nodes/id/:id", MoneroNode)
v1.Get("/nodes/logs", ProbeLogs) v1.Get("/nodes/logs", ProbeLogs)
v1.Get("/fees", NetFee) v1.Get("/fees", NetFee)
v1.Get("/countries", Countries) v1.Get("/countries", Countries)

View file

@ -1,6 +1,7 @@
package repo package repo
import ( import (
"database/sql"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt" "fmt"
@ -16,6 +17,7 @@ import (
) )
type MoneroRepository interface { type MoneroRepository interface {
Node(id int) (MoneroNode, error)
Add(protocol string, host string, port uint) error Add(protocol string, host string, port uint) error
Nodes(q MoneroQueryParams) (MoneroNodes, error) Nodes(q MoneroQueryParams) (MoneroNodes, error)
GiveJob(acceptTor int) (MoneroNode, error) GiveJob(acceptTor int) (MoneroNode, error)
@ -64,6 +66,19 @@ type MoneroNode struct {
CorsCapable bool `json:"cors" db:"cors_capable"` CorsCapable bool `json:"cors" db:"cors_capable"`
} }
func (repo *MoneroRepo) Node(id int) (MoneroNode, error) {
node := MoneroNode{}
err := repo.db.Get(&node, `SELECT * FROM tbl_node WHERE id = ?`, id)
if err != nil && err != sql.ErrNoRows {
fmt.Println("WARN:", err)
return node, errors.New("Can't get node information")
}
if err == sql.ErrNoRows {
return node, errors.New("Node not found")
}
return node, err
}
type MoneroNodes struct { type MoneroNodes struct {
TotalRows int `json:"total_rows"` TotalRows int `json:"total_rows"`
RowsPerPage int `json:"rows_per_page"` RowsPerPage int `json:"rows_per_page"`