예제 #1
0
        public async Task <ActionResult> TrackWorkout(string userId, ExerciseInputModel input)
        {
            var exercise = new Exercise
            {
                Name             = input.Name,
                Sets             = input.Sets,
                Reps             = input.Reps,
                CaloriesPerRep   = input.CaloriesPerRep,
                UserId           = userId,
                ExerciseCategory = new ExerciseCategory {
                },
                Workout          = new Workout {
                }
            };

            await this.Save(exercise);

            await this.publisher.Publish(new ExerciseTrackedMessage
            {
                UserId   = userId,
                Calories = input.CaloriesPerRep *input.Reps *input.Sets
            });

            return(Result.Success);
        }
예제 #2
0
        public async Task CreateExerciseAsync(ExerciseInputModel input)
        {
            var exercise = new Exercise
            {
                MuscleGroupId = input.MuscleGroupId,
                Name          = input.Name,
                WorkoutId     = input.WorkoutId,
            };

            foreach (var inputSet in input.Sets)
            {
                var set = new Set
                {
                    Reps = inputSet.Reps,
                };

                exercise.Sets.Add(set);
            }

            var workout = this.db.Workouts.FirstOrDefault(x => x.Id == input.WorkoutId);

            await this.db.WorkoutExercises.AddAsync(new WorkoutExercise
            {
                Exercise = exercise,
                Workout  = workout,
            });

            await this.db.Exercises.AddAsync(exercise);

            await this.db.SaveChangesAsync();
        }
예제 #3
0
        public async Task AssignMuscleGroupsShouldAssignCorrectMuscleGroupsToExercise()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var workoutService = new WorkoutsService(dbContext);

            var muscleGroup = new MuscleGroup
            {
                Name = "testName",
            };

            await dbContext.MuscleGroups.AddAsync(muscleGroup);

            await dbContext.SaveChangesAsync();

            var exerciseToAdd = new ExerciseInputModel();

            var result = workoutService.AssignMuscleGroups(exerciseToAdd);

            Assert.NotNull(result.MuscleGroups);
            Assert.Equal("testName", result.MuscleGroups.FirstOrDefault().Text);
            Assert.Equal("1", result.MuscleGroups.FirstOrDefault().Value);
        }
예제 #4
0
        public IActionResult AddExerciseToWorkout()
        {
            var model = new ExerciseInputModel();

            var viewModel = this.workoutService.AssignMuscleGroups(model);

            return(this.View(viewModel));
        }
예제 #5
0
        public IActionResult Workout()
        {
            var viewModel = new ExerciseInputModel
            {
                Exercises = this.exercisesService.GetAll <ExerciseDropdownViewModel>(),
            };

            return(this.View(viewModel));
        }
예제 #6
0
        public ExerciseInputModel AssignMuscleGroups(ExerciseInputModel model)
        {
            model.MuscleGroups = this.db.MuscleGroups
                                 .Select(x => new SelectListItem
            {
                Text  = x.Name,
                Value = x.Id.ToString(),
            })
                                 .OrderBy(x => x.Text);

            return(model);
        }
예제 #7
0
        public async Task <IActionResult> AddExerciseToWorkout(ExerciseInputModel model)
        {
            if (!this.ModelState.IsValid)
            {
                this.workoutService.AssignMuscleGroups(model);

                return(this.View(model));
            }

            await this.workoutService.CreateExerciseAsync(model);

            this.TempData["Message"] = "Exercise added successfully to Workout";

            return(this.RedirectToAction("AddExerciseToWorkout", "Scheduler", new { id = model.WorkoutId }));
        }
예제 #8
0
        public async Task <ExerciseDto> CreateExerciseAsync(ExerciseInputModel input)
        {
            var exerciseForDb = input
                                .MapTo <Exercise>();

            var messageData = new ExerciseCreatedMessage {
            };

            var message = new Message(messageData);

            await this.Save(exerciseForDb, message);

            await this.publisher.Publish(messageData);

            await this.MarkMessageAsPublished(message.Id);

            var muscleGroupExercises = new List <MuscleGroupExercises>();

            foreach (var muscleGroupId in input.SecondaryMuscleGroups)
            {
                var muscleGroupExercise = new MuscleGroupExercises
                {
                    MuscleGroupId = muscleGroupId,
                    Exercise      = exerciseForDb
                };

                muscleGroupExercises.Add(muscleGroupExercise);
            }

            await context
            .MuscleGroupExercises
            .AddRangeAsync(muscleGroupExercises);

            await this.context
            .SaveChangesAsync();

            var exerciseFromDb = await this.context
                                 .Exercises
                                 .Include(x => x.MainMuscleGroup)
                                 .Include(x => x.SecondaryMuscleGroupExercises)
                                 .ThenInclude(x => x.MuscleGroup)
                                 .FirstOrDefaultAsync(x => x.Name == input.Name);

            return(exerciseFromDb
                   .MapTo <ExerciseDto>());
        }
예제 #9
0
        public async Task <ActionResult> Create(ExerciseInputModel input)
        {
            var isExerciseExists = await this.exercisesService
                                   .IsExerciseExistsByName(input.Name);

            if (isExerciseExists)
            {
                return(BadRequest(ExercisesConstants.ExistingExercise));
            }

            var result = await this.exercisesService
                         .CreateExerciseAsync(input);

            return(CreatedAtRoute("GetById",
                                  new { exerciseId = result.Id },
                                  result));
        }
예제 #10
0
        public async Task CreateExerciseMethodShouldAddCorrectNewExerciseToDbAndWorkout()
        {
            var optionsBuilder = new DbContextOptionsBuilder <ApplicationDbContext>()
                                 .UseInMemoryDatabase(Guid.NewGuid().ToString());
            var dbContext = new ApplicationDbContext(optionsBuilder.Options);

            var workoutService = new WorkoutsService(dbContext);

            var workoutToAdd = new WorkoutInputModel
            {
                Date     = DateTime.Now.Date,
                Duration = 180,
                TypeId   = 2,
            };

            await workoutService.CreateAsync(workoutToAdd, "Icaka99");

            var sets = new List <SetInputModel>
            {
                new SetInputModel {
                    ExerciseId = 2, Reps = 1,
                },
            };

            var exerciseToAdd = new ExerciseInputModel
            {
                MuscleGroupId = 1,
                Name          = "testName",
                WorkoutId     = 1,
                Sets          = sets,
            };

            await workoutService.CreateExerciseAsync(exerciseToAdd);

            Assert.True(dbContext.Exercises.Any());
            Assert.Equal(2, dbContext.Exercises.FirstOrDefault().Id);
            Assert.Equal(1, dbContext.Exercises.FirstAsync().Result.MuscleGroupId);
            Assert.Equal("testName", dbContext.Exercises.FirstAsync().Result.Name);
            Assert.True(dbContext.Exercises.FirstAsync().Result.Sets.Any());
            Assert.Equal(1, dbContext.Exercises.FirstAsync().Result.Sets.FirstOrDefault().Reps);
            Assert.Equal(2, dbContext.Exercises.FirstAsync().Result.Sets.FirstOrDefault().ExerciseId);
        }
예제 #11
0
        public async Task <ActionResult> TrackWorkout(ExerciseInputModel input)
        {
            var result = await this.exerciseService.TrackWorkout(this.currentUser.UserId, input);

            return(result);
        }