diff --git a/cmd/server/admin.go b/cmd/server/admin.go index a0c41eb..71ec906 100644 --- a/cmd/server/admin.go +++ b/cmd/server/admin.go @@ -44,6 +44,7 @@ func init() { cmd.Root.AddCommand(probersCmd) probersCmd.AddCommand(listProbersCmd) probersCmd.AddCommand(addProbersCmd) + probersCmd.AddCommand(editProbersCmd) probersCmd.AddCommand(deleteProbersCmd) listProbersCmd.Flags().StringP("sort-by", "s", "last_submit_ts", "Sort by column name, can be id or last_submit_ts") listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc") diff --git a/cmd/server/probers.go b/cmd/server/probers.go index 7ccea05..701065a 100644 --- a/cmd/server/probers.go +++ b/cmd/server/probers.go @@ -101,6 +101,33 @@ This command will display the prober name and API key when successfully executed }, } +var editProbersCmd = &cobra.Command{ + Use: "edit", + Short: "Edit prober", + Long: `Edit prober name by id.`, + Run: func(cmd *cobra.Command, _ []string) { + if err := database.ConnectDB(); err != nil { + fmt.Println(err) + return + } + proberId, err := strconv.Atoi(stringPrompt("Prober ID:")) + if err != nil { + fmt.Println("Invalid ID:", err) + return + } + + proberName := stringPrompt("Prober Name:") + proberRepo := repo.NewProberRepo(database.GetDB()) + err = proberRepo.Edit(proberId, proberName) + if err != nil { + fmt.Println("Failed to update prober:", err) + return + } + + fmt.Printf("Prober ID %d updated\n", proberId) + }, +} + var deleteProbersCmd = &cobra.Command{ Use: "delete", Short: "Delete prober", diff --git a/internal/repo/prober.go b/internal/repo/prober.go index 898bde1..fefaed1 100644 --- a/internal/repo/prober.go +++ b/internal/repo/prober.go @@ -11,7 +11,7 @@ import ( type ProberRepository interface { Add(name string) (Prober, error) - Update(id int, name string) error + Edit(id int, name string) error Probers(q ProbersQueryParams) ([]Prober, error) CheckApi(key string) (Prober, error) Delete(id int) error @@ -42,9 +42,19 @@ func (repo *ProberRepo) Add(name string) (Prober, error) { return Prober{Name: name, ApiKey: apiKey}, nil } -func (repo *ProberRepo) Update(id int, name string) error { +func (repo *ProberRepo) Edit(id int, name string) error { query := `UPDATE tbl_prober SET name = ? WHERE id = ?` - _, err := repo.db.Exec(query, name, id) + 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") + } return err }