change db location, remove multiple ids
This commit is contained in:
parent
5d45e648a3
commit
69006beddd
@ -29,9 +29,13 @@ var rmCmd = &cobra.Command{
|
|||||||
Long: `Remove a todo given its id.`,
|
Long: `Remove a todo given its id.`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
id, err := strconv.ParseUint(args[0], 10, 0)
|
var ids []uint64
|
||||||
|
for _, arg := range args {
|
||||||
|
id, err := strconv.ParseUint(arg, 10, 0)
|
||||||
errPanic(err, "failed to parse uint")
|
errPanic(err, "failed to parse uint")
|
||||||
deleteTodo(id)
|
ids = append(ids, id)
|
||||||
|
}
|
||||||
|
deleteTodo(ids)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -45,7 +45,7 @@ func parseDateString(date string) time.Time {
|
|||||||
|
|
||||||
// openDB returns gorm database.
|
// openDB returns gorm database.
|
||||||
func openDB() *gorm.DB {
|
func openDB() *gorm.DB {
|
||||||
db, err := gorm.Open(sqlite.Open("todo.db"), &gorm.Config{})
|
db, err := gorm.Open(sqlite.Open("/home/user/.todo.db"), &gorm.Config{})
|
||||||
errPanic(err, "failed to connect database")
|
errPanic(err, "failed to connect database")
|
||||||
|
|
||||||
return db
|
return db
|
||||||
@ -82,9 +82,11 @@ func updateTodo(id uint64, desc string, date string, comp bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// deleteTodo deletes an entry of id todo in the database.
|
// deleteTodo deletes an entry of id todo in the database.
|
||||||
func deleteTodo(id uint64) {
|
func deleteTodo(ids []uint64) {
|
||||||
db := openDB()
|
db := openDB()
|
||||||
|
for _, id := range ids {
|
||||||
db.Delete(&Todo{}, id)
|
db.Delete(&Todo{}, id)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// getAllTodos return all todos from the database.
|
// getAllTodos return all todos from the database.
|
||||||
|
Loading…
Reference in New Issue
Block a user