//Method to add a document to the mongoDB collection to store the exercise for viewing/editing public string AddExercise(string table, DayOfWorkoutsModel exerciseDay) { var collection = db.GetCollection <BsonDocument>(table); foreach (var item in exerciseDay.GetListOfExercises()) { var doc = new BsonDocument(item.GetExerciseInfo()); // convert dictionary class to Bson Document collection.InsertOne(doc); } return("Workouts added!"); }
static void Main(string[] args) { // call database methods off this object var myDB = new Models.WorkoutDB(Constants.DATABASE); // erase all documents in collection. This program is just a test to show database CRUD with mongoDB myDB.DeleteAll(Constants.DATABASE); // build up workout object to add to the database var e1 = new Models.ExerciseModel("squat", 405, 5, DateTime.Parse("3/21/2021"), "That was hard"); var e2 = new Models.ExerciseModel("bench", 315, 1, DateTime.Parse("3/21/2021"), "Nothing but a peanut"); var e3 = new Models.ExerciseModel("deadlift", 500, 10, DateTime.Parse("3/21/2021"), "I wish."); var workoutDay = new Models.DayOfWorkoutsModel(DateTime.Parse(e1.GetExerciseInfo()["date"])); workoutDay.AddExercise(e1); workoutDay.AddExercise(e2); workoutDay.AddExercise(e3); // add the workout to the database string myString = myDB.AddExercise(Constants.DATABASE, workoutDay); Console.WriteLine(myString); // print out all values in the table myDB.GetAllExercises(Constants.DATABASE); // test delete functionality; delete document based on inputted date and exercise var toDelete = new Dictionary <string, string>(); toDelete.Add("exercise", "squat"); var deleteDate = DateTime.Parse("3/21/2021"); toDelete.Add("date", deleteDate.ToString()); string delString = myDB.DeleteExercise(Constants.DATABASE, toDelete); Console.WriteLine(delString); // print out all values in the table myDB.GetAllExercises(Constants.DATABASE); // test update functionality; update a specified exercise var toUpdate = new Dictionary <string, string>(); toUpdate.Add("exercise", "deadlift"); var updateDate = DateTime.Parse("3/21/2021"); toUpdate.Add("date", updateDate.ToString()); var updateValues = new Dictionary <string, string>(); updateValues.Add("weight", 550.ToString()); updateValues.Add("reps", 7.ToString()); string updateStr = myDB.UpdateExercise(Constants.DATABASE, toUpdate, updateValues); Console.WriteLine(updateStr); // print out all values in the table myDB.GetAllExercises(Constants.DATABASE); Console.ReadLine(); }