public async Task <IActionResult> Create(WorkoutCreateInputModel workoutCreateInputModel, List <string> acceptedPaymentMethods)
        {
            if (acceptedPaymentMethods.Count == 0)
            {
                this.ModelState.AddModelError("PaymentMethods", ModelConstants.Workout.PaymentMethodsError);
            }

            if (!this.ModelState.IsValid)
            {
                this.ViewData["Activities"] = await this.GetAllWorkoutActivitiesAsSelectListItems();

                var paymentMethodsByType = await this.GetAllPaymentMethodsByTypeAsync();

                this.ViewData["paymentMethodsInAdvance"] = paymentMethodsByType["paymentMethodsInAdvance"];
                this.ViewData["paymentMethodsOnSite"]    = paymentMethodsByType["paymentMethodsOnSite"];

                return(this.View(workoutCreateInputModel));
            }

            workoutCreateInputModel.PaymentMethods = acceptedPaymentMethods;
            var userId = this.User.FindFirst(ClaimTypes.NameIdentifier).Value;

            var result = await this.workoutsService.CreateAsync(workoutCreateInputModel, userId);

            return(this.RedirectToAction("Details", "Workouts", new { area = string.Empty, id = result.Id }));
        }
Ejemplo n.º 2
0
        public async Task TestCancelAsync_WithIncorrectWorkoutId_ShouldThrowNullRefEx()
        {
            var testUserId   = "testUserId";
            var testUserName = "******";

            var user = new TrainConnectedUser()
            {
                Id       = testUserId,
                UserName = testUserName,
            };

            await this.usersRepository.AddAsync(user);

            await this.usersRepository.SaveChangesAsync();

            var workoutActivity = new WorkoutActivity()
            {
                Id   = "activityId1",
                Name = "activityName1",
                Icon = "iconUrl1",
            };

            await this.workoutActivityRepository.AddAsync(workoutActivity);

            await this.workoutActivityRepository.SaveChangesAsync();

            var paymentMethod = new PaymentMethod()
            {
                Id               = "activityId1",
                Name             = "activityName1",
                PaymentInAdvance = false,
            };

            await this.paymentMethodsRepository.AddAsync(paymentMethod);

            await this.paymentMethodsRepository.SaveChangesAsync();

            WorkoutCreateInputModel workoutCreateInputModel = new WorkoutCreateInputModel()
            {
                Activity        = workoutActivity.Name,
                Duration        = 15,
                Location        = "location1",
                MaxParticipants = 10,
                Price           = 10.50m,
                Time            = DateTime.Now.AddDays(1),
                PaymentMethods  = new List <string>()
                {
                    paymentMethod.Name
                },
            };

            await this.workoutsService.CreateAsync(workoutCreateInputModel, testUserId);

            var createdWorkout = await this.workoutsRepository.All()
                                 .FirstOrDefaultAsync();

            var incorrectWorkoutId = "incorrect" + createdWorkout.Id;

            await Assert.ThrowsAsync <NullReferenceException>(async() => await this.workoutsService.CancelAsync(incorrectWorkoutId, testUserId));
        }
        public async Task <WorkoutDetailsViewModel> CreateAsync(WorkoutCreateInputModel workoutCreateInputModel, string userId)
        {
            var workoutActivity = this.workoutActivityRepository.All()
                                  .FirstOrDefault(x => x.Name == workoutCreateInputModel.Activity);

            if (workoutActivity == null)
            {
                throw new NullReferenceException(string.Format(ServiceConstants.Workout.NullReferenceActivityName, workoutCreateInputModel.Activity));
            }

            var user = this.usersRepository.All()
                       .FirstOrDefault(x => x.Id == userId);

            var paymentMethods = this.paymentMethodsRepository.All()
                                 .Where(n => workoutCreateInputModel.PaymentMethods.Contains(n.Name))
                                 .ToArray();

            if (paymentMethods.Count() == 0)
            {
                throw new NullReferenceException(string.Format(ServiceConstants.Workout.NullReferencePaymentMethodName));
            }

            var workout = new Workout
            {
                ActivityId      = workoutActivity.Id,
                Activity        = workoutActivity,
                Coach           = user,
                CoachId         = user.Id,
                Time            = workoutCreateInputModel.Time,
                Location        = workoutCreateInputModel.Location,
                Duration        = workoutCreateInputModel.Duration,
                Price           = workoutCreateInputModel.Price,
                Notes           = workoutCreateInputModel.Notes,
                MaxParticipants = workoutCreateInputModel.MaxParticipants,
            };

            await this.workoutsRepository.AddAsync(workout);

            await this.workoutsRepository.SaveChangesAsync();

            foreach (var paymentMethod in paymentMethods)
            {
                await this.workoutsPaymentsMethodsRepository.AddAsync(new WorkoutsPaymentMethods
                {
                    WorkoutId       = workout.Id,
                    PaymentMethodId = paymentMethod.Id,
                });
            }

            await this.workoutsPaymentsMethodsRepository.SaveChangesAsync();

            var workoutDetailsViewModel = AutoMapper.Mapper.Map <WorkoutDetailsViewModel>(workout);

            return(workoutDetailsViewModel);
        }
