refactor: Use proberRepo struct instead of interface

o need to use interface when calling `NewProber()`.
This commit is contained in:
Cristian Ditaputratama 2024-07-07 03:25:30 +07:00
parent 4d1a2da49c
commit 73308d2a32
Signed by: ditatompel
GPG key ID: 31D3D06D77950979

View file

@ -12,15 +12,7 @@ import (
const ProberAPIKey = "X-Prober-Api-Key" // HTTP header key const ProberAPIKey = "X-Prober-Api-Key" // HTTP header key
type ProberRepository interface { type proberRepo struct {
Add(name string) (Prober, error)
Edit(id int, name string) error
Probers(QueryProbers) ([]Prober, error)
CheckAPI(key string) (Prober, error)
Delete(id int) error
}
type ProberRepo struct {
db *database.DB db *database.DB
} }
@ -34,12 +26,12 @@ type Prober struct {
// Initializes a new ProberRepository // Initializes a new ProberRepository
// //
// NOTE: This "prober" is different with "probe" which is used to fetch a new job // NOTE: This "prober" is different with "probe" which is used to fetch a new job
func NewProber() ProberRepository { func NewProber() *proberRepo {
return &ProberRepo{db: database.GetDB()} return &proberRepo{db: database.GetDB()}
} }
// Add a new prober machine // Add a new prober machine
func (r *ProberRepo) Add(name string) (Prober, error) { func (r *proberRepo) Add(name string) (Prober, error) {
apiKey := uuid.New() apiKey := uuid.New()
query := ` query := `
INSERT INTO tbl_prober ( INSERT INTO tbl_prober (
@ -59,7 +51,7 @@ func (r *ProberRepo) Add(name string) (Prober, error) {
} }
// Edit an existing prober // Edit an existing prober
func (r *ProberRepo) Edit(id int, name string) error { func (r *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 := r.db.Exec(query, name, id) res, err := r.db.Exec(query, name, id)
if err != nil { if err != nil {
@ -76,7 +68,7 @@ func (r *ProberRepo) Edit(id int, name string) error {
} }
// Delete an existing prober // Delete an existing prober
func (r *ProberRepo) Delete(id int) error { func (r *proberRepo) Delete(id int) error {
res, err := r.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id) res, err := r.db.Exec(`DELETE FROM tbl_prober WHERE id = ?`, id)
if err != nil { if err != nil {
return err return err
@ -120,7 +112,7 @@ func (q QueryProbers) toSQL() (args []interface{}, where, sortBy, sortDirection
return args, where, sortBy, sortDirection return args, where, sortBy, sortDirection
} }
func (r *ProberRepo) Probers(q QueryProbers) ([]Prober, error) { func (r *proberRepo) Probers(q QueryProbers) ([]Prober, error) {
args, where, sortBy, sortDirection := q.toSQL() args, where, sortBy, sortDirection := q.toSQL()
var probers []Prober var probers []Prober
@ -153,7 +145,7 @@ func (r *ProberRepo) Probers(q QueryProbers) ([]Prober, error) {
return probers, nil return probers, nil
} }
func (r *ProberRepo) CheckAPI(key string) (Prober, error) { func (r *proberRepo) CheckAPI(key string) (Prober, error) {
var p Prober var p Prober
query := ` query := `
SELECT SELECT