Rename NewProberRepo to NewProber

Also rename ProbersQueryParams to QueryProbers
This commit is contained in:
Cristian Ditaputratama 2024-05-27 07:17:27 +07:00
parent ab82985bdf
commit 38320bc316
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
3 changed files with 24 additions and 21 deletions

View file

@ -48,8 +48,8 @@ xmr-nodes probers list -s last_submit_ts -d asc sin1`,
sortBy, _ := cmd.Flags().GetString("sort-by") sortBy, _ := cmd.Flags().GetString("sort-by")
sortDir, _ := cmd.Flags().GetString("sort-dir") sortDir, _ := cmd.Flags().GetString("sort-dir")
probersRepo := monero.NewProberRepo(database.GetDB()) probersRepo := monero.NewProber()
probers, err := probersRepo.Probers(monero.ProbersQueryParams{ probers, err := probersRepo.Probers(monero.QueryProbers{
Search: strings.Join(args, " "), Search: strings.Join(args, " "),
SortBy: sortBy, SortBy: sortBy,
SortDirection: sortDir, SortDirection: sortDir,
@ -108,7 +108,7 @@ This command will display the prober name and API key when successfully executed
proberName = stringPrompt("Prober Name:") proberName = stringPrompt("Prober Name:")
} }
proberRepo := monero.NewProberRepo(database.GetDB()) proberRepo := monero.NewProber()
prober, err := proberRepo.Add(proberName) prober, err := proberRepo.Add(proberName)
if err != nil { if err != nil {
fmt.Println(err) fmt.Println(err)
@ -135,7 +135,7 @@ var editProbersCmd = &cobra.Command{
} }
proberName := stringPrompt("Prober Name:") proberName := stringPrompt("Prober Name:")
proberRepo := monero.NewProberRepo(database.GetDB()) proberRepo := monero.NewProber()
err = proberRepo.Edit(proberId, proberName) err = proberRepo.Edit(proberId, proberName)
if err != nil { if err != nil {
fmt.Println("Failed to update prober:", err) fmt.Println("Failed to update prober:", err)
@ -161,7 +161,7 @@ var deleteProbersCmd = &cobra.Command{
return return
} }
proberRepo := monero.NewProberRepo(database.GetDB()) proberRepo := monero.NewProber()
err = proberRepo.Delete(proberId) err = proberRepo.Delete(proberId)
if err != nil { if err != nil {
fmt.Println("Failed to delete prober:", err) fmt.Println("Failed to delete prober:", err)

View file

@ -1,7 +1,6 @@
package handler package handler
import ( import (
"xmr-remote-nodes/internal/database"
"xmr-remote-nodes/internal/monero" "xmr-remote-nodes/internal/monero"
"github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2"
@ -17,9 +16,7 @@ func CheckProber(c *fiber.Ctx) error {
}) })
} }
proberRepo := monero.NewProberRepo(database.GetDB()) prober, err := monero.NewProber().CheckApi(key)
prober, err := proberRepo.CheckApi(key)
if err != nil { if err != nil {
return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{ return c.Status(fiber.StatusUnauthorized).JSON(fiber.Map{
"status": "error", "status": "error",

View file

@ -12,7 +12,7 @@ import (
type ProberRepository interface { type ProberRepository interface {
Add(name string) (Prober, error) Add(name string) (Prober, error)
Edit(id int, name string) error Edit(id int, name string) error
Probers(q ProbersQueryParams) ([]Prober, error) Probers(q QueryProbers) ([]Prober, error)
CheckApi(key string) (Prober, error) CheckApi(key string) (Prober, error)
Delete(id int) error Delete(id int) error
} }
@ -28,10 +28,14 @@ type Prober struct {
LastSubmitTs int64 `json:"last_submit_ts" db:"last_submit_ts"` LastSubmitTs int64 `json:"last_submit_ts" db:"last_submit_ts"`
} }
func NewProberRepo(db *database.DB) ProberRepository { // Initializes a new ProberRepository
return &ProberRepo{db} //
// NOTE: This "prober" is different with "probe" which is used to fetch a new job
func NewProber() ProberRepository {
return &ProberRepo{db: database.GetDB()}
} }
// Add a new prober machine
func (repo *ProberRepo) Add(name string) (Prober, error) { func (repo *ProberRepo) Add(name string) (Prober, error) {
apiKey := uuid.New() apiKey := uuid.New()
query := ` query := `
@ -51,6 +55,7 @@ func (repo *ProberRepo) Add(name string) (Prober, error) {
return Prober{Name: name, ApiKey: apiKey}, nil return Prober{Name: name, ApiKey: apiKey}, nil
} }
// Edit an existing prober
func (repo *ProberRepo) Edit(id int, name string) error { func (repo *ProberRepo) Edit(id int, name string) error {
query := `UPDATE tbl_prober SET name = ? WHERE id = ?` query := `UPDATE tbl_prober SET name = ? WHERE id = ?`
res, err := repo.db.Exec(query, name, id) res, err := repo.db.Exec(query, name, id)
@ -67,6 +72,7 @@ func (repo *ProberRepo) Edit(id int, name string) error {
return err return err
} }
// Delete an existing prober
func (repo *ProberRepo) Delete(id int) error { func (repo *ProberRepo) Delete(id int) error {
res, err := repo.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id) res, err := repo.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
if err != nil { if err != nil {
@ -82,13 +88,13 @@ func (repo *ProberRepo) Delete(id int) error {
return err return err
} }
type ProbersQueryParams struct { type QueryProbers struct {
Search string Search string
SortBy string SortBy string
SortDirection string SortDirection string
} }
func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) { func (repo *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
queryParams := []interface{}{} queryParams := []interface{}{}
whereQueries := []string{} whereQueries := []string{}
where := "" where := ""
@ -103,7 +109,7 @@ func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) {
where = "WHERE " + strings.Join(whereQueries, " AND ") where = "WHERE " + strings.Join(whereQueries, " AND ")
} }
probers := []Prober{} var probers []Prober
allowedSort := []string{"id", "last_submit_ts"} allowedSort := []string{"id", "last_submit_ts"}
sortBy := "last_submit_ts" sortBy := "last_submit_ts"
@ -133,18 +139,18 @@ func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) {
defer row.Close() defer row.Close()
for row.Next() { for row.Next() {
prober := Prober{} var p Prober
err = row.Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs) err = row.Scan(&p.Id, &p.Name, &p.ApiKey, &p.LastSubmitTs)
if err != nil { if err != nil {
return probers, err return probers, err
} }
probers = append(probers, prober) probers = append(probers, p)
} }
return probers, nil return probers, nil
} }
func (repo *ProberRepo) CheckApi(key string) (Prober, error) { func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
prober := Prober{} var p Prober
query := ` query := `
SELECT SELECT
id, id,
@ -156,6 +162,6 @@ func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
WHERE WHERE
api_key = ? api_key = ?
LIMIT 1` LIMIT 1`
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs) err := repo.db.QueryRow(query, key).Scan(&p.Id, &p.Name, &p.ApiKey, &p.LastSubmitTs)
return prober, err return p, err
} }