public async Task TestCreateAsync_WithExistingName_ShouldReturnAlreadyExistingActivity()
        {
            var testActivityName        = "testActivityName";
            var testActivityDescription = "testActivityDescription";
            var testIcon = "testIconUrl";

            await this.workoutActivitiesRepository.AddAsync(new WorkoutActivity()
            {
                Name = testActivityName,
            });

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivityServiceModel = new WorkoutActivityServiceModel()
            {
                Name        = testActivityName,
                Description = testActivityDescription,
                Icon        = testIcon,
            };

            var actualResult = await this.workoutActivitiesService.CreateAsync(workoutActivityServiceModel);

            var expectedResult = await this.workoutActivitiesRepository.All()
                                 .Where(n => n.Name == testActivityName)
                                 .To <WorkoutActivityDetailsViewModel>()
                                 .FirstOrDefaultAsync();

            Assert.Equal(actualResult.Name, expectedResult.Name);
            Assert.Equal(actualResult.Icon, expectedResult.Icon);
            Assert.Equal(actualResult.Id, expectedResult.Id);
        }
Beispiel #2
0
        public async Task <WorkoutActivityDetailsViewModel> CreateAsync(WorkoutActivityServiceModel workoutActivityServiceModel)
        {
            var checkActivityExists = this.workoutActivitiesRepository.All()
                                      .FirstOrDefault(x => x.Name == workoutActivityServiceModel.Name);

            if (checkActivityExists != null)
            {
                return(AutoMapper.Mapper.Map <WorkoutActivityDetailsViewModel>(checkActivityExists));
            }

            var workoutActivity = new WorkoutActivity
            {
                Name        = workoutActivityServiceModel.Name,
                Description = workoutActivityServiceModel.Description,
                Icon        = workoutActivityServiceModel.Icon,
            };

            await this.workoutActivitiesRepository.AddAsync(workoutActivity);

            await this.workoutActivitiesRepository.SaveChangesAsync();

            var workoutActivityDetailsViewModel = AutoMapper.Mapper.Map <WorkoutActivityDetailsViewModel>(workoutActivity);

            return(workoutActivityDetailsViewModel);
        }