Skip to content

Instantly share code, notes, and snippets.

@wemgl
Last active February 4, 2019 01:57
Show Gist options
  • Save wemgl/c059aae8e7c05b2a242d6981c1b0d744 to your computer and use it in GitHub Desktop.
Save wemgl/c059aae8e7c05b2a242d6981c1b0d744 to your computer and use it in GitHub Desktop.
var t todo
idDoc := bson.D{{"_id", objectIDS}}
err = db.Collection(collName).FindOne(ctx, idDoc).Decode(&t)
if err != nil {
return "", fmt.Errorf("updateTask: couldn't decode task from db: %v", err)
}
var task string
fmt.Println("old task:", t.Task)
fmt.Print("updated task: ")
for input.Scan() {
if err := input.Err(); err != nil {
return "", fmt.Errorf("updateTask: couldn't read task from command line: %v", err)
}
task = input.Text()
if len(strings.Trim(task, " ")) == 0 {
return "", errors.New("updateTask: can't update a to-do item with no task")
}
t.Task = task
break
}
_, err = db.Collection(collName).UpdateOne(
ctx,
idDoc,
bson.D{
{"$set", bson.D{{"task", t.Task}}},
{"$currentDate", bson.D{{"modifiedAt", true}}},
},
)
if err != nil {
return "", fmt.Errorf("updateTask: task for to-do list couldn't be created: %v", err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment