2024-05-03 17:11:56 +00:00
|
|
|
package repo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2024-05-03 18:11:21 +00:00
|
|
|
"slices"
|
2024-05-03 17:11:56 +00:00
|
|
|
"strings"
|
2024-05-08 14:35:04 +00:00
|
|
|
"xmr-remote-nodes/internal/database"
|
2024-05-03 17:11:56 +00:00
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProberRepository interface {
|
2024-05-18 12:03:56 +00:00
|
|
|
Add(name string) (Prober, error)
|
2024-05-18 12:50:20 +00:00
|
|
|
Edit(id int, name string) error
|
2024-05-17 17:56:13 +00:00
|
|
|
Probers(q ProbersQueryParams) ([]Prober, error)
|
2024-05-04 12:52:22 +00:00
|
|
|
CheckApi(key string) (Prober, error)
|
2024-05-07 15:37:36 +00:00
|
|
|
Delete(id int) error
|
2024-05-03 17:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ProberRepo struct {
|
|
|
|
db *database.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
type Prober struct {
|
|
|
|
Id int64 `json:"id" db:"id"`
|
|
|
|
Name string `json:"name" db:"name"`
|
|
|
|
ApiKey uuid.UUID `json:"api_key" db:"api_key"`
|
|
|
|
LastSubmitTs int64 `json:"last_submit_ts" db:"last_submit_ts"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewProberRepo(db *database.DB) ProberRepository {
|
|
|
|
return &ProberRepo{db}
|
|
|
|
}
|
|
|
|
|
2024-05-18 12:03:56 +00:00
|
|
|
func (repo *ProberRepo) Add(name string) (Prober, error) {
|
|
|
|
apiKey := uuid.New()
|
2024-05-19 21:22:58 +00:00
|
|
|
query := `
|
|
|
|
INSERT INTO tbl_prober (
|
|
|
|
name,
|
|
|
|
api_key,
|
|
|
|
last_submit_ts
|
|
|
|
) VALUES (
|
|
|
|
?,
|
|
|
|
?,
|
|
|
|
?
|
|
|
|
)`
|
2024-05-18 12:03:56 +00:00
|
|
|
_, err := repo.db.Exec(query, name, apiKey, 0)
|
2024-05-03 17:11:56 +00:00
|
|
|
if err != nil {
|
2024-05-18 12:03:56 +00:00
|
|
|
return Prober{}, err
|
2024-05-03 17:11:56 +00:00
|
|
|
}
|
2024-05-18 12:03:56 +00:00
|
|
|
return Prober{Name: name, ApiKey: apiKey}, nil
|
2024-05-03 17:11:56 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 12:50:20 +00:00
|
|
|
func (repo *ProberRepo) Edit(id int, name string) error {
|
2024-05-07 16:23:31 +00:00
|
|
|
query := `UPDATE tbl_prober SET name = ? WHERE id = ?`
|
2024-05-18 12:50:20 +00:00
|
|
|
res, err := repo.db.Exec(query, name, id)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
row, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if row == 0 {
|
|
|
|
return fmt.Errorf("no rows affected")
|
|
|
|
}
|
2024-05-07 16:23:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *ProberRepo) Delete(id int) error {
|
2024-05-19 21:22:58 +00:00
|
|
|
res, err := repo.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
|
2024-05-18 12:39:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
row, err := res.RowsAffected()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if row == 0 {
|
|
|
|
return fmt.Errorf("no rows affected")
|
|
|
|
}
|
2024-05-07 16:23:31 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:56:13 +00:00
|
|
|
type ProbersQueryParams struct {
|
|
|
|
Search string
|
|
|
|
SortBy string
|
|
|
|
SortDirection string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (repo *ProberRepo) Probers(q ProbersQueryParams) ([]Prober, error) {
|
2024-05-03 17:11:56 +00:00
|
|
|
queryParams := []interface{}{}
|
|
|
|
whereQueries := []string{}
|
|
|
|
where := ""
|
|
|
|
|
2024-05-17 17:56:13 +00:00
|
|
|
if q.Search != "" {
|
|
|
|
whereQueries = append(whereQueries, "(name LIKE ? OR api_key LIKE ?)")
|
|
|
|
queryParams = append(queryParams, "%"+q.Search+"%")
|
|
|
|
queryParams = append(queryParams, "%"+q.Search+"%")
|
2024-05-03 17:11:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(whereQueries) > 0 {
|
|
|
|
where = "WHERE " + strings.Join(whereQueries, " AND ")
|
|
|
|
}
|
|
|
|
|
2024-05-17 17:56:13 +00:00
|
|
|
probers := []Prober{}
|
2024-05-03 17:11:56 +00:00
|
|
|
|
2024-05-03 18:11:21 +00:00
|
|
|
allowedSort := []string{"id", "last_submit_ts"}
|
2024-05-17 17:56:13 +00:00
|
|
|
sortBy := "last_submit_ts"
|
2024-05-03 18:11:21 +00:00
|
|
|
if slices.Contains(allowedSort, q.SortBy) {
|
|
|
|
sortBy = q.SortBy
|
|
|
|
}
|
|
|
|
sortDirection := "DESC"
|
|
|
|
if q.SortDirection == "asc" {
|
|
|
|
sortDirection = "ASC"
|
|
|
|
}
|
|
|
|
|
2024-05-19 21:22:58 +00:00
|
|
|
query := fmt.Sprintf(`
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
api_key,
|
|
|
|
last_submit_ts
|
|
|
|
FROM
|
|
|
|
tbl_prober
|
|
|
|
%s -- where clause if any
|
|
|
|
ORDER BY %s %s`, where, sortBy, sortDirection)
|
2024-05-03 17:11:56 +00:00
|
|
|
|
|
|
|
row, err := repo.db.Query(query, queryParams...)
|
|
|
|
if err != nil {
|
|
|
|
return probers, err
|
|
|
|
}
|
|
|
|
defer row.Close()
|
|
|
|
|
|
|
|
for row.Next() {
|
|
|
|
prober := Prober{}
|
|
|
|
err = row.Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
|
|
|
|
if err != nil {
|
|
|
|
return probers, err
|
|
|
|
}
|
2024-05-17 17:56:13 +00:00
|
|
|
probers = append(probers, prober)
|
2024-05-03 17:11:56 +00:00
|
|
|
}
|
|
|
|
return probers, nil
|
|
|
|
}
|
2024-05-04 12:52:22 +00:00
|
|
|
|
|
|
|
func (repo *ProberRepo) CheckApi(key string) (Prober, error) {
|
|
|
|
prober := Prober{}
|
2024-05-19 21:22:58 +00:00
|
|
|
query := `
|
|
|
|
SELECT
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
api_key,
|
|
|
|
last_submit_ts
|
|
|
|
FROM
|
|
|
|
tbl_prober
|
|
|
|
WHERE
|
|
|
|
api_key = ?
|
|
|
|
LIMIT 1`
|
2024-05-04 12:52:22 +00:00
|
|
|
err := repo.db.QueryRow(query, key).Scan(&prober.Id, &prober.Name, &prober.ApiKey, &prober.LastSubmitTs)
|
|
|
|
return prober, err
|
|
|
|
}
|