This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| objectIDS, err := primitive.ObjectIDFromHex(id) | |
| if err != nil { | |
| return 0, fmt.Errorf("deleteTask: couldn't convert to-do ID from input: %v", err) | |
| } | |
| idDoc := bson.D{{"_id", objectIDS}} | |
| res, err := db.Collection(collName).DeleteOne(ctx, idDoc) | |
| if err != nil { | |
| return 0, fmt.Errorf("deleteTask: couldn't delete to-do from db: %v", err) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| res, err := db.Collection(collName).InsertOne(ctx, bson.D{ | |
| {"task", t.Task}, | |
| {"createdAt", primitive.DateTime(timeMillis(t.CreatedAt))}, | |
| {"modifiedAt", primitive.DateTime(timeMillis(t.ModifiedAt))}, | |
| }) | |
| if err != nil { | |
| return "", fmt.Errorf("createTask: task for to-do list couldn't be created: %v", err) | |
| } | |
| return res.InsertedID.(primitive.ObjectID).Hex(), nil |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| func configDB(ctx context.Context) (*mongo.Database, error) { | |
| uri := fmt.Sprintf(`mongodb://%s:%s@%s/%s`, | |
| ctx.Value(usernameKey).(string), | |
| ctx.Value(passwordKey).(string), | |
| ctx.Value(hostKey).(string), | |
| ctx.Value(databaseKey).(string), | |
| ) | |
| client, err := mongo.NewClient(options.Client().ApplyURI(uri)) | |
| if err != nil { | |
| return nil, fmt.Errorf("todo: couldn't connect to mongo: %v", err) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // package declaration and imports omitted for brevity | |
| type key string | |
| const ( | |
| hostKey = key("hostKey") | |
| usernameKey = key("usernameKey") | |
| passwordKey = key("passwordKey") | |
| databaseKey = key("databaseKey") | |
| ) |
NewerOlder