/// <summary> /// /// </summary> /// <param name="workoutDto"></param> /// <returns></returns> public async Task <bool> UpdateWorkout(AthleteWorkoutDto workoutDto) { //1. Update the Workout DB Record var workoutDb = Mapper.Map <Workout>(workoutDto); return(await _db.UpdateAsync(workoutDb)); }
public async void CreateWorkoutTest() { try { var athleteWorkout = new AthleteWorkoutDto() { Id = 0, Name = "Test #1 workout", Session = null, SessionId = 1, Topic = "Lower Body", SubTopic = "Body Core", Category = "Strength", Exercises = new List <AthleteExerciseDto>() { new AthleteExerciseDto() { Id = 1, Category = "Strength", Description = "Work on legs.", Duration = 90, ImageUrl = "http://exercises.youtrain.me.s3.amazonaws.com/wp-content/uploads/Dumbbell-Standing-Step-Up-622x485.png", IsDone = false, IsModified = false, Name = "Bench Press", Note = new NoteDto() { Id = 0, Text = "Rest 10 seconds", Created = DateTime.Parse("2017-03-03") }, TrainingLoad = "100", Sets = new AthleteExerciseSetDto() { Id = 0, Order = 1, RecoveryTimeInMin = 20, Reps = 15, Sets = 4, Weight = 100 } } } }; var result = await _service.CreateWorkout(athleteWorkout); Assert.NotEqual(false, result); } catch (System.Exception ex) { throw; } }
public async Task <IActionResult> UpdateAthleteWorkoutExercise([FromBody, Required] AthleteWorkoutDto workoutDto) { try { if (workoutDto == null) { return(BadRequest()); } if (await _service.UpdateWorkoutExercises(workoutDto)) { return(CreatedAtRoute("default", true)); } return(BadRequest("Failed to update the database")); } catch (Exception ex) { Log.Error(ex, ex.Message); return(BadRequest(ex.Message)); } }
/// <summary> /// /// </summary> /// <param name="workoutDto"></param> /// <returns></returns> public async Task <bool> UpdateWorkoutExercises(AthleteWorkoutDto workoutDto) { bool result = true; //1. Update the Workout Exercises DB Record foreach (var exercise in workoutDto.Exercises) { //Retrieve the Exercise entity from DB var exerciseRec = await _db.FindAsync <WorkoutExercise>(query => query .Include <Exercise>(join => join.InnerJoin() .Where($"{nameof(Exercise.Id):C} = @exerciseId")) .Include <Image>(join => join.LeftOuterJoin()) .WithParameters(new { exerciseId = exercise.Id })); if (exerciseRec == null) { continue; } var exerciseDb = exerciseRec.First(); //3. Update a Note DB Record var noteDb = Mapper.Map <Note>(exercise.Note); result &= await _db.UpdateAsync(noteDb); //4. Update Exercise DB Record var exercDb = Mapper.Map <Exercise>(exercise); //Update a Image DB Record by creating a new one and updating the exercise's Image ID, but only if the URL has changed if (exerciseDb.Exercise.Image.Url != exercise.ImageUrl) { var imgDb = new Image() { Url = exercise.ImageUrl }; await _db.InsertAsync(imgDb); exercDb.ImageId = imgDb.Id; } else { exercDb.ImageId = exerciseDb.Exercise.Image.Id; } result &= await _db.UpdateAsync(exercDb); //6. Update WorkoutExercise DB Record //var workoutExeDb = Mapper.Map<WorkoutExercise>(workoutDto.); //result &= await _db.UpdateAsync(workoutExeDb); //7. Update WorkoutExerciseData DB Record var workoutExeDataDb = Mapper.Map <WorkoutExerciseData>(exercise); workoutExeDataDb.WorkoutId = workoutDto.Id; workoutExeDataDb.WorkoutExerciseId = exerciseDb.Id; //Get Message Id var messageRec = await _db.FindAsync <WorkoutExerciseData>(query => query .Where($"{nameof(WorkoutExerciseData.Id):C} = @exerciseDataId") .Include <Message>(join => join.LeftOuterJoin()) .WithParameters(new { exerciseDataId = exercise.ExerciseDataId })); if (messageRec == null) { continue; } var messageDb = messageRec.First(); workoutExeDataDb.MessageId = messageDb.Message.Id; result &= await _db.UpdateAsync(workoutExeDataDb); } return(result); }
/// <summary> /// /// </summary> /// <param name="workoutDto"></param> /// <returns></returns> public async Task <bool> CreateWorkout(AthleteWorkoutDto workoutDto) { bool result = true; //1. Create Workout DB Record var workoutDb = Mapper.Map <Workout>(workoutDto); await _db.InsertAsync(workoutDb); result &= workoutDb.Id > 0; //2. Create a WorkoutData DB Record var workoutDataDb = Mapper.Map <WorkoutData>(workoutDto); workoutDataDb.WorkoutId = workoutDb.Id; workoutDataDb.Duration = workoutDto.Exercises.Sum(x => x.Duration); await _db.InsertAsync(workoutDataDb); result &= workoutDataDb.Id > 0; int sequenceOrder = 1; foreach (var exercise in workoutDto.Exercises) { //3. Create a Note DB Record var noteDb = new Note() { Text = exercise.Note.Text, Created = DateTime.UtcNow }; await _db.InsertAsync(noteDb); result &= noteDb.Id > 0; //4. Create a Imagete DB Record //Update a Image DB Record by creating a new one and updating the exercise's Image ID, but only if the URL has changed var imageRec = await _db.FindAsync <Image>(query => query .Where($"{nameof(Image.Url):C} = @imageUrl") .WithParameters(new { imageUrl = exercise.ImageUrl })); var imageId = (imageRec != null && imageRec.Any()) ? imageRec.First().Id : 0; if (imageId == 0) { var imgDb = new Image() { Url = exercise.ImageUrl }; await _db.InsertAsync(imgDb); result &= imgDb.Id > 0; imageId = imgDb.Id; } //5. Create Exercise DB Record var exercDb = Mapper.Map <Exercise>(exercise); exercDb.ImageId = imageId; await _db.InsertAsync(exercDb); result &= exercDb.Id > 0; //6. Create WorkoutExercise DB Record var workoutExeDb = Mapper.Map <WorkoutExercise>(exercise); workoutExeDb.SequenceNumber = sequenceOrder; await _db.InsertAsync(workoutExeDb); result &= workoutExeDb.Id > 0; //7. Create WorkoutExerciseData DB Record var workoutExeDataDb = Mapper.Map <WorkoutExerciseData>(exercise); workoutExeDataDb.WorkoutId = workoutDb.Id; workoutExeDataDb.WorkoutExerciseId = workoutExeDb.Id; workoutExeDataDb.NoteId = noteDb.Id; await _db.InsertAsync(workoutExeDataDb); result &= workoutExeDataDb.Id > 0; sequenceOrder++; } return(result); }
public async void UpdateWorkoutExerciseTest() { try { var athleteWorkout = new AthleteWorkoutDto() { Id = 1, Name = "Hazard's workout", Session = new SessionDto(), SessionId = 1, Topic = "Upper Body", SubTopic = "Body Core", Category = "Strength", Exercises = new List <AthleteExerciseDto>() { new AthleteExerciseDto() { Id = 0, Category = "Upper Body", Description = "When doing a bench press, take 3 seconds to drop from start position to a place around 4 inches away from your chest. Hold for a second before exploding back to start position as quickly as possible.", Duration = 0, ImageUrl = "http://jayantpandey.com/jayantpandey/ExerciseContent/img/flatbenchpress.jpg", IsDone = true, IsModified = false, Name = "Bench Press", Note = new NoteDto() { Id = 1, Text = "Rest 10 seconds and drink more liquids", Created = DateTime.Parse("2016-10-07") }, TrainingLoad = "195", Sets = new AthleteExerciseSetDto() { Id = 0, Order = 1, RecoveryTimeInMin = 25, Reps = 12, Sets = 3, Weight = 140 } }, new AthleteExerciseDto() { Id = 1, Category = "Upper Body", Description = "Work on Arms Strength", Duration = 0, ImageUrl = "https://bretcontreras.com/wp-content/uploads/Seated-Barbell-Overhead-Press.jpg", IsDone = false, IsModified = true, Name = "Military Press", Note = new NoteDto() { Id = 2, Text = "Do 3 sets", Created = DateTime.Parse("2016-10-12") }, TrainingLoad = "95", Sets = new AthleteExerciseSetDto() { Id = 1, Order = 2, RecoveryTimeInMin = 30, Reps = 6, Sets = 3, Weight = 80 } } } }; var result = await _service.UpdateWorkoutExercises(athleteWorkout); Assert.NotEqual(false, result); } catch (System.Exception ex) { Console.WriteLine(ex); throw; } }