Ability to edit prober name by ID from CLI #2

This commit is contained in:
Cristian Ditaputratama 2024-05-18 19:50:20 +07:00
parent b5e2787420
commit 120ba51c87
Signed by: ditatompel
GPG key ID: 31D3D06D77950979
3 changed files with 41 additions and 3 deletions

View file

@ -44,6 +44,7 @@ func init() {
cmd.Root.AddCommand(probersCmd) cmd.Root.AddCommand(probersCmd)
probersCmd.AddCommand(listProbersCmd) probersCmd.AddCommand(listProbersCmd)
probersCmd.AddCommand(addProbersCmd) probersCmd.AddCommand(addProbersCmd)
probersCmd.AddCommand(editProbersCmd)
probersCmd.AddCommand(deleteProbersCmd) 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-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") listProbersCmd.Flags().StringP("sort-dir", "d", "desc", "Sort direction, can be asc or desc")

View file

@ -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{ var deleteProbersCmd = &cobra.Command{
Use: "delete", Use: "delete",
Short: "Delete prober", Short: "Delete prober",

View file

@ -11,7 +11,7 @@ import (
type ProberRepository interface { type ProberRepository interface {
Add(name string) (Prober, error) Add(name string) (Prober, error)
Update(id int, name string) error Edit(id int, name string) error
Probers(q ProbersQueryParams) ([]Prober, error) Probers(q ProbersQueryParams) ([]Prober, error)
CheckApi(key string) (Prober, error) CheckApi(key string) (Prober, error)
Delete(id int) error Delete(id int) error
@ -42,9 +42,19 @@ func (repo *ProberRepo) Add(name string) (Prober, error) {
return Prober{Name: name, ApiKey: apiKey}, nil 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 = ?` 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 return err
} }