Ejemplo n.º 4
0
        public async Task <IActionResult> CreateWorkout(WorkoutCreateInputModel input)
        {
            if (!this.ModelState.IsValid)
            {
                return(this.View(input));
            }

            var workoutId = await this.workoutsService.CreateAsync(input.Name, input.StartTime, input.EndTime, input.PrivateTraining, input.Notes, input.TrainersIds, input.ExercisesIds, input.ClientsIds);

            this.TempData["InfoMessage"] = "The workout is created!";
            return(this.RedirectToAction("AllGroupWorkouts"));
        }
Ejemplo n.º 5
0
        public async Task TestCancelAsync_WithTestData_ShouldCancelWorkout()
        {
            var testUserId   = "testUserId";
            var testUserName = "******";

            var user = new TrainConnectedUser()
            {
                Id       = testUserId,
                UserName = testUserName,
            };

            await this.usersRepository.AddAsync(user);

            await this.usersRepository.SaveChangesAsync();

            var workoutActivity = new WorkoutActivity()
            {
                Id   = "activityId1",
                Name = "activityName1",
                Icon = "iconUrl1",
            };

            await this.workoutActivityRepository.AddAsync(workoutActivity);

            await this.workoutActivityRepository.SaveChangesAsync();

            var paymentMethod = new PaymentMethod()
            {
                Id               = "activityId1",
                Name             = "activityName1",
                PaymentInAdvance = false,
            };

            await this.paymentMethodsRepository.AddAsync(paymentMethod);

            await this.paymentMethodsRepository.SaveChangesAsync();

            WorkoutCreateInputModel workoutCreateInputModel = new WorkoutCreateInputModel()
            {
                Activity        = workoutActivity.Name,
                Duration        = 15,
                Location        = "location1",
                MaxParticipants = 10,
                Price           = 10.50m,
                Time            = DateTime.Now.AddDays(1),
                PaymentMethods  = new List <string>()
                {
                    paymentMethod.Name
                },
            };

            await this.workoutsService.CreateAsync(workoutCreateInputModel, testUserId);

            var initialResult = await this.workoutsRepository.All()
                                .ToArrayAsync();

            Assert.NotEmpty(initialResult);

            var workoutId = initialResult.FirstOrDefault().Id;

            await this.workoutsService.CancelAsync(workoutId, testUserId);

            var finalResult = await this.workoutsRepository.All()
                              .ToArrayAsync();

            Assert.Empty(finalResult);
        }
Ejemplo n.º 6
0
        public async Task TestGetCancelDetailsAsync_WithTestData_ShouldReturnWorkoutDetails()
        {
            var testUserId   = "testUserId";
            var testUserName = "******";

            var user = new TrainConnectedUser()
            {
                Id       = testUserId,
                UserName = testUserName,
            };

            await this.usersRepository.AddAsync(user);

            await this.usersRepository.SaveChangesAsync();

            var workoutActivity = new WorkoutActivity()
            {
                Id   = "activityId1",
                Name = "activityName1",
                Icon = "iconUrl1",
            };

            await this.workoutActivityRepository.AddAsync(workoutActivity);

            await this.workoutActivityRepository.SaveChangesAsync();

            var paymentMethod = new PaymentMethod()
            {
                Id               = "activityId1",
                Name             = "activityName1",
                PaymentInAdvance = false,
            };

            await this.paymentMethodsRepository.AddAsync(paymentMethod);

            await this.paymentMethodsRepository.SaveChangesAsync();

            WorkoutCreateInputModel workoutCreateInputModel = new WorkoutCreateInputModel()
            {
                Activity        = workoutActivity.Name,
                Duration        = 15,
                Location        = "location1",
                MaxParticipants = 10,
                Price           = 10.50m,
                Time            = DateTime.Now.AddDays(1),
                PaymentMethods  = new List <string>()
                {
                    paymentMethod.Name
                },
            };

            await this.workoutsService.CreateAsync(workoutCreateInputModel, testUserId);

            var expectedResult = await this.workoutsRepository.All()
                                 .Where(w => w.CoachId == testUserId)
                                 .To <WorkoutCancelViewModel>()
                                 .FirstOrDefaultAsync();

            var workoutId = expectedResult.Id;

            var actualResult = await this.workoutsService.GetCancelDetailsAsync(workoutId, testUserId);

            Assert.Equal(expectedResult.ActivityName, actualResult.ActivityName);
            Assert.Equal(expectedResult.Duration, actualResult.Duration);
            Assert.Equal(expectedResult.Price, actualResult.Price);
            Assert.Equal(expectedResult.Location, actualResult.Location);
            Assert.Equal(expectedResult.MaxParticipants, actualResult.MaxParticipants);
            Assert.Equal(expectedResult.Time, actualResult.Time);
            Assert.Equal(expectedResult.Id, actualResult.Id);
        